Hello, (Machine Learning) World!
By: Emannuel Carvalho
Tags: Coreml, Machine Learning, iOS, Beginner, Hello World

In a former post, I showed how to setup some basic tools for Machine Learning. Now it's time for our Hello, World!. In any language or platform, it's quite common to start learning with a simple program that prints out "Hello, World!". The Machine Learning — specially Neural Networks — equivalent to that is the MNIST, which is a dataset with annotated data on handwritten digits.

An example of a number "5" in the MNIST dataset.

If you've never ever trained a model or written any code, you might wanna start with that. And this is what this post is about. If you already know how to train a model, there will soon be a post here on how to import it to Core ML and use your model in an iOS app.

As I said in the previous post, many iOS developers have started thinking about Machine Learning since the launch of Core ML and that's great! Nonetheless, it is sometimes a little overwhelming to come into a completely new world with so many new things appearing everyday. It's hard to decide where to start. If you follow along, by the end of this post you will have trained your own model for handwritten digit recognition.

What we will do:

Review the necessary tools

Get the dataset

Build a very simple model

Train it on our data

Test it and check how well it performs

Think about what could make it better

Our toolbox 🛠🗜⚙️

If you need help installing the tools, you can take a look at this post. Basically, we will use Python 2 as the programming language and we will also use keras in order to get the dataset and build the model — it will also help with some preprocessing.

In order to make things a little more organised, I'm also using virtualenv and virtualenvwrapper to keep track of the versions and to avoid conflicts with the rest of the system; and I'm using jupyter to write the code together with the text explaining each step. Those last tools are optional though.

Getting the data

Keras makes it very easy for us to get the MNIST data for this first experiment. Once you have keras installed, all you need to do is import the mnist dataset helper and load it:

Preprocessing

Then, we will need some preprocessing in order to pass our data to the model. Basically, what we will do is change the shape of X_train and X_test ; change the its type; and normalize it. We will also change y_train and y_test a bit so they can be encoded as one-hot vectors (i.e. an array with 10 positions where each position represents a digit from 0–9):

Creating the model

Our first model will be very simple, so all we need is to import Sequential from keras.models and both Dense and Flatten from keras.layers . Once we have them we can create and compile our model:

Training and validating

Now that we have our model compiled, it's time for us to train it. We will be using a batch size of 32 and we will train for 10 epochs. X_test and y_test will be used as our validation data:

That very simple model can achieve 92% accuracy. That's not bad for a first time, is it?

But we can definitely do better.

Where can we go from here?

This was a very simple model and it's intended to make you less afraid of training a model and actually having a hands on experience with machine leraning.

There are many challenges you can move from this tutorial. You can decide to create a better model by using a convolutional neural network, which is commonly used for problems that envolve image processing and computer vision. You can also decide that 92% is fine for an MVP and try to convert your model into an .mlmodel file and use it in your iOS app. If you choose to do that, you will find some very interesting challenges on how to prepare the data in iOS for your model and how to interpret the results.

That's it for now!

I hope you had a good time creating and training your first neural network! I will soon write a post on how to import your model to Xcode using coremltools.

Until then, if you have any questions, comments, suggestions, etc. you can comment here below or reach me at twitter.