示例#1
0
        /// <summary>
        /// The constructor
        /// </summary>
        public NNSim()
        {
            this.xor_template = new TrainingTemplate();
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                0
            }));

            this.input1Neuron = new Neuron("input1", 0, 1);
            this.input2Neuron = new Neuron("input2", 0, 1);

            this.hidden1Neuron = new Neuron("hidden1", 0, 1);
            this.hidden2Neuron = new Neuron("hidden2", 0, 1);

            this.outputNeuron = new Neuron("output", 0, 1);

            input1Neuron.fireRule = new SigmoidFireRule();
            input2Neuron.fireRule = new SigmoidFireRule();

            hidden1Neuron.fireRule = new SigmoidFireRule();
            hidden1Neuron.fireRule = new SigmoidFireRule();

            outputNeuron.fireRule = new SigmoidFireRule();

            input1Neuron.addNewInput("network_in_1", 1, 1);
            input2Neuron.addNewInput("network_in_2", 1, 1);

            hidden1Neuron.inputs.Add(input1Neuron.output);
            hidden1Neuron.inputs.Add(input2Neuron.output);

            hidden2Neuron.inputs.Add(input1Neuron.output);
            hidden2Neuron.inputs.Add(input2Neuron.output);

            outputNeuron.inputs.Add(hidden1Neuron.output);
            outputNeuron.inputs.Add(hidden2Neuron.output);

            errors = new double[4];
        }
示例#2
0
        private void button1_Click(object sender, EventArgs e)
        {
            neuron = new Neuron("AND-Neuron", 0, 2);
            neuron.addNewInput("input1", 0, 0);
            neuron.addNewInput("input2", 0, 0);
            PerceptronNetwork pn = new PerceptronNetwork(neuron);

            TrainingTemplate andTemplate = new TrainingTemplate("AND Template");

            andTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            andTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                0
            }));
            andTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                0
            }));
            andTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                1
            }));

            TrainingTemplate orTemplate = new TrainingTemplate("OR Template");

            orTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            orTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                1
            }));
            orTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                1
            }));
            orTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                1
            }));

            TrainingTemplate xorTemplate = new TrainingTemplate("XOR Template");

            xorTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            xorTemplate.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                1
            }));
            xorTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                1
            }));
            xorTemplate.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                0
            }));


            templatesList = new List <TrainingTemplate>();

            ErrorHistory errorProg = new ErrorHistory();

            double error = pn.train(xorTemplate, 100, errorProg);

            labelWeight1.Text = neuron.inputs[0].weight.ToString("N3");

            labelWeight2.Text = neuron.inputs[1].weight.ToString("N3");

            labelError.Text = error.ToString("N3");



            for (int X = 0; X < errorProg.errorPoints.Count; X++)
            {
                chart1.Series["Error"].Points.AddXY(X, errorProg.errorPoints[X]);
            }

            //chart1.DataBind(errorProg);
        }
示例#3
0
        static void Main(string[] args)
        {
            //We create the training template

            TrainingTemplate xor_template = new TrainingTemplate();

            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 0
            }, new List <double> {
                0
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                0, 1
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 0
            }, new List <double> {
                1
            }));
            xor_template.addTrainingRow(new TrainingRow(new List <double> {
                1, 1
            }, new List <double> {
                0
            }));

            //We create the network
            SimpleNetwork sn = new SimpleNetwork();

            // We create the neurons
            Neuron input1Neuron = new Neuron("input1", 0, 1);
            Neuron input2Neuron = new Neuron("input2", 0, 1);

            Neuron hidden1Neuron = new Neuron("hidden1", 0, 1);
            Neuron hidden2Neuron = new Neuron("hidden2", 0, 1);

            Neuron outputNeuron = new Neuron("output", 0, 1);

            //We asign them Sigmoid fire functions

            input1Neuron.fireRule = new SigmoidFireRule();
            input2Neuron.fireRule = new SigmoidFireRule();

            hidden1Neuron.fireRule = new SigmoidFireRule();
            hidden1Neuron.fireRule = new SigmoidFireRule();

            outputNeuron.fireRule = new SigmoidFireRule();

            // We bind them together

            input1Neuron.addNewInput("network_in_1", 1, 1);
            input2Neuron.addNewInput("network_in_2", 1, 1);

            hidden1Neuron.inputs.Add(input1Neuron.output);
            hidden1Neuron.inputs.Add(input2Neuron.output);

            hidden2Neuron.inputs.Add(input1Neuron.output);
            hidden2Neuron.inputs.Add(input2Neuron.output);

            outputNeuron.inputs.Add(hidden1Neuron.output);

            // We put them into layers

            sn.inputLayer.neurons.Add(input1Neuron);
            sn.inputLayer.neurons.Add(input2Neuron);

            sn.hiddenLayer.neurons.Add(hidden1Neuron);
            sn.hiddenLayer.neurons.Add(hidden2Neuron);

            sn.outputLayer.neurons.Add(outputNeuron);

            // We train

            double error = sn.train(xor_template, 5000, new ErrorHistory());

            Console.WriteLine(error);
            Console.ReadKey();
        }