Good day. Suppose I want to make a simple neural network for XOR:


X = 0, Y = 0, O = 0 | X = 1, Y = 0, O = 1 | X = 0, Y = 1, O = 1 | X = 1, Y = 1, O = 0


I understand that 1.1 and 2.1 are input and the value is transferred to them without change. After transmitting the signals further, in each neuron the value is multiplied by weight, etc. At the end I have to compare the outputs of 1.3 and 2.3 and get the answer, if it is 1 row, then the answer is 0, if 2 is the answer 1. neural network example For simplicity, let the neuron class contain the weight per input (ie, 2) and the signal strength:

Class Neuron Public weight() As Double = {0, 0} Public power As Double = 0.0 End Class 

Next we create an array and get a random weight, within the unit:

 Dim neural(2, 1) As Neuron Dim rand As New Random For I As Integer = 0 To 2 Step 1 For J As Integer = 0 To 1 Step 1 neural(I, J) = New Neuron neural(I, J).weight(0) = rand.NextDouble() neural(I, J).weight(1) = rand.NextDouble() Next Next 

We get the input values:

 neural(0, 0).power = Console.ReadLine() neural(0, 1).power = Console.ReadLine() 

Activation function:

 Function Sigmoid(ByRef int As Double) As Double Const e_sigmoid As Double = 2.71828 Return (1 / (1 + (e_sigmoid ^ (-(int))))) End Function 

And a simple code of operation of this neural network:

 For I As Integer = 1 To 2 Step 1 neural(I, 0).power = Sigmoid(((neural(I - 1, 0).power * neural(I, 0).weight(0)) + (neural(I - 1, 1).power * neural(I, 0).weight(1)))) neural(I, 1).power = Sigmoid(((neural(I - 1, 0).power * neural(I, 1).weight(0)) + (neural(I - 1, 1).power * neural(I, 1).weight(1)))) Next 

Now I have a question whether it will work and how to train it? Examples are not important on what.

  • There are ready-made tools. Are you for training? Yes, you can also use the Python libraries through IronPython - Arantler
  • Now you need backpropagation to do for your network. Without this, it will not work to train her. For such a simple network, you can implement a gradient descent. This is usually slower, but easier to implement. - sanmai
  • And for XOR there should be enough three neurons - sanmai
  • I cannot train it, because I cannot understand how to calculate the error: the error is equal to the difference between the expected result and the result of the neuron. So, the output of the neuron is some 0.7, and consider 1 XOR 1 = 0, then 0 - 0.7? - macced
  • Just if possible, can you give an example on the knee, pseudocode, etc. - macced

1 answer 1

He listened to the comment about 3 neurons. Simple implementation:

enter image description here

 Module Module1 'Переменные НС: Dim enters() As Double = {0, 0} Dim hidden_layer() As Double = {0, 0} Dim output As Double = 0 Dim synapses_hidden(,) As Double = {{0.0, 0.1}, {0.0, 0.1}} Dim synapses_out() As Double = {0.1, 0.0} 'Данные для обучения: Dim learn(,) As Integer = {{0, 0}, {1, 0}, {0, 1}, {1, 1}} 'Обучает всем ответам кроме 0 - 0 - 1: Dim learn_answer() As Integer = {0, 1, 1, 1} Sub Main() 'Обучаем НС: Console.WriteLine("::: TRAINING NETWORK START :::") 'Глобальная переменная и временная: Dim global_error As Double = 0.0 Dim currect_error As Double = 0.0 'Массив ошибок: Dim errors(hidden_layer.Length - 1) 'Счетчик итераций: Dim iteration As Integer = 0 'Выполняем пока есть ошибка: Do global_error = 0 For I As Integer = 0 To learn.Length / 2 - 1 Step 1 'Заполняем вход для НС: enters(0) = learn(I, 0) enters(1) = learn(I, 1) 'Суммируем: StartNetwork() 'Получаем погрешность: currect_error = learn_answer(I) - output 'Пишем в глобальную: global_error += Math.Abs(currect_error) 'Пишем в массив ошибок: For J As Integer = 0 To errors.Length - 1 Step 1 errors(J) = currect_error * synapses_out(J) Next 'Корректируем веса (от выхода к входу): For J As Integer = 0 To enters.Length - 1 Step 1 For K As Integer = 0 To synapses_hidden.Length / 2 - 1 Step 1 synapses_hidden(K, J) += 0.1 * errors(J) * enters(J) Next Next 'Меняем следующий слой: For J As Integer = 0 To synapses_out.Length - 1 Step 1 synapses_out(J) += 0.1 * currect_error * hidden_layer(J) Next Next iteration += 1 Loop While global_error <> 0 Console.WriteLine("::: NUMBER OF ITERATIONS OF THE TRAINING -> " + iteration.ToString + " TIMES :::") 'Запуск НС For I As Integer = 0 To learn.Length / 2 - 1 Step 1 'Заполняем входные данные enters(0) = learn(I, 0) enters(1) = learn(I, 1) 'Суммируем StartNetwork() 'Выводим результат Console.WriteLine(output) Next Console.ReadLine() End Sub Sub StartNetwork() 'Суммируем для скрытого слоя: For I As Integer = 0 To hidden_layer.Length - 1 Step 1 hidden_layer(I) = 0 For J As Integer = 0 To enters.Length - 1 Step 1 hidden_layer(I) += synapses_hidden(I, J) * enters(J) Next 'Функция активации: If hidden_layer(I) > 0.5 Then hidden_layer(I) = 1.0 Else hidden_layer(I) = 0.0 End If Next output = 0 'Выходной слой: For I As Integer = 0 To hidden_layer.Length - 1 Step 1 output += synapses_out(I) * hidden_layer(I) Next 'Функция активации: If output > 0.5 Then output = 1 Else output = 0 End If End Sub End Module