コード例 #1
0
        private void UnitTest1()
        {
            OutputText.Text = "";
            NetworkDocument doc = new NetworkDocument();
            InputNeuron i1 = new InputNeuron() {OutputValue = 0.7};
            InputNeuron i2 = new InputNeuron() {OutputValue = 0.1};
            InputNeuron i3 = new InputNeuron() {OutputValue = 0.3};

            doc.Inputs.Add(i1);
            doc.Inputs.Add(i2);
            doc.Inputs.Add(i3);

            OutputNeuron o = new OutputNeuron();
            doc.Outputs.Add(new OutputNeuron());

            NeuronHelper.ConnectNodes(i1, o, -0.3);
            NeuronHelper.ConnectNodes(i2, o, 3.1);
            NeuronHelper.ConnectNodes(i3, o, 0.5);
            double expected = 0.25;
            double answer = o.SumInputs(true);

            OutputText.Text += "Summation test 3 inputs values 0.7, 0.1,0.3 respective weights 0.3,3.1,0.5" +
                               Environment.NewLine;
            OutputText.Text += "Expected = " + expected + Environment.NewLine;
            OutputText.Text += "Actual = " + answer + Environment.NewLine;
        }
コード例 #2
0
        private void UnitTest3()
        {
            double output1;
            double output2;
            OutputText.Text = "";
            NetworkDocument doc = new NetworkDocument() {Bias = -2.0,Momentum = 0.05,LearningRate = 0.5};
            doc.ActivationFunction = Enumerations.ActivationFunctions.ActivationSigmoidal;
            doc.UsesBias = true;
            InputNeuron i0 = new InputNeuron() { OutputValue = 0.1 };
            InputNeuron i1 = new InputNeuron() { OutputValue = 0.9 };
            doc.Inputs.Add(i0);
            doc.Inputs.Add(i1);
            doc.NumLayers = 2;//this should build the layers
            HiddenNeuron l00 = new HiddenNeuron(){BiasValue = 2.0};
            HiddenNeuron l01 = new HiddenNeuron() { BiasValue = 2.0 };
            HiddenNeuron l10 = new HiddenNeuron() { BiasValue = 3.0 };
            HiddenNeuron l11 = new HiddenNeuron() { BiasValue = -2.0 };
            doc.Hidden[0].Add(l00);
            doc.Hidden[0].Add(l01);
            doc.Hidden[1].Add(l10);
            doc.Hidden[1].Add(l11);
            OutputNeuron o = new OutputNeuron() {ExpectedValue = 0.9,BiasValue = -2};
            doc.Outputs.Add(o);
            //connect layer 1
            NeuronHelper.ConnectNodes(i0, l00, -2);
            NeuronHelper.ConnectNodes(i0, l01, 3);
            NeuronHelper.ConnectNodes(i1, l00, -2);
            NeuronHelper.ConnectNodes(i1, l01, 3);
            //connect layer 2
            NeuronHelper.ConnectNodes(l00, l10, -2);
            NeuronHelper.ConnectNodes(l00, l11, 2);
            NeuronHelper.ConnectNodes(l01, l10, -4);
            NeuronHelper.ConnectNodes(l01, l11, 2);
            //output layer
            NeuronHelper.ConnectNodes(l10, o, 3);
            NeuronHelper.ConnectNodes(l11, o, 1);

            output1 = l00.SumInputs(true);
            output2 = l01.SumInputs(true);
            WriteOutput(0, output1);
            WriteOutput(5, output2);

            output1 = l00.CalcOutput(doc);
            output2 = l01.CalcOutput(doc);
            WriteOutput(0.5, output1);
            WriteOutput(0.99, output2);

            output1 = l10.SumInputs(doc.UsesBias);
            output2 = l11.SumInputs(doc.UsesBias);
            WriteOutput(-1.972, output1);
            WriteOutput(0.98, output2);

            output1 = l10.CalcOutput(doc);
            output2 = l11.CalcOutput(doc);
            WriteOutput(0.122, output1);
            WriteOutput(0.728, output2);

            output1 = doc.Outputs[0].SumInputs(doc.UsesBias);
            output2 = doc.Outputs[0].CalcOutput(doc);
            WriteOutput(-0.906, output1);
            WriteOutput(0.288, output2);

            //backward pass
            output1 = doc.Outputs[0].CalcError();
            WriteOutput(0.125, output1);

            output1 = l10.CalcError();
            WriteOutput(0.040, output1);
            output1 = l11.CalcError();
            WriteOutput(0.025, output1);

            output1 = l00.CalcError();
            WriteOutput(0, output1);
            output1 = l01.CalcError();
            WriteOutput(0, output1);

            o.AdjustFromWeights(doc);
            l10.AdjustFromWeights(doc);
            l11.AdjustFromWeights(doc);
            l00.AdjustFromWeights(doc);
            l01.AdjustFromWeights(doc);
        }