With the introduction of iOS 10, Apple added a collection of functions to its Accelerate framework that allows developers to implement neural networks in their apps. With machine learning — specifically machine learning powered by neural networks — increasingly becoming a bigger part of many apps and iOS itself, it’s great to see Apple opening up APIs for running neural networks to third-party developers. I was thrilled to hear this news and could not wait to use iOS as an entry point to jump in and learn more about machine learning.

As I read through the documentation and watched the WWDC session on BNNS (Basic Neural Network Subroutines), I quickly realized that both of these resources are targeted towards those who are already familiar with neural networks and general machine learning concepts. As someone who was completely new to the field, I needed to read up and learn about machine learning basics before I could make use of these APIs.

What are neural networks?

Neural networks are one of the many ways to conduct machine learning. A neural network is made up of a large collection of neural processing units (or neurons). Connections between each of these neurons can be thought of as adaptive weights that are being tuned over time to be able to predict answers to familiar questions based on historical data. This process of tuning weights and biases of a neural network by feeding it input and adjusting them according is called training. On the other hand, the act of predicting output for a given input using weights and biases retrieved from a trained network is called inference.

Typically, a neural network is constructed using multiple layers of linked neurons, with the first layer being a representation of the input data and the last layer output data. It’s not unusual to have millions of neurons clustered together in a single neural network, especially for those that are used for performing non-trivial tasks such as image recognition. As data passes through the network, the network makes predictions of what it expects the output to be by using a predetermined activation function. Initially, these predictions are far off from the correct answers, but improve as the network gradually teaches itself to make better predictions by learning from its mistakes and adjusting itself over time.

What can you do with the BNNS API?

Starting with iOS 10, Apple is offering developers a set of efficient, optimized functions to create neural networks for inference. At the moment, there is no official API support for training neural networks. This means that you will have to pre-train a network and embed the resulting trained network as part of your app bundle. This is the approach Apple takes for speech recognition in Siri, and face recognition and search in Photos; iOS comes preloaded with a 200 megabyte “brain” for features such as these. A different approach to take would be to sidestep these API entirely and offload both the training and inference to a server cluster. This is how machine learning-backed features are implemented in apps today, most notably Gmail’s Smart Reply feature and Prisma’s art filters.

A Simple Example

Now that we have a basic understanding of how neural networks operate, we can apply this knowledge and implement a basic neural network for inference using the BNNS API. First, check out this blog post from imtrask and follow the step-by-step guide to implement a bare bones neural network. We are going to be using data retrieved from the network described in the second example.

To start, we need to define descriptors to configure the different layers of the network. In this example, the input layer is composed of three neurons, the hidden layer has four neurons and the output layer contains one neuron. The data type used for each layer is 32-bit Float type.

Next, we need to retrieve the weights from the example network and configure our network with them. In the example that we are looking at, the weights are defined and stored in syn1 and syn2 variables.

The last step in constructing our network is to put these pieces together. We are going to be using the sigmoid function as the activation function.

That’s it! We’ve created a miniature neural network; it’s time to put it to work and test it out. Let’s try to feed some input into the network and see how it does.

The network should be predicting 0.0026057, 0.00386756, 0.241905 and 0.428274 for each input set respectively. We can verify that these are the correct predictions by plugging the input stack used here into the original network.

Where to go from here?

I’ll admit up front: putting in three numbers and getting one number back is not a very exciting application of neural networks. But, this example demonstrates how neural networks operate at the very basic level. Just like with transistors in a computer, you get something powerful and amazing when you put millions of these units together.

If you would like to get into the nitty-gritty details and understand neural networks from the mathematical standpoint, I would recommend reading Michael Nielsen’s book, Neural Networks and Deep Learning, which not only covers the underlying principles but also shows you how to create and train a digit-recognition neural network. Another excellent resource is Matthijs Hollemans’ post on implementing a VGGNet image recognition network on the iPhone.

Machine learning and neural networks are only going to become more and more pervasive in the software that we use day-to-day going forward. With the release of iOS 10, now is the perfect time to start learning about them and think about how they can be integrated into our applications.

Share Button