Nyheter 12 mars, 2021

How to train a neural network with machine learning

Machine learning based on artificial neural networks are everywhere and used widely in numerous applications. Feed these networks with information and they will eventually learn and be able to make predictions for new data. But how does the internals of these networks operate?

This article will explain how classification with artificial neural networks work, by walking through the process of training a neural network to see the difference between cats and dogs.

The dataset

The number one thing needed to train a neural network is a decent amount of structured data. Meaning each of the images must have a label associated with it, in our case – “cat” or “dog”.  Structured data also refers to data consistency, i.e all images are the same size and are preprocessed in the same way. We can’t expect to give the network 1000 random pictures and expect reasonable predictions. And this makes sense, right? If we want the algorithm to see the difference between a cat and a dog, we need to provide that information to the network.


The network

Let’s start at the very top level to understand what a neural network is and how it works. We have one yellow input layer, one or more blue hidden layer(s) and one red output layer.


The input layer is the data we’re using, transformed into a list of numerical values. In our case, each pixel of the cat/dog image would represent one yellow input node. The output layer nodes will each represent the probability that the data fed to the network corresponds to that particular label. In our case, the output would be the label “cat” or “dog”. In our case, a potential prediction probability could be the prediction confidence as a cat, dog, possibly both or neither of them.


Each of the arrows represents a connection of how the data travels through the network. Before feeding data to the neural network, each of the connections is given a random value between 0 and 1. When training the network, we feed the data through the network and tweak these connections to match the desired output – sort of like your brain would increase or decrease the power of certain neurons when experiencing or learning something new. The trained network takes new data and utilizes these fine-tuned connections to predict the probabilities of each label, cat or dog.


If we’re lucky, our network’s accuracy is good enough for our application and no fine-tuning is needed, however tweaking the size of the hidden layer(s) in the network, as well as other parameters, are usually needed. When we’re happy with our accuracy, we can export our model to run on a phone, server or someplace else where it makes sense. Training the models are processor intensive and the classification based on the training is not computing-intensive. Classification is a relatively faster process.


Machine learning in iOS


Four years back Apple released their own version of the Machine Learning framework, Core ML. The framework has evolved over the years and it has added support for neural networks designed to understand images, video, sound, and other rich media.


On-device learning and personalized User Experience:


Core ML models run on the user’s device so this makes an excellent framework choice for on-device training of ML models. All machine learning applications come with an initial basic version of the trained model. To stay relevant to the user’s behaviour this initial model version is updated by the on-device training of the models. Models learning from the user’s behaviour enables the app to display UI/workflows personalized to each user. This on-device learning also prevents data from leaving the user’s device.


Privacy complaint ML Models


Core ML supports out-of-the-box encryption for machine learning models. This feature makes it easy for your models to be GDPR compliant as well.


Machine learning to enrich the UX:


Some of the use cases for this mobile framework could be:

  • Identifying a person’s face in the image. (The yellow square you see in the Camera capture screen.)
  • Finding facial features for unlocking your device with FaceID.
  • Tracking the object movements in a video.
  • Scanning and recognizing barcodes.
  • Determining the angle to which an image is tilted.
  • Identifying language in a document.
  • Speech recognition, speech analysis, sound analysis.
  • Augment the writing process for authors.


The opportunities to automate workflows and enrich your digital product with machine learning are endless.


A simple use could be combining this Core ML framework with the Vision framework in iOS:

  • Understanding a dice roll event in a Casino


Machine learning frameworks in other platforms:

  • Notable Machine learning tools include: Amazon Machine Learning, Azure ML Studio, TensorFlow