Example #1
0
        public void ForwardBackwardTest1()
        {
            Shape       shape = new Shape(Shape.BWHC, -1, 3, 2, 4);
            MaxOutLayer layer = new MaxOutLayer(shape, 2);

            Tensor xTemp = new Tensor(null, shape.Reshape(Axis.B, 1));

            xTemp.Set(new float[]
            {
                11, 12, 13, 14, 12, -11, 15, 16,
                21, 22, 23, 24, 22, -21, 25, 26,
                31, 32, 33, 34, 32, -31, 35, 36,
            });

            Tensor expectedTemp = new Tensor(null, layer.OutputShape.Reshape(Axis.B, 1));

            expectedTemp.Set(new float[]
            {
                12, 14, 12, 16,
                22, 24, 22, 26,
                32, 34, 32, 36,
            });

            Tensor dyTemp = new Tensor(null, expectedTemp.Shape);

            dyTemp.Set(new float[]
            {
                12, 14, 12, 16,
                22, 24, 22, 26,
                32, 34, 32, 36,
            });

            Tensor expectedDxTemp = new Tensor(null, xTemp.Shape);

            expectedDxTemp.Set(new float[]
            {
                0, 12, 0, 14, 12, 0, 0, 16,
                0, 22, 0, 24, 22, 0, 0, 26,
                0, 32, 0, 34, 32, 0, 0, 36,
            });

            for (int i = 1; i <= 3; i++)
            {
                Session session = new Session();

                Tensor x = session.Tile(xTemp, (int)Axis.B, i);
                Tensor y = layer.Forward(session, new[] { x })[0];

                Tensor expected = session.Tile(expectedTemp, (int)Axis.B, i);
                Helpers.AreTensorsEqual(expected, y);

                // unroll the graph
                y.SetGradient(session.Tile(dyTemp, (int)Axis.B, i).Weights);
                session.Unroll();

                Tensor expectedDx = session.Tile(expectedDxTemp, (int)Axis.B, i);
                Helpers.AreArraysEqual(expectedDx.Length, expectedDx.Weights, x.Gradient);
            }
        }