The simplest artificial neural network explained

Givi Odikadze
5 min readMay 11, 2022

--

Photo by Shubham Dhage on Unsplash

Theory

Mimicking neurons

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 xᵢ each having a specific weight wᵢ and one output y.

At the simplest level, the output is the sum of its inputs times its weights:

A simple example

The purpose of a network is to learn a certain output y given certain input(s) x by approximating a complex function with many parameters w that we couldn’t come up with ourselves.

Say we have a network with two inputs x₁ = 0.2 and x₂ = 0.4 and two weights w₁ and w₂.

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 1.

If we calculate the output of this network, we will get

The error

If the output y doesn’t match the expected target value, then we have an error.
For example, if we wanted to get a target value of t = 0.5, then we would have a difference 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 w₁ from 1.0 to 0.5 would do the trick, since

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.

Gradient descent

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.

What is a gradient?

It’s essentially a vector pointing to the direction of the steepest ascent of a function. The gradient is denoted with ∇ and is simply the partial derivative of each variable of a function expressed as a vector.

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 f(x, y) = 2x² + 3y³, then the gradient would be

What is gradient descent?

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 ϵ to define how small of a step to take in that direction.

If ϵ is too large, then we risk overshooting the function minimum, but if it’s too low then the network will take longer to learn and we risk getting stuck in a shallow local minimum.

Gradient descent applied to our example network

For our two weights w₁ and w₂ we need to find the gradient of those weights with respect to the error function E.

For both w₁ and w₂ we can find the gradient by using the chain rule

From now on we will denote the following expression as δ for simplicity:

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.

Code example

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 1s and ~1 when given a 1 and a 0.

How to run

Go on https://github.com/gokadin/ai-simplest-network

docker build -t simplest-network .
docker run --rm simplest-network

References

  1. Artificial intelligence engines by James V Stone (2019)
  2. Complete guide on deep learning: http://neuralnetworksanddeeplearning.com/chap2.html

--

--

Givi Odikadze

I’m a software engineer addicted to anything tech and science.