AForge.Neuro.Learning.EvolutionaryLearning.RunEpoch C# (CSharp) Метод

RunEpoch() публичный Метод

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.
Результат double
        public double RunEpoch( double[][] input, double[][] output )
        {
            Debug.Assert( input.Length > 0 );
            Debug.Assert( output.Length > 0 );
            Debug.Assert( input.Length == output.Length );
            Debug.Assert( network.InputsCount == input.Length );

            // check if it is a first run and create population if so
            if ( population == null )
            {
                // sample chromosome
                DoubleArrayChromosome chromosomeExample = new DoubleArrayChromosome(
                    chromosomeGenerator, mutationMultiplierGenerator, mutationAdditionGenerator,
                    numberOfNetworksWeights );

                // create population ...
                population = new Population( populationSize, chromosomeExample,
                    new EvolutionaryFitness( network, input, output ), selectionMethod );
                // ... and configure it
                population.CrossoverRate = crossOverRate;
                population.MutationRate = mutationRate;
                population.RandomSelectionPortion = randomSelectionRate;
            }

            // run genetic epoch
            population.RunEpoch( );

            // get best chromosome of the population
            DoubleArrayChromosome chromosome = (DoubleArrayChromosome) population.BestChromosome;
            double[] chromosomeGenes = chromosome.Value;

            // put best chromosome's value into neural network's weights
            int v = 0;

            for ( int i = 0; i < network.Layers.Length; i++ )
            {
                Layer layer = network.Layers[i];

                for ( int j = 0; j < layer.Neurons.Length; j++ )
                {
                    ActivationNeuron neuron = layer.Neurons[j] as ActivationNeuron;

                    for ( int k = 0; k < neuron.Weights.Length; k++ )
                    {
                        neuron.Weights[k] = chromosomeGenes[v++];
                    }
                    neuron.Threshold = chromosomeGenes[v++];
                }
            }

            Debug.Assert( v == numberOfNetworksWeights );

            return 1.0 / chromosome.Fitness;
        }
    }

Usage Example

        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}.");
        }
All Usage Examples Of AForge.Neuro.Learning.EvolutionaryLearning::RunEpoch