Prototyping with Swift Playgrounds

For the last few months I have been working with neural networks, trying to figure out exactly what the inputs to the network and the network architecture should be. This is harder than than you would imagine. And neural network architecture is still more of an art than a science.

After using and modifying Caffe for a while, all in C++, I realized we needed a better prototyping language. Don't get me wrong, I love C++. But it sucks for prototyping. Every time we want to include a new library we have to spends hours fiddling with make files. Not to mention that Caffe hasn't been ported over to C++11, which makes it pretty painful to work with.

I first considered using Python, after all Caffe has a Python interface. And there are great numeric and scientific libraries available. But there are a few major drawbacks, the main one being that the end product is going to be an iPad app. I don't want to have to rewrite all my code and I don't want to have to figure out how to replace all the libraries it depends on with iOS-compatible versions.

I also got a trial of MatLab, which seemed promising. After spending some time with it I realized it wasn't up to the task. The interface is sluggish, it crashes frequently and the MatLab language is horrible. It does generate C code that I can use, but I don't like using code that I don't understand. It would be hard to justify paying for a licence, let alone two.

I also had a quick look at Torch, but I never got plotting to work. And we didn't want to scratch all the work we did on Caffe and figure out all the installation on the servers from scratch.

So I finally decided go with Swift. I've been using the language for other projects and it's great. We can't use the code directly from Caffe or even compile it on the servers, but we can do all the preprocessing in Swift and spew out data that Caffe can understand.

I initially thought that we would have to spend a lot of time getting a set of basic tools and libraries but I was wrong. I initially used Surge but ended up creating my own fork Upsurge which has a pretty comprehensive set of numerical algorithms (based on the Accelerate framework). And then I wrote a pretty basic set of plotting views. And voilĂ !

I can write code in a playground and immediately see the results. So far it's been a breeze compared to anything else I tried. If you need to do some prototyping I highly recommend it.

Playground

Would love to hear if you've had good or bad experiences using playgrounds.


Update

As part of this I ended up creating a number of support frameworks, all in Swift:

  • Upsurge: Math utilities using the Accelerate framework
  • PlotKit: Plotting library
  • BrainCore: Swift neural network library (feed-forward only right now)
  • HDF5Kit: Read and write HDF5 files to interface with other apps