Exemple #1
0
            //Adjust the weights of the inputs
            public void AdjustWeights(NeuralNetwork neuralNetwork, int targetOutput, NeuralData neuralData, double learningRate)
            {
                double         initialError   = neuralData.Error; //Store the initial Error
                Queue <double> initialWeights = new Queue <double>();

                GetWeights(initialWeights);

                int count = 10;

                while (neuralData.Error >= initialError)
                {
                    foreach (Conection conection in Conections)
                    {     //Loop through every conection
                        if ((conection.ConectedFrom.Output > 0 && targetOutput == 0))
                        { //If the conected neuron fired and we wanted the neuron to not fire
                            conection.weight -= learningRate;
                            conection.ConectedFrom.AdjustWeights(targetOutput, learningRate);
                        }
                        else if ((conection.ConectedFrom.Output) == 0 && targetOutput == 1)
                        {   //If the conected neuron didn't fire and we wanted it to fire.
                            conection.weight += learningRate;
                            conection.ConectedFrom.AdjustWeights(targetOutput, learningRate);
                        }
                        neuralData = neuralNetwork.CalculateOutputs(neuralData);
                        neuralData.CalculateError();
                        if (neuralData.Error < initialError)
                        {
                            return;
                        }
                    }
                    count--;
                    if (count < 0)
                    {
                        //we were unable to improve the error, we must fix the weights
                        SetWeights(initialWeights);
                        neuralNetwork.CalculateOutputs(neuralData);
                        neuralData.CalculateError();
                        break;
                    }
                }
            }
Exemple #2
0
            public void Train()
            {
                Random rand = new Random();

                for (int i = 0; i < Globals.XmlData.TrainingIterations; i++)
                {
                    //Generate the input and target output
                    NeuralData neuralData = GenerateTrainingData(rand);

                    //Calculate the actual outputs from our generated inputs
                    neuralData = CalculateOutputs(neuralData);

                    //Calculate the error and print it
                    double initialError = neuralData.CalculateError();
                    neuralData.PrintInput();
                    neuralData.PrintTarget();
                    neuralData.PrintActual();
                    Console.WriteLine("Initial error: {0}", Math.Round(initialError, 3));

                    //Adjust the weights. If our error is zero, don't change anything
                    if (initialError > 0)
                    {
                        Console.WriteLine("Adjusting weights.");
                        //loop through each output and compare actual to target
                        for (int j = 0; j < neuralData.TargetOutput.Count; j++)
                        {
                            if (neuralData.ActualOutput[j] != neuralData.TargetOutput[j])
                            {   //Actual output does not match target output
                                NeuralLayers.Last().Neurons[j].AdjustWeights(this, neuralData.TargetOutput[j], neuralData, Math.Pow(neuralData.ActualOutput[j] - neuralData.TargetOutput[j], 2));
                            }
                        }
                        Console.WriteLine("New error: {0}", Math.Round(neuralData.CalculateError(), 3));
                        neuralData.PrintActual();
                    }
                    Console.WriteLine("");
                }
            }