Beispiel #1
0
        public void 合流のある計算グラフでもpythonと同じBakcwardになる()
        {
            var chain     = new helper.models.VerySmallChain();
            var optimizer = new chainer.optimizers.Adam();
            var input1    = new Variable(builder.DenseOfArray(new float[, ] {
                { 4, 3, 2 }
            }));
            var input2 = new Variable(builder.DenseOfArray(new float[, ] {
                { 10, 11, 12 }
            }));
            var target = new Variable(builder.DenseOfArray(new float[, ] {
                { 100 }
            }));

            optimizer.Setup(chain);
            Helper.AssertMatrixAlmostEqual(chain.fc._Params["W"].Value, builder.DenseOfArray(new float[, ] {
                { -1, 0, 1 }
            }));
            Helper.AssertMatrixAlmostEqual(chain.fc._Params["b"].Value, builder.DenseOfArray(new float[, ] {
                { 1 }
            }));

            var loss = chainer.functions.MeanSquaredError.ForwardStatic(
                chain.Forward(input1),
                target
                ) + chainer.functions.MeanSquaredError.ForwardStatic(
                chain.Forward(input1),
                target
                ) + chainer.functions.MeanSquaredError.ForwardStatic(
                chain.Forward(input2),
                target
                );

            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                builder.DenseOfArray(new float[, ] {
                { 29811.0f }
            }),
                delta: 0.01f
                );
            loss.Backward();
            Helper.AssertMatrixAlmostEqual(
                chain.fc._Params["W"].Grad,
                builder.DenseOfArray(new float[, ] {
                { -3556f, -3346f, -3136f }
            }),
                delta: 0.01f
                );
        }
Beispiel #2
0
        public void chainer_pythonと同じ値になる()
        {
            var chain     = new VerySmallChain();
            var optimizer = new chainer.optimizers.Adam();
            var input     = new Variable(builder.DenseOfArray(new float[, ] {
                { 4, 3, 2 }
            }));
            var target = new Variable(builder.DenseOfArray(new float[, ] {
                { 100 }
            }));

            optimizer.Setup(chain);
            Helper.AssertMatrixAlmostEqual(chain.fc._Params["W"].Value, builder.DenseOfArray(new float[, ] {
                { -1, 0, 1 }
            }));
            Helper.AssertMatrixAlmostEqual(chain.fc._Params["b"].Value, builder.DenseOfArray(new float[, ] {
                { 1 }
            }));

            var loss = MeanSquaredError.ForwardStatic(
                chain.Forward(input),
                target
                );

            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                builder.DenseOfArray(new float[, ] {
                { 10201 }
            }),
                delta: 0.01f
                );
            optimizer.ZeroGrads();
            loss.Backward();
            optimizer.Update();

            loss = MeanSquaredError.ForwardStatic(
                chain.Forward(input),
                target
                );
            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                builder.DenseOfArray(new float[, ] {
                { 10198.9794921875f }
            }),
                delta: 0.01f
                );
            optimizer.ZeroGrads();
            loss.Backward();
            optimizer.Update();

            loss = MeanSquaredError.ForwardStatic(
                chain.Forward(input),
                target
                );
            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                builder.DenseOfArray(new float[, ] {
                { 10196.9609375f }
            }),
                delta: 0.01f
                );

            for (int i = 0; i < 100; i++)
            {
                loss = MeanSquaredError.ForwardStatic(
                    chain.Forward(input),
                    target
                    );
                optimizer.ZeroGrads();
                loss.Backward();
                optimizer.Update();
            }

            loss = MeanSquaredError.ForwardStatic(
                chain.Forward(input),
                target
                );
            Helper.AssertMatrixAlmostEqual(
                loss.Value,
                builder.DenseOfArray(new float[, ] {
                { 9996.3515625f }
            }),
                delta: 0.01f
                );
        }