RunEpoch() public method

Runs learning epoch.

While running the neural network's learning process, it is required to pass the same input and output values for each epoch. On the very first run of the method it will initialize evolutionary fitness function with the given input/output. So, changing input/output in middle of the learning process, will break it.

public RunEpoch ( double input, double output ) : double
input double Array of input vectors.
output double Array of output vectors.
return double
        public void TestGenetic()
        {
            ActivationNetwork network = new ActivationNetwork(new SigmoidFunction(), 2, 2, 1);
            EvolutionaryLearning superTeacher = new EvolutionaryLearning(network, 10);

            double lastError = double.MaxValue;
            int counter = 0;
            while (true)
            {
                counter++;
                var error = superTeacher.RunEpoch(input, output);
                if (lastError - error < 0.0000001 && error < 0.0001)
                    break;
                lastError = error;
            }

            Assert.IsTrue(Math.Abs(network.Compute(input[0])[0] - output[0][0]) < 0.03);
            Assert.IsTrue(Math.Abs(network.Compute(input[1])[0] - output[1][0]) < 0.03);
            Assert.IsTrue(Math.Abs(network.Compute(input[2])[0] - output[2][0]) < 0.03);
            Assert.IsTrue(Math.Abs(network.Compute(input[3])[0] - output[3][0]) < 0.03);
            Console.WriteLine($"Loop counter = {counter}.");
        }
        public void TestGenetic()
        {
            ActivationNetwork network = new ActivationNetwork(new SigmoidFunction(), inputCount, firstLayerNeurons, secondLayerNeurons, thirdLayerNeurons, lastLayerNeurons);
            EvolutionaryLearning superTeacher = new EvolutionaryLearning(network, 20);

            double lastError = double.MaxValue;
            int counter = 0;
            while (true)
            {
                counter++;
                var error = superTeacher.RunEpoch(input, output);
                if ((lastError - error < 0.00001 && error < 0.01) || counter > 12000)
                    break;
                lastError = error;
            }

            var result1 = network.Compute(new double[] {1, 0, 1, 0, 1, 0, 1, 0});
            Console.WriteLine($"2 + 2, 2 * 2 = {result1[0]}, {result1[1]}");
            var result2 = network.Compute(new double[] {0, 1, 0, 1, 1, 0, 0, 1});
            Console.WriteLine($"1 + 1, 2 * 1 = {result2[0]}, {result2[1]}");
            var result3 = network.Compute(new double[] {1, 0, 1, 0, 0, 1, 0, 0});
            Console.WriteLine($"2 + 2, 1 * 0 = {result3[0]}, {result3[1]}");
            var result4 = network.Compute(new double[] {0, 1, 0, 0, 0, 1, 1, 0});
            Console.WriteLine($"1 + 0, 1 * 2 = {result4[0]}, {result4[1]}");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Background worker for neural network learning
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorkerNeural_DoWork(object sender, DoWorkEventArgs e)
        {
            string connectionString = "Data Source=192.168.0.245;Initial Catalog=MyDB;Integrated Security=True; Connection Timeout=30000";// +
               using (SqlConnection connection = new SqlConnection(connectionString))
               {
               try
               {
                   connection.Open();
                   string queryString = "SELECT * FROM rates WHERE [i1]<>0 AND [i2]<>0 AND [i3]<>0 AND [i4]<>0 AND [i5]<>0 AND [i6]<>0 AND [i7]<>0 AND [i8]<>0" +
                                         " AND [i9]<>0 AND [i10]<>0 AND [i11]<>0 AND [i12]<>0 AND [i13]<>0 AND [i14]<>0 AND [i15]<>0 AND [i16]<>0" +
                                         " AND [i17]<>0 AND [i18]<>0 AND [i20]<>0 AND [i21]<>0 AND [i23]<>0";
                   SqlCommand command = new SqlCommand(queryString, connection);
                   SqlDataReader reader = command.ExecuteReader();

                   DataTable dt = new DataTable();
                   dt.Load(reader);
                   int value_count = dt.Rows.Count;
                   double[][] input_arr = new double[value_count][];
                   double[][] output_arr = new double[value_count][];
                   for (int j = 0; j < value_count; j++)
                   {
                       input_arr[j] = new double[21];
                       output_arr[j] = new double[1];
                   }
                   // Call Read before accessing data.

                   for (int i = 0; i < value_count; i++)
                   {
                       DataRow row = dt.Rows[i];
                       input_arr[i][0] = Double.Parse(row["i1"].ToString());
                       input_arr[i][1] = Double.Parse(row["i2"].ToString());
                       input_arr[i][2] = Double.Parse(row["i3"].ToString());
                       input_arr[i][3] = Double.Parse(row["i4"].ToString());
                       input_arr[i][4] = Double.Parse(row["i5"].ToString());
                       input_arr[i][5] = Double.Parse(row["i6"].ToString());
                       input_arr[i][6] = Double.Parse(row["i7"].ToString());
                       input_arr[i][7] = Double.Parse(row["i8"].ToString());
                       input_arr[i][8] = Double.Parse(row["i9"].ToString());
                       input_arr[i][9] = Double.Parse(row["i10"].ToString());
                       input_arr[i][10] = Double.Parse(row["i11"].ToString());
                       input_arr[i][11] = Double.Parse(row["i12"].ToString());
                       input_arr[i][12] = Double.Parse(row["i13"].ToString());
                       input_arr[i][13] = Double.Parse(row["i14"].ToString());
                       input_arr[i][14] = Double.Parse(row["i15"].ToString());
                       input_arr[i][15] = Double.Parse(row["i16"].ToString());
                       input_arr[i][16] = Double.Parse(row["i17"].ToString());
                       input_arr[i][17] = Double.Parse(row["i18"].ToString());
                       input_arr[i][18] = Double.Parse(row["i20"].ToString());
                       input_arr[i][19] = Double.Parse(row["i21"].ToString());
                       input_arr[i][20] = Double.Parse(row["i23"].ToString());
                       //output_arr[i][0] = Double.Parse(row["Difference"].ToString());
                       if (Double.Parse(row["Difference"].ToString()) > 0)
                           output_arr[i][0] = 1;
                       else if (Double.Parse(row["Difference"].ToString()) == 0)
                           output_arr[i][0] = 0;
                       else
                           output_arr[i][0] = -1;
                   }
                   int[] neurons = new int[5] { 21, 21, 21, 21, 1 };
                   AForge.Neuro.BipolarSigmoidFunction sigmoiddFunction = new AForge.Neuro.BipolarSigmoidFunction();
                   //AForge.Neuro.SigmoidFunction sigmoiddFunction = new AForge.Neuro.SigmoidFunction(2);
                   AForge.Neuro.ActivationNetwork network = new AForge.Neuro.ActivationNetwork(sigmoiddFunction, 21, 1);
                   AForge.Neuro.ActivationNetwork network1 = new AForge.Neuro.ActivationNetwork(sigmoiddFunction, 21, 1);//neurons);
                   //AForge.Neuro.Learning.DeltaRuleLearning teacher = new AForge.Neuro.Learning.DeltaRuleLearning(network) { LearningRate = 1};
                   AForge.Neuro.Learning.EvolutionaryLearning teacher = new AForge.Neuro.Learning.EvolutionaryLearning(network, 1000);
                   // AForge.Neuro.Learning.ResilientBackpropagationLearning teacher = new AForge.Neuro.Learning.ResilientBackpropagationLearning(network) { LearningRate = 1 };
                   //AForge.Neuro.Learning.PerceptronLearning teacherP = new PerceptronLearning(network1){ LearningRate =1};
                   //AForge.Neuro.Learning.BackPropagationLearning teacher = new AForge.Neuro.Learning.BackPropagationLearning(network) { LearningRate =1, Momentum = .2 };

                   // loop
                   bool noNeedToStop = false;
                   double error = 0;
                   //double error1 = 0;
                   double lastError = 0;
                   double learningRate = 1;
                   int k = 0;
                   sigmoiddFunction.Alpha = 0.01;
                   while (!noNeedToStop)
                   {
                       // run epoch of learning procedure
                       //error = teacher.RunEpoch(input_arr, output_arr);
                       //error = teacherP.RunEpoch(input_arr,output_arr);
                       error = teacher.RunEpoch(input_arr, output_arr);
                       double temp = Math.Abs(lastError - error);
                       if (error < 30)
                           noNeedToStop = true;
                       else if (temp < 0.0000001)
                       {
                           lastError = error;
                           k++;
                           if (k > 1000)
                           {
                               network.Randomize();
                               k = 0;
                           }
                           learningRate /= 2;

                           //if (learningRate < 0.001)
                           // {
                           //   learningRate = 0.001;
                           //network.Randomize();
                           // noNeedToStop = true;
                           // }
                       }
                       else
                           lastError = error;
                       // teacherP.LearningRate = learningRate;
                   }
                   network.Save(@"E:\\neural");

               }
               catch (Exception ex)
               {
                   message += " Exception: " + ex.Message;
               }
               finally
               {
                   connection.Close();
               }
               }
        }
Exemplo n.º 4
0
        //
        private ConfusionMatrix RunNN(Double[][] trainingSet, Double[][] trainingOutput, Double[][] testSet, int[] expected)
        {
            double alpha = 2.0;
            //ActivationNetwork network = new ActivationNetwork(new BipolarSigmoidFunction(alpha), 3, 3, 1);
            ActivationNetwork network = new ActivationNetwork(new SigmoidFunction(alpha), 2, 3, 1);
            ActivationNeuron neuron = network.Layers[1].Neurons[0] as ActivationNeuron;
            ActivationLayer layer = network.Layers[0] as ActivationLayer;

            EvolutionaryLearning teacher = new EvolutionaryLearning(network, 100);
            // ResilientBackpropagationLearning teacher = new ResilientBackpropagationLearning(network);
            // teacher.LearningRate = 0.01;
            //teacher.Momentum = 0.1;

            //Enrich the dimensions of the vectors, padding 1 to the end
            var richTraining = trainingSet;
            var richTesting = testSet;

            int epochs = 0;

            while (true)
            {
                double error = teacher.RunEpoch(richTraining, trainingOutput);// / trainingSet.Length;
                //++epochs;
                if (epochs == 200)
                {
                    epochs = 0;
                    network = new ActivationNetwork(new SigmoidFunction(alpha), 3, 3, 1);
                    teacher = new EvolutionaryLearning(network, 100);

                }
                if (error <= 2.5) break;
                //Console.Write("Iter: " + epochs + " " + error + "\n");

            }

            var predicted = richTesting
                   .Select(x => network.Compute(x))
                   .Select(x => Convert.ToInt32(Math.Round(x[0])))
                   .ToArray();

            List<Double[,]> classifiers = new List<Double[,]>();

            // Calculate the coordinates of the classifier
            double k = (neuron.Weights[1] != 0) ? (-neuron.Weights[0] / neuron.Weights[1]) : 0;
            double b = (neuron.Weights[1] != 0) ? (-((ActivationNeuron)neuron).Threshold / neuron.Weights[1]) : 0;

            // Create the line and feed it to the data series
            double[,] classifier = new double[2, 2]{
               { perceChart.RangeX.Min, perceChart.RangeX.Min * k + b},
               { perceChart.RangeX.Max, perceChart.RangeX.Max * k + b}
              };

            classifiers.Add(classifier);
            //For test, assume 0 as positive and 1 as negative
            int positive = 0;
            int negative = 1;

            //Create a confusion matrix with the calculated parameters
            ConfusionMatrix cmatrix = new ConfusionMatrix(predicted, expected, positive, negative);

            if (MostAccurateNN == null || cmatrix.Accuracy > MostAccurateNN.Item2.Accuracy)
                MostAccurateNN = Tuple.Create(classifiers, cmatrix);

            return cmatrix;
        }