示例#1
0
        public void CanBackPropagateTwoLayerNetGradient1()
        {
            Layer inputlayer = new Layer(new double[, ] {
                { 1, 1, 1 }, { 1, 1, 1 }
            });
            Layer outputlayer = new Layer(new double[, ] {
                { 1, 1 }
            });
            LayerChain network = new LayerChain();

            network.Add(inputlayer);
            network.Add(outputlayer);

            NetworkVector inputvector    = new NetworkVector(new double[] { 1, 0, 0 });
            NetworkVector outputgradient = new NetworkVector(new double[] { 1 });

            network.Run(inputvector);
            network.BackPropagate(outputgradient);

            LayerState inputState  = network.State[0];
            LayerState outputState = network.State[1];


            double[,] inputWeights      = inputState.Weights;
            double[,] inputWeightsCheck = new double[, ] {
                { 0, 1, 1 }, { 0, 1, 1 }
            };
            double[] inputBiases      = inputState.Biases;
            double[] inputBiasesCheck = new double[] { -1, -1 };
            for (int i = 0; i < inputWeights.GetLength(0); i++)
            {
                Assert.AreEqual(inputBiasesCheck[i], inputBiases[i]);

                for (int j = 0; j < inputWeights.GetLength(1); j++)
                {
                    Assert.AreEqual(inputWeights[i, j], inputWeights[i, j]);
                }
            }

            double[,] outputWeights      = outputState.Weights;
            double[,] outputWeightsCheck = new double[, ] {
                { 0, 0 }
            };
            double[] outputBiases      = outputState.Biases;
            double[] outputBiasesCheck = new double[] { -1 };
            for (int i = 0; i < outputWeights.GetLength(0); i++)
            {
                Assert.AreEqual(outputBiasesCheck[i], outputBiases[i]);
                for (int j = 0; j < outputWeights.GetLength(1); j++)
                {
                    Assert.AreEqual(outputWeightsCheck[i, j], outputWeights[i, j]);
                }
            }
        }
示例#2
0
        public void CanRunWithZeroInput()
        {
            Layer layer = new Layer(new double[, ] {
                { 1 }
            });
            LayerChain    layerlist = new LayerChain(layer);
            NetworkVector vector    = new NetworkVector(new double[] { 0 });

            layerlist.Run(vector);
            Assert.AreEqual(vector, layerlist.Output);
        }
示例#3
0
        public void CannotRunWithInputOfWrongSize()
        {
            Layer layer = new Layer(new double[, ] {
                { 1 }
            });
            LayerChain    layerlist = new LayerChain(layer);
            NetworkVector input     = new NetworkVector(new double[] { 0, 0 });

            try
            {
                layerlist.Run(input);
                Assert.Fail("Run should throw an ArgumentException for input of the wrong size, but did not.");
            }
            catch (ArgumentException)
            { }
        }
示例#4
0
        public void CanRunTwoLayersWithZeroInput()
        {
            Layer layer1 = new Layer(new double[, ] {
                { 1, 1 }, { 1, 1 }
            });
            Layer layer2 = new Layer(new double[, ] {
                { 1, 1 }
            });
            LayerChain layerlist = new LayerChain(layer1);

            layerlist.Add(layer2);
            NetworkVector vector = new NetworkVector(new double[] { 0, 0 });

            layerlist.Run(vector);
            Assert.AreEqual(0, layerlist.Output.ToArray()[0]);
        }
示例#5
0
        public void CanRunTwoLayerNetWithOneInput()
        {
            Layer inputlayer = new Layer(new double[, ] {
                { 1, 1, 1 }, { 1, 1, 1 }
            });
            Layer outputlayer = new Layer(new double[, ] {
                { 1, 1 }
            });
            LayerChain network = new LayerChain();

            network.Add(inputlayer);
            network.Add(outputlayer);

            NetworkVector inputvector = new NetworkVector(new double[] { 1, 0, 0 });

            network.Run(inputvector);

            double output = network.Output.ToArray()[0];

            Assert.AreEqual(2, output);
        }