Beispiel #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]);
                }
            }
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public void CannotAddNonLayer()
        {
            Layer layer = new Layer(new double[, ] {
                { 1 }
            });
            NetworkComponent layer_component = new Layer(new double[, ] {
                { 2 }
            });
            NetworkComponent non_layer_component = new SoftMaxUnit(1);

            LayerChain lChain = new LayerChain();

            lChain.Add(layer);
            lChain.Add(layer_component);

            try
            {
                lChain.Add(non_layer_component);
                Assert.Fail("Attempt to add a network component that cannot be cast as Layer failed to throw an ArgumentException.");
            }
            catch (ArgumentException) { }
        }
Beispiel #4
0
        public void CanCountLayers()
        {
            Layer layer = new Layer(new double[, ] {
                { 1 }
            });

            LayerChain layerlist = new LayerChain();

            Assert.AreEqual(0, layerlist.NumberOfComponents);

            for (int i = 1; i < 10; i++)
            {
                layerlist.Add(layer);
                Assert.AreEqual(i, layerlist.NumberOfComponents);
            }
        }
Beispiel #5
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]);
        }
Beispiel #6
0
        public void CannotAddLayerOfWrongSize()
        {
            Layer layer1 = new Layer(new double[, ] {
                { 1 }
            });
            Layer layer2 = new Layer(new double[, ] {
                { 1, 2 }
            });
            LayerChain layerlist = new LayerChain(layer1);

            try
            {
                layerlist.Add(layer2);
                Assert.Fail("Add should throw and ArgumentException if when trying to add a layer of the wrong size, but did not.");
            }
            catch (ArgumentException)
            { }

            Assert.AreNotEqual(null, layerlist);
        }