public void AveragePoolingLayer_Forward()
        {
            var bottom = new Tensor(1, 1, 3, 3);

            var filler = new ConstantFiller(2.0d);
            filler.Fill(bottom);

            var layer = new AveragePoolingLayer(3, 1, 1);
            layer.Setup(bottom, top);

            Assert.Equal(1, top.Num);
            Assert.Equal(1, top.Channels);
            Assert.Equal(3, top.Height);
            Assert.Equal(3, top.Width);

            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
            {
                var topData = topCpu.Data;
                AssertInRange(8.0d / 9, topData[0]);
                AssertInRange(4.0d / 3, topData[1]);
                AssertInRange(8.0d / 9, topData[2]);
                AssertInRange(4.0d / 3, topData[3]);
                AssertInRange(2.0d, topData[4]);
                AssertInRange(4.0d / 3, topData[5]);
                AssertInRange(8.0d / 9, topData[6]);
                AssertInRange(4.0d / 3, topData[7]);
                AssertInRange(8.0d / 9, topData[8]);
            }
        }
Esempio n. 2
0
        public void AveragePoolingLayer_Forward()
        {
            var bottom = new Tensor(1, 1, 3, 3);

            var filler = new ConstantFiller(2.0d);

            filler.Fill(bottom);

            var layer = new AveragePoolingLayer(3, 1, 1);

            layer.Setup(bottom, top);

            Assert.Equal(1, top.Num);
            Assert.Equal(1, top.Channels);
            Assert.Equal(3, top.Height);
            Assert.Equal(3, top.Width);

            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
            {
                var topData = topCpu.Data;
                AssertInRange(8.0d / 9, topData[0]);
                AssertInRange(4.0d / 3, topData[1]);
                AssertInRange(8.0d / 9, topData[2]);
                AssertInRange(4.0d / 3, topData[3]);
                AssertInRange(2.0d, topData[4]);
                AssertInRange(4.0d / 3, topData[5]);
                AssertInRange(8.0d / 9, topData[6]);
                AssertInRange(4.0d / 3, topData[7]);
                AssertInRange(8.0d / 9, topData[8]);
            }
        }
Esempio n. 3
0
        public void ForwardBackwardTest()
        {
            foreach (string format in new[] { Shape.BWHC, Shape.BHWC, Shape.BCHW })
            {
                Shape shape = new Shape(format, -1, 13, 11, 2);
                foreach (int kw in new[] { 1, 2, 3, 4, 5 })
                {
                    foreach (int kh in new[] { 1, 2, 3, 4, 5 })
                    {
                        foreach (int kstridex in new[] { 1, 2, 3 })
                        {
                            foreach (int kstridey in new[] { 1, 2, 3 })
                            {
                                foreach (int kpaddingx in new[] { 0, 2, -2 })
                                {
                                    foreach (int kpaddingy in new[] { 0, 2, -2 })
                                    {
                                        Kernel kernel             = new Kernel(kw, kh, kstridex, kstridey, kpaddingx, kpaddingy);
                                        AveragePoolingLayer layer = new AveragePoolingLayer(shape, kernel);

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

                                            Tensor x = new Tensor(null, shape.Reshape(Axis.B, mb));
                                            x.Randomize(this.random);

                                            Tensor y = layer.Forward(session, new[] { x })[0];

                                            Tensor expected = AveragePoolingLayerTest.CalculateY(x, kernel);
                                            Helpers.AreTensorsEqual(expected, y);

                                            // unroll the graph
                                            y.RandomizeGradient(this.random);
                                            session.Unroll();

                                            Tensor expectedDX = AveragePoolingLayerTest.CalculateDX(x, y, kernel);
                                            Helpers.AreGradientsEqual(expectedDX, x);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }