コード例 #1
0
        private void Run()
        {
            GA ga = new GA();

            double[][] input = new double[4][];
            input[0] = new double[] { 1, 1 };
            input[1] = new double[] { 0, 1 };
            input[2] = new double[] { 1, 0 };
            input[3] = new double[] { 0, 0 };

            double[][] output = new double[4][];
            output[0] = new double[] { 0 };
            output[1] = new double[] { 1 };
            output[2] = new double[] { 1 };
            output[3] = new double[] { 0 };
            List <NeuralNet> result = ga.DetermineFitness(ga.SpawnGeneration(1000000, 2, 0, 1), input, output);

            result = ga.SelectParents(result);
            for (int i = 0; i < 2; i++)
            {
                net = result[i];
                PrintOut(0, 1);
                PrintOut(1, 0);
                PrintOut(0, 0);
                PrintOut(1, 1);
            }

            /* Random random = new Random();
             * int seed = 0;
             *
             * double[][] input = new double[4][];
             * input[0] = new double[] { 1, 1 };
             * input[1] = new double[] { 0, 1 };
             * input[2] = new double[] { 1, 0 };
             * input[3] = new double[] { 0, 0 };
             *
             * double[][] output = new double[4][];
             * output[0] = new double[] { 0 };
             * output[1] = new double[] { 1 };
             * output[2] = new double[] { 1 };
             * output[3] = new double[] { 0 };
             *
             * net.Init(3, 2, 4, 1);
             * net.Train(input, output, 7, 8);
             *
             * PrintOut(0, 1);
             * PrintOut(1, 0);
             * PrintOut(0, 0);
             * PrintOut(1, 1);*/

            Console.Read();
        }
コード例 #2
0
        public void CalculateErrors(NeuralNet neuralNet, double[] expectedOutput)
        {
            //Calculate outputlayer errors
            for (int i = 0; i < neuralNet.outputLayer.neurons.Count; i++)
            {
                double temp = neuralNet.outputLayer.neurons[i].Output;
                neuralNet.outputLayer.neurons[i].error = (expectedOutput[i] - temp) * temp * (1.0F - temp);
            }

            //Calculate hiddenlayer errors
            for (int i = 0; i < neuralNet.hiddenLayer.neurons.Count; i++)
            {
                double error = 0;
                Neuron temp  = neuralNet.hiddenLayer.neurons[i];
                for (int a = 0; a < neuralNet.outputLayer.neurons.Count; a++)
                {
                    Neuron outputNode = neuralNet.outputLayer.neurons[a];

                    error += outputNode.error * outputNode.Input[i].Weight * temp.Output * (1.0 - temp.Output);
                }
                neuralNet.hiddenLayer.neurons[i].error = error;
            }
        }
コード例 #3
0
        private void AdjuestNet(NeuralNet neuralNet, double learningrate, double[] expected)
        {
            for (int i = 0; i < neuralNet.hiddenLayer.neurons.Count; i++)
            {
                Neuron node = neuralNet.hiddenLayer.neurons[i];

                for (int i2 = 0; i2 < neuralNet.outputLayer.neurons.Count; i2++)
                {
                    Neuron output   = neuralNet.outputLayer.neurons[i2];
                    double momentum = 0;
                    for (int i3 = 0; i3 < expected.Count(); i3++)
                    {
                        momentum = Math.Abs(expected[i3] - output.Output);
                    }
                    output.Input[i].Weight += momentum * learningrate * output.error * node.Output;
                    output.delta           += momentum * learningrate * output.error * output.weight;
                }
            }

            for (int i = 0; i < neuralNet.inputLayer.neurons.Count; i++)
            {
                Neuron node = neuralNet.inputLayer.neurons[i];

                for (int i2 = 0; i2 < neuralNet.hiddenLayer.neurons.Count; i2++)
                {
                    Neuron output   = neuralNet.hiddenLayer.neurons[i2];
                    double momentum = 0;
                    for (int i3 = 0; i3 < expected.Count(); i3++)
                    {
                        momentum = Math.Abs(expected[i3] - neuralNet.outputLayer.neurons[i3].Output);
                    }
                    output.Input[i].Weight += momentum * learningrate * output.error * node.Output;
                    output.delta           += momentum * learningrate * output.error * output.weight;
                }
            }
        }