A basic interactive introduction to neural networks

A basic introduction to neural networks

Introduction

First, let's define a neural network : It's a group of neurons connected to one another that takes one or more input and returns one or more outputs.
You can model a neural network as some kind of function.
The neural network that we will study here are made out of layers. Each layer contains neurons. The neurons inside a layer are not connected.
Each layer is a smaller network that is processing the information in it's own way. The first layer applies a first simple process, the second layer makes a more complex analysis and so on.

The purpose of neural networks

Usually, neural network are used to accomplish complex task that are not suited for a computer : Image recognition, Voice analysis ...
The strengh of neural network is their ability to "evolve" : You can modify a neural network so that it give slightly different answer. Then, you can test if your neural network is more efficient than previously and repeat. That way, the neural network will be improving itself over time. Given enough time to train and a good "teacher" (a function that evaluates the performances of the network), this kind of program can do anything.
Let's dive in and see precisely how they work.

How a neuron works ?

A quick explanation of what is happening :

We initialize the network (With 1 and 3 for example):

So the result of the networks is :
 
					(1*1*0.25) + (3*1*0.75) = 0.25 + 2.25 = 2.5 
				
General formula to compute the value of the first neuron of a layer based on the entering connections :
 
					StartValue + (n1_value * n1_weight * n1_connection_1) + (n2_value * n2_weight * n2_connection_1) + (n3_value * n3_weight * n3_connection_1) + ...
				
The code used to generate the network :


		

A more complex network example

Networks can be a lot more complex than what we've just seen : This network take 5 inputs and gives back 4 answers.
Here, the weight of each neuron and the weights of his connections are random to make things even more interesting.
The code used to generate the network :


		

The behaviour of a simple neuron given 2 outputs

We will now draw the results of a neural network given 2 outputs to show how powerfull a neuron is.

The result : (The coordinates goes from bottom (0) to top (100) and from left (0) to right (100))



Value of the first weight :
Value of the second weight :

We can see that just by changing 2 parameters, the neural network can process quite complex patterns.
Here are some remarquable cases :

That's already quite a lot of models that our network can simulate.
With more neurons, even more patterns can be analysed.
The code as usual :


		

A robot trying to grab apples

Let's try to train our network with a simple task : collecting apples in a field (green dots)
Ok, let's define the problem more precisely :

Now, we need to figure out a way to generate a network that is adapted to this task. We'll test 3 different networks :


Above, a visualization of what the robot sees and the 2d world. Below, the brain of the robot

Results :

The code is more complicated because we need to render the world. The neural network part is at the bottom.


		

A robot painter !

Work in progress. Just a fancy gradient generator for now.


			

Credits

Made by vanyle (2017) in javascript, Feel free to reuse the code.
The code to create networks is below.

The code used to generated all thoses networks