Esempio n. 1
0
        public void CanBPWC_trivialBatch()
        {
            NetworkMatrix weights = new NetworkMatrix(new double[, ] {
                { 1 }
            });
            NetworkVector         biases         = new NetworkVector(new double[] { 10 });
            BatchWeightedCombiner wc             = new BatchWeightedCombiner(weights, biases);
            NetworkVector         input          = new NetworkVector(new double[] { 1 });
            NetworkVector         outputgradient = new NetworkVector(new double[] { 1 });

            wc.StartBatch();
            wc.Run(input);
            wc.BackPropagate(outputgradient);
            wc.EndBatchAndUpdate();

            NetworkVector outputCheck        = new NetworkVector(new double[] { 11 });
            NetworkVector inputGradientCheck = new NetworkVector(new double[] { 1 });

            double[,] weightsCheck = new double[, ] {
                { 0 }
            };
            double[] biasesCheck = new double[] { 9 };
            Assert.AreEqual(outputCheck, wc.Output);
            Assert.AreEqual(inputGradientCheck, wc.InputGradient);

            for (int i = 0; i < wc.NumberOfOutputs; i++)
            {
                Assert.AreEqual(biasesCheck[i], wc.State.Biases[i]);

                for (int j = 0; j < wc.NumberOfInputs; j++)
                {
                    Assert.AreEqual(weightsCheck[i, j], wc.State.Weights[i, j]);
                }
            }
        }
Esempio n. 2
0
        public void CanBPWC2x3_nonTrivialBatch()
        {
            NetworkMatrix weights = new NetworkMatrix(new double[, ] {
                { 1, 2, 3 }, { 5, 7, 11 }
            });
            NetworkVector         biases         = new NetworkVector(new double[] { 100, 200 });
            BatchWeightedCombiner wc             = new BatchWeightedCombiner(weights, biases);
            NetworkVector         input          = new NetworkVector(new double[] { 1, 2, 3 });
            NetworkVector         outputgradient = new NetworkVector(new double[] { 1, 1 });

            wc.StartBatch();
            for (int i = 0; i < 2; i++)
            {
                wc.Run(input);
                wc.BackPropagate(outputgradient);
            }
            wc.EndBatchAndUpdate();

            NetworkVector outputCheck        = new NetworkVector(new double[] { 114, 252 });
            NetworkVector inputGradientCheck = new NetworkVector(new double[] { 6, 9, 14 });

            double[,] weightsCheck = new double[, ] {
                { -1, -2, -3 }, { 3, 3, 5 }
            };
            double[] biasesCheck = new double[] { 98, 198 };
            Assert.AreEqual(outputCheck, wc.Output);
            Assert.AreEqual(inputGradientCheck, wc.InputGradient);

            for (int i = 0; i < wc.NumberOfOutputs; i++)
            {
                Assert.AreEqual(biasesCheck[i], wc.State.Biases[i]);

                for (int j = 0; j < wc.NumberOfInputs; j++)
                {
                    Assert.AreEqual(weightsCheck[i, j], wc.State.Weights[i, j]);
                }
            }
        }