コード例 #1
0
        public void backward()
        {
            int inSize, outSize;
            var countExamples = 3;

            inSize = outSize = 5;

            var input         = MatrixD.Build.Random(countExamples, inSize);
            var layer         = new BiasLayer(inSize);
            var nextGradients = MatrixD.Build.Random(countExamples, outSize);

            Assert.IsTrue(layer.backward(input, nextGradients).EEquals(nextGradients));
        }
コード例 #2
0
        public void forward()
        {
            var countExamples = 3;
            var inSize        = 5;
            var input         = MatrixD.Build.Random(countExamples, inSize);

            var layer           = new BiasLayer(inSize);
            var expectedOutputs = MatrixD.Build.repeat(countExamples, inSize, 0);

            for (var i = 0; i < countExamples; i++)
            {
                for (var j = 0; j < inSize; j++)
                {
                    expectedOutputs[i, j] = input[i, j] + layer.Biases[j];
                }
            }

            Assert.IsTrue(layer.forward(input).EEquals(expectedOutputs));
        }
コード例 #3
0
        public void backwardLearn()
        {
            var learnRate = GlobalRandom.NextDouble();
            int inSize, outSize;
            var countExamples = 3;

            inSize = outSize = 5;

            var input         = MatrixD.Build.Random(countExamples, inSize);
            var layer         = new BiasLayer(inSize);
            var nextGradients = MatrixD.Build.Random(countExamples, outSize);

            var expectedBiases = layer.Biases.ShallowCopy();
            var gradientSums   = nextGradients.ColumnSums().AsArray();

            for (int i = 0; i < expectedBiases.Length; i++)
            {
                expectedBiases[i] -= learnRate * gradientSums[i];
            }

            layer.backwardLearn(input, nextGradients, learnRate);
            Assert.IsTrue(expectedBiases.EEquals(layer.Biases));
        }