Beispiel #1
0
        public void TestBias()
        {
            double input = 0.3;
            double inpToOutWeight = 0.4;
            double biasToOutWeight = 0.5;

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var bias = new Bias();
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(inpToOutWeight, inputNode, outputNode);
            Connection.Create(biasToOutWeight, bias, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.Nodes[1][0].Output;

            //Output
            // = Sig(inpToOut.Output + biasToOut.Output)
            // = Sig((inpToOut.Weight * inpToOut.GetInput()) + (biasToOutWeight * 1))
            // = Sig((inpToOut.Weight * inputNode.Output) + (biasToOutWeight))
            // = Sig((this.weight * this.input) + (biasToOutWeight))
            var expOut = sigmoid.Calculate((inpToOutWeight * input) + biasToOutWeight);

            Assert.AreEqual(nwOut, expOut);
        }
Beispiel #2
0
        public void TestConnection()
        {
            double weight = 2;
            double input = 5;
            var inp = new Input(input);
            var outp = new Perceptron(new SigmoidFunction(), "Output");
            var wc = Connection.Create(weight, inp, outp);

            Assert.AreEqual(wc.Output, weight * input);
            Assert.AreEqual(inp.GetOutgoingConnections()[0], outp.GetIncommingConnections()[0]);
        }
Beispiel #3
0
        public void TestDelConnection()
        {
            var inp = new Input(5);
            var outp = new Perceptron(new SigmoidFunction());

            var beforeConnect = outp.Output;

            var con = Connection.Create(1, inp, outp);

            outp.ResetCache();
            var afterConnect = outp.Output;

            Assert.AreNotEqual(beforeConnect, afterConnect);

            con.Delete();
            outp.ResetCache();

            Assert.AreEqual(beforeConnect, outp.Output);
        }
Beispiel #4
0
        public void TestPerceptronCaching()
        {
            double local1 = -10;
            double local2 = -5;

            var in1 = new Input(local1, "Input1");
            var in2 = new Input(local2, "Input2");
            var p = new Perceptron(new SigmoidFunction());

            Connection.Create(0.5, in1, p);
            Connection.Create(1, in2, p);

            Assert.IsTrue(p.Output < 0.1);

            in1.Value = 10;
            in2.Value = 5;

            Assert.IsTrue(p.Output < 0.1);

            p.ResetCache();

            Assert.IsFalse(p.Output < 0.1);
        }
Beispiel #5
0
        public void TestNetworkOutput()
        {
            var rand = new Random();
            double input = (double)(rand.NextDouble());
            double weight = (double)(rand.NextDouble());

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(weight, inputNode, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.CurOutput()[0];
            Assert.AreEqual(outputNode.Output, nwOut);

            var expOut = sigmoid.Calculate(input * weight);

            Assert.AreEqual(nwOut, expOut);
        }
Beispiel #6
0
        public void TestGetInputResult()
        {
            var rand = new Random();
            double input = (double)(rand.NextDouble() * 100 + 1);
            double weight = (double)(rand.NextDouble() * 2);

            var sigmoid = new SigmoidFunction();
            var nw = new Network(sigmoid, false);

            var inputNode = new Input(input);
            var outputNode = new Perceptron(sigmoid, "Output");
            Connection.Create(weight, inputNode, outputNode);

            nw.Nodes = new Node[][] {
                new Node[] { inputNode },
                new Node[] { outputNode }
            };

            var nwOut = nw.GetInputResult(input)[0];

            //Output
            // = Sig(inpToOut.Output)
            // = Sig(inpToOut.Weight * inpToOut.GetInput())
            // = Sig(inpToOut.Weight * inputNode.Output)
            // = Sig(this.weight * this.input)
            var expOut = sigmoid.Calculate(weight * input);

            Assert.AreEqual(nwOut, expOut);
        }