public void Backwardがただしい()
        {
            var x = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 2, 3 }
            }));
            var y = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 4, -1, 0.1f }
            }));
            var result = Multiply.ForwardStatic(
                x, y
                );
            var target = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 0.5f, 0.5f, 0.5f }
            }));
            var loss = MeanSquaredError.ForwardStatic(
                result,
                target
                );

            UnityEngine.Debug.Log(loss.Value);
            loss.Backward();
            UnityEngine.Debug.Log(result.Grad);
            Helper.AssertMatrixAlmostEqual(
                x.Grad,
                Matrix <float> .Build.DenseOfArray(new float[, ]
            {
                { 9.33333397f, 1.66666675f, -0.01333333f }
            })                                                  // chainer-python
                );
        }
        public void 簡単なoptimizeしてみる()
        {
            var x = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 1, 1 }
            }));
            var constant = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 1, 1 }
            }));
            var target = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 2, 3 }
            }));

            const float lr = 0.1f;

            var convergence = false;

            for (var i = 0; i < 100; i++)
            {
                var loss = MeanSquaredError.ForwardStatic(
                    Add.ForwardStatic(x, constant),
                    target
                    );

                x.Grad = null;
                loss.Backward();
                x.Value -= x.Grad * lr;
                if (loss.Value[0, 0] < 0.1f)
                {
                    convergence = true;
                    break;
                }
            }
            Assert.IsTrue(convergence);
        }
Exemple #3
0
        public void Backwardの式が正しい()
        {
            var x = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 2, 3 }
            }));
            var W = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ]
            {
                { 3, 4, 3 },
                { 1, 2, 3 },
                { 5, 4, 1 },
            }));
            var b = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 4, 2, 1 }
            }));
            var target = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 0.5f, 0.5f, 0.5f }
            }));
            var loss = MeanSquaredError.ForwardStatic(
                Linear.ForwardStatic(x, W, b),
                target
                );

            loss.Backward();
            chainer.Helper.AssertMatrixAlmostEqual(
                x.Grad,
                Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 112.33333588f, 127.33333588f, 89f }
            })
                );
        }
        public void ForwardBackwardで形が変わらない()
        {
            var func = new MeanSquaredError();
            var x0   = new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 1, 1 }
            }));

            var loss = func.Forward(
                x0,
                new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 2, 3 }
            }))
                );

            loss.CurrentGrad = loss.Value;
            var gxs = func.Backward(loss).ToList();

            Assert.AreEqual(gxs[0].ColumnCount, x0.Value.ColumnCount);
            Assert.AreEqual(gxs[0].RowCount, x0.Value.RowCount);
        }
        public void Forwardできる()
        {
            var func = new MeanSquaredError();
            var loss = func.Forward(
                new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 1, 1 }
            })),
                new Variable(Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1, 2, 3 }
            }))
                );

            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                Matrix <float> .Build.DenseOfArray(new float[, ] {
                { 1.6666666269302368f }
            }),
                delta: 0.00001f
                );
        }
        public void Backardの式が正しい()
        {
            var testInput = new Variable(DenseMatrix.OfArray(new float[, ] {
                { 1, 2, 3 }
            }));
            var target = new Variable(DenseMatrix.OfArray(new float[, ] {
                { 0.5f, 0.5f, 0.5f }
            }));
            var loss = MeanSquaredError.ForwardStatic(
                ReLU.ForwardStatic(testInput),
                target
                );

            loss.Backward();
            chainer.Helper.AssertMatrixAlmostEqual(
                DenseMatrix.OfArray(new float[, ] {
                { 0.33333334f, 1f, 1.66666675f }
            }),                                                                     // by chainer.py
                testInput.Grad
                );
        }
Exemple #7
0
        public void Backardの式が正しい()
        {
            var testInput = new Variable(DenseMatrix.OfArray(new float[, ] {
                { 1, 2, 3 }
            }));
            var target = new Variable(DenseMatrix.OfArray(new float[, ] {
                { 0.5f, 0.5f, 0.5f }
            }));
            var loss = MeanSquaredError.ForwardStatic(
                Sigmoid.ForwardStatic(testInput),
                target
                );

            loss.Backward();
            chainer.Helper.AssertMatrixAlmostEqual(
                testInput.Grad,
                DenseMatrix.OfArray(new float[, ] {
                { 0.03028592f, 0.02665417f, 0.01363052f }
            })                                                                              // by chainer.py
                );
        }