Ejemplo n.º 1
0
        /// <summary>
        /// Train neural network
        /// </summary>
        /// <param name="input">Input data</param>
        /// <param name="output">Output data</param>
        /// <param name="rounds">Optional number of rounds (defaults to 1)</param>
        /// <returns></returns>
        public bool Train(NeuralData input, NeuralData output, long rounds = 1)
        {
            if ((input.Count != this.Layers[0].Neurons.Count) || (output.Count != this.Layers[this.Layers.Count - 1].Neurons.Count))
            {
                return(false);
            }

            for (long r = 0; r < rounds; r++)
            {
                Run(input);

                for (int i = 0; i < this.Layers[this.Layers.Count - 1].Neurons.Count; i++)
                {
                    Neuron neuron = this.Layers[this.Layers.Count - 1].Neurons[i];

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

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

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

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

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

                ++this.TrainingRounds;
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Internal function to return an array of double from an input string
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static double[] StringToNeural(string input)
        {
            var nd = new NeuralData();

            foreach (var c in input.ToCharArray())
            {
                string ch = c.ToString();
                double d;
                double.TryParse(ch, out d);

                nd.Add(d);
            }

            return(nd.ToArray());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes the neural network
        /// </summary>
        /// <param name="input">Input data</param>
        /// <returns></returns>
        public NeuralData Run(NeuralData input)
        {
            if (input.Count != this.Layers[0].NeuronCount)
            {
                return(null);
            }

            for (int l = 0; l < Layers.Count; l++)
            {
                Layer layer = Layers[l];

                for (int n = 0; n < layer.Neurons.Count; n++)
                {
                    Neuron neuron = layer.Neurons[n];

                    if (l == 0)
                    {
                        neuron.Value = input[n];
                    }
                    else
                    {
                        neuron.Value = 0;
                        for (int np = 0; np < this.Layers[l - 1].Neurons.Count; np++)
                        {
                            neuron.Value = neuron.Value + this.Layers[l - 1].Neurons[np].Value * neuron.Dendrites[np].Weight;
                        }

                        neuron.Value = Activate(this.Activation, neuron.Value + neuron.Bias);
                    }
                }
            }

            Layer last      = this.Layers[this.Layers.Count - 1];
            int   numOutput = last.Neurons.Count;

            double[] output = new double[numOutput];
            for (int i = 0; i < last.Neurons.Count; i++)
            {
                output[i] = last.Neurons[i].Value;
            }

            return(new NeuralData(output));
        }
Ejemplo n.º 4
0
        public Form1()
        {
            InitializeComponent();

            nn nw = new nn(1.25, new int[] { 2, 2, 1 }, Activation.SIGMOID, "TestNet");

            //nw = nw.Load(@"c:\tmp\test_multip.xml");

            //nw.SaveImage(@"c:\tmp\nn.jpg");

            for (long i = 0; i < 500000; i++)
            {
                nd outx = nw.OutputDataInt(0);
                nd inpx = new nd(0, 0);
                nw.Train(inpx, outx, 5);

                outx = nw.OutputDataInt(1);
                inpx = new nd(0, 1);
                nw.Train(inpx, outx, 5);

                outx = nw.OutputDataInt(1);
                inpx = new nd(1, 0);
                nw.Train(inpx, outx, 5);

                outx = nw.OutputDataInt(0);
                inpx = new nd(1, 1);
                nw.Train(inpx, outx, 5);
            }

            MessageBox.Show("Training rounds so far: " + nw.TrainingRounds.ToString());

            // *****************************************************************

            nd outp = nw.Run(new nd(1, 1));

            MessageBox.Show(outp.BinaryToInt().ToString());


            //nw.Save();
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Extenstion to NeuralData for quick conversion and initialization of integer output value
 /// </summary>
 /// <param name="nw"></param>
 /// <param name="number"></param>
 /// <returns></returns>
 public static NeuralData OutputDataInt(this NeuralNetwork nw, long number)
 {
     return(new NeuralData(NeuralData.ConvertToBinary(number, nw.OutputNeuronsCount)));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Extension to NeuralData for quick value conversion toward int
 /// </summary>
 /// <param name="nd"></param>
 /// <returns></returns>
 public static int BinaryToInt(this NeuralData nd)
 {
     return(NeuralData.BinaryToInt(nd.ToArray()));
 }