📜 ⬆️ ⬇️

Neural networks for dummies. Kohonen network

The article Neural Networks for Dummies. Home author Paul_Smith showed how easy it is to create a neural network for recognizing images. But there is one thing but what he described is not a neural network. Before his next article I want to tell you how to solve the same problem, but using the Kohonen neural network.

So, we will recognize the numbers written in white on black, such as these:
imageimageimageimageimageimage

Pictures are 45 by 45 pixels, which means that there will be 45 * 45 inputs to our neural network.
For simplicity, we recognize only numbers from 0 to 5, so we will have 6 neurons - one for each answer.

The structure of our neural network:
image
')
Each connection of the input network with a neuron has its own weight. The impulse, passing through the connection, changes: impulse = impulse * weight_communication.
The neuron receives pulses from all inputs and simply summarizes them. The neuron with the larger total impulse wins. Everything is simple, we realize!

Classes to represent network elements (C #):
//
public class Input
{
//
public Link [] OutgoingLinks;
}

//
public class Link
{
//
public Neuron Neuron;
//
public double Weight;
}

public class Neuron
{
//
public Link [] IncomingLinks;
//
public double Power { get; set; }
}

Creating and initializing a network is a boring business, for whom it is interesting - see the attached source. I will dwell only on the fact that the color of a pixel is a number from 0 to 255, with 0 being black, 255 being white, the colors between them are grayscale.

The state of the KohonenNetwork class is the Input [] array and the Neuron [] array:
public class KohonenNetwork
{
private readonly Input [] _inputs;
private readonly Neuron [] _neurons;
...
}

Suppose our network is already trained. Then, in order to find out what is depicted in the picture, we will call the Handle method, everything will be multiplied there, it will add up and there will be a maximum:
//
public int Handle( int [] input)
{
for ( var i = 0; i < _inputs.Length; i++)
{
var inputNeuron = _inputs[i];
foreach ( var outgoingLink in inputNeuron.OutgoingLinks)
{
outgoingLink.Neuron.Power += outgoingLink.Weight * input[i];
}
}
var maxIndex = 0;
for ( var i = 1; i < _neurons.Length; i++)
{
if (_neurons[i].Power > _neurons[maxIndex].Power)
maxIndex = i;
}
// :
foreach ( var outputNeuron in _neurons)
{
outputNeuron.Power = 0;
}
return maxIndex;
}

But before asking anything about the network, it must be taught. For training, we show pictures and indicate what is drawn on them:
image
Learning is the change in link weights:
public void Study( int [] input, int correctAnswer)
{
var neuron = _neurons[correctAnswer];
for ( var i = 0; i < neuron.IncomingLinks.Length; i++)
{
var incomingLink = neuron.IncomingLinks[i];
incomingLink.Weight = incomingLink.Weight + 0.5 * (input[i] - incomingLink.Weight);
}
}

After learning in two fonts, the neural network distinguishes numbers from other fonts. Including the control test will be passed on such figures here:
imageimageimageimageimageimage
Of course, such an article is no good for captcha recognition - everything stops working, you just need to move, stretch or rotate the image.
However, it becomes clear to everyone that using neural networks is not so difficult if we start with simple examples.
Source code

Source: https://habr.com/ru/post/143668/


All Articles