Пример #1
0
        public static bool Train(NeuralNetwork nn, double[] input, double[] output)
        {
            if (input.Length != nn.Layers[0].Neurons.Count ||
                output.Length != nn.Layers[nn.Layers.Count - 1].Neurons.Count)
            {
                return(false);
            }

            nn.Run(input);

            for (var i = 0; i < nn.Layers[nn.Layers.Count - 1].Neurons.Count; i++)
            {
                var neuron = nn.Layers[nn.Layers.Count - 1].Neurons[i];

                neuron.Delta = neuron.Value * (1 - neuron.Value) * (output[i] - neuron.Value);

                for (var j = nn.Layers.Count - 2; j >= 1; j--)
                {
                    for (var k = 0; k < nn.Layers[j].Neurons.Count; k++)
                    {
                        var n = nn.Layers[j].Neurons[k];

                        n.Delta = n.Value *
                                  (1 - n.Value) *
                                  nn.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
                                  nn.Layers[j + 1].Neurons[i].Delta;
                    }
                }
            }

            for (var i = nn.Layers.Count - 1; i >= 1; i--)
            {
                for (var j = 0; j < nn.Layers[i].Neurons.Count; j++)
                {
                    var n = nn.Layers[i].Neurons[j];
                    n.Bias = n.Bias + nn.LearningRate * n.Delta;

                    for (var k = 0; k < n.Dendrites.Count; k++)
                    {
                        n.Dendrites[k].Weight = n.Dendrites[k].Weight +
                                                nn.LearningRate * nn.Layers[i - 1].Neurons[k].Value * n.Delta;
                    }
                }
            }

            return(true);
        }
Пример #2
0
 private void buttonRun_Click(object sender, EventArgs e)
 {
     _neuralNetwork.Run(_param.InitValues.ToArray());
 }