Build A Very Simple Neural Network — Part 1

Francesco Scassillo
6 min readJan 4, 2021

The goal of these lessons

The goal of these lessons is to understanding and write a neural network with Python. The neural network that we are to be going to build is a single neuron that given two inputs will return us one output.

This is the first part of the lessons, where we will take a first look at what is a neural network and trying to pose a question.

This is a start but you will be impersonated about how simple is.

Pose the question

We want a neural network that distinguishes African elephants and Indian elephants giving to it two input, two numerical values.

The two inputs are variables that describe something like an adult elephant (weight and length), the output will return us if the elephant is African or Indian.

What is a neural network?

According to Wikipedia: A neural network is a network or circuit of neurons, or in a modern sense, an artificial neural network, composed of artificial neurons or nodes. Thus a neural network is either a biological neural network, made up of real biological neurons or an artificial neural network, for solving artificial intelligence (AI) problems. The connections of the biological neuron are modeled as weights. A positive weight reflects an excitatory connection, while negative values mean inhibitory connections. All inputs are modified by weight and summed. This activity is referred to as a linear combination. Finally, an activation function controls the amplitude of the output. For example, an acceptable range of output is usually between 0 and 1, or it could be −1 and 1.

The resulting output is a value between 0 and 1, that we as humans need
to interpret. In our case, a result that is nearly to 0 will mean that is an African elephant, and a result nearly to 1 will mean that is an Indian one (or vice versa). But how we can say that one elephant is African and one is Indian? Simple you say, one lives in Africa and the other one in India. Well… but we want to distinguish them from their characteristics, like his length and his weight.

Dataset

Now we have to have a dataset where there are African elephant and Indian elephant that report the characteristics of each elephant, such as weight, length, and the race of the elephant.

Small dataset of elephants.

Now given this dataset we will insert a new elephant with a length of 6.6 meters and a weight of 6 tons, but without knowing what kind of race is. How can we assume which race is? As we can see from the dataset, African elephants are bigger than Indian elephants, and each elephant of a race share more or less the same range of measurements. So comparing the previous characteristics of the unknown elephant with the dataset we can determine which elephant race is. So let’s try to draw a cartesian graph where we will insert in the x-axis the weight and in the y-axis the length.

The blue dots are Indian elephants and the green dots are Afican elephants, the red dot is the unknown elephant. It is easy to see that the dots are in two different groups:

Making this separation we can certainly say that the mysterious elephant is an African elephant.

Now this work is easy with one elephant and this graph, but imagine having one thousand elephants to enter, it will be a lot of work… Fortunately, our computer could help us with training it to recognize Africans end Indians elephants, doing the work for us. Now we need a neural network to do this work.

Simple neural network diagram

Given this neuron in Input1 (I1) we will put the weight of an elephant and in Input2 (I2) the length.

I1 = 4.5I2 = 5.6

The output will result a completely random value like 0.9, this value must be interpreted from us. So we decide that a value nearly 0 is an African elephant and a value nerly 1 an Indian elephant. But our neuron not known what this value is and the outputs are always random, without a real meaning for us.

So this neural network is worthless and meaningless? Yes. But after will we see why.

First we have to understand from what the output is resulting. From a mathematical point of view a neural network is a function. In mathematic a function is a relation from the input and output, like:

4 -> 8, 7 -> 14f(x) = x*2f(2) = 4

This is a double function so output is the input moltplied to 2.

Returning to out neural network we have two variables in input (I1, I2) that are multiplied to their segment weight (w1, w2): I1*w1 and I2*w2, after either will be summed adding a variable called bias (b). (for now the bias is not in our interesting)

O(I1,I2) = (I1*w1) + (I1*w2) + b

Now we want that output is a value compressed to 0 and 1, to do this we need another function: the sigmoid function.

With this function every value in input give a value between 0 and 1, always. So in our neural network function, that we will call NR, we will put the sigmoid function.

NR(I1,I2) = Sigmoid((I1*w1) + (I2 * w2) + b)

The w1, w2 and b variables are random, so is attendible that the output of our function is meanless. We need to give to w1 and w2 values from a phase called training.

Traning a neural network means give to it a lot of example to learn.

Ok, we are going further in mathematical concepts, and is needed to take a whole article for this. At this point we have seeing fast the first essential functions, i break the mathematical part here.

More mathematics behind…

You could be interested in what is a cost function, error propagation, learning rate, etc. But since this lesson is more practical than theoretical will not talk about this in this article; if you could be interested, I will link you to another my article: Essentials Mathematics Behind Neural Networks (I really advise you to take a look).

Write some code

Ok, let’s start with the fun part.

This is the code of our neural network, in the second part we will analise the code deeply, for now play with it :).

Part two: (coming soon)

Let me know what you think in the comments and clap if you think that this lesson could be interesting.

Thank you for reading.

Once your computer is pretending to be a neural net, you get it to be able to do a particular task by just showing it a whole lot of examples.

Francesco Scassillo

--

--

Francesco Scassillo

I'm a freelance software developer. Deep thinker appasionte about philosophy.