This is the simplest artificial neural network possible explained and demonstrated.
- part 1 - simplest network (you are here)
- part 2 - backpropagation
- part 3 - backpropagation-continued
Artificial neural networks are inspired by the brain by having interconnected artificial neurons store patterns and communicate with each other.
The simplest form of an artificial neuron has one or multiple inputs
At the simplest level, the output is the sum of its inputs times its weights. $$ y = \sum_{i=1}^n w_i x_i $$
The purpose of a network is to learn a certain output
Say we have a network with two inputs
The idea is to adjust the weights in such a way that the given inputs produce the desired output.
Weights are normally initialized randomly since we can't know their optimal value ahead of time, however for simplicity we will initialize them both to
If we calculate the output of this network, we will get $$ y = x_1 w_1 + x_2 w_2 = 0.2 * 1.0 + 0.4 * 1.0 = 0.6$$
If the output
For example, if we wanted to get a target value of
One common way to measure the error (also referred to as the cost function) is to use the mean squared error:
If we had multiple associations of inputs and target values, then the error becomes the average sum of each association.
We use the mean squared error to measure how far away the results are from our desired target. The squaring removes negative signs and gives more weight to bigger differences between output and target.
To rectify the error, we would need to adjust the weights in a way that the output matches our target. In our example, lowering
However, in order to adjust the weights of our neural networks for many different inputs and target values, we need a learning algorithm to do this for us automatically.
The idea is to use the error to understand how each weight should be adjusted so that the error is minimized, but first, we need to learn about gradients.
It's essentially a vector pointing to the direction of the steepest ascent of a function. The gradient is denoted with
It looks like this for a two variable function:
Let's inject some numbers and calculate the gradient with a simple example.
Say we have a function
The descent part simply means using the gradient to find the direction of steepest ascent of our function and then going in the opposite direction by a small amount many times to find the function global (or sometimes local) minimum.
We use a constant called the learning rate, denoted with
If
For our two weights
For both
From now on we will denote the
Once we have the gradient, we can update our weights by subtracting the calculated gradient times the learning rate.
And we repeat this process until the error is minimized and is close enough to zero.
The included example teaches the following dataset to a neural network with two inputs and one output using gradient descent:
Once learned, the network should output ~0 when given two $1$s and ~$1$ when given a
docker build -t simplest-network .
docker run --rm simplest-network
- Artificial intelligence engines by James V Stone (2019)
- Complete guide on deep learning: http://neuralnetworksanddeeplearning.com/chap2.html