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]); } }
public void CloneTest() { AveragePoolingLayer layer1 = new AveragePoolingLayer(AveragePoolingLayerTest.shape, new Kernel(2, 2, 2, 2)); AveragePoolingLayer layer2 = layer1.Clone() as AveragePoolingLayer; Assert.AreEqual(JsonConvert.SerializeObject(layer1), JsonConvert.SerializeObject(layer2)); }
public void CopyConstructorTest1() { AveragePoolingLayer layer1 = new AveragePoolingLayer(AveragePoolingLayerTest.shape, new Kernel(3, 2, 1, 2)); AveragePoolingLayer layer2 = new AveragePoolingLayer(layer1); Assert.AreEqual(JsonConvert.SerializeObject(layer1), JsonConvert.SerializeObject(layer2)); }
public void SerializeTest() { AveragePoolingLayer layer1 = new AveragePoolingLayer(AveragePoolingLayerTest.shape, new Kernel(2, 2, 2, 2)); string s1 = JsonConvert.SerializeObject(layer1); AveragePoolingLayer layer2 = JsonConvert.DeserializeObject <AveragePoolingLayer>(s1); string s2 = JsonConvert.SerializeObject(layer2); Assert.AreEqual(s1, s2); }
public void AveragePoolingLayer_BackwardGradient( Size kernel, Size stride, Size padding ) { var filler = new ConstantFiller(2.0d); filler.Fill(bottom); var checker = new GradientChecker(1e-2f, 1e-2f); var layer = new AveragePoolingLayer(kernel, stride, padding); checker.CheckExhaustive(layer, bottom, top); }
public void AveragePoolingLayer_SetupPadded() { var layer = new AveragePoolingLayer(3, 2, 1); layer.Setup(bottom, top); Assert.Equal(bottom.Num, top.Num); Assert.Equal(bottom.Channels, top.Channels); Assert.Equal(4, top.Height); Assert.Equal(3, top.Width); }
public void ConstructorTest2() { AveragePoolingLayer layer = new AveragePoolingLayer(AveragePoolingLayerTest.shape, new Kernel(3, 2, 1, 2)); CollectionAssert.AreEqual(new[] { -1, 3, 2, 2 }, layer.OutputShape.Axes); Assert.AreEqual("AP3x2+1x2(S)", layer.Architecture); Assert.AreEqual(3, layer.Kernel.Width); Assert.AreEqual(2, layer.Kernel.Height); Assert.AreEqual(1, layer.Kernel.StrideX); Assert.AreEqual(2, layer.Kernel.StrideY); }
public void AveragePoolingLayer_BackwardGradient(Size kernel, Size stride, Size padding) { var filler = new ConstantFiller(2.0d); filler.Fill(bottom); var checker = new GradientChecker(1e-2f, 1e-2f); var layer = new AveragePoolingLayer(kernel, stride, padding); checker.CheckExhaustive(layer, bottom, top); }
public void ArchitectureConstructorTest4() { string architecture = "AP2"; AveragePoolingLayer layer = new AveragePoolingLayer(AveragePoolingLayerTest.shape, architecture, null); Assert.AreEqual(architecture, layer.Architecture); Assert.AreEqual(2, layer.Kernel.Width); Assert.AreEqual(2, layer.Kernel.Height); Assert.AreEqual(2, layer.Kernel.StrideX); Assert.AreEqual(2, layer.Kernel.StrideY); Assert.AreEqual(0, layer.Kernel.PaddingX); Assert.AreEqual(0, layer.Kernel.PaddingY); }
public void ArchitectureConstructorTest5() { string architecture = "AP"; try { AveragePoolingLayer layer = new AveragePoolingLayer(AveragePoolingLayerTest.shape, architecture, null); } catch (ArgumentException e) { Assert.AreEqual( new ArgumentException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.E_InvalidLayerArchitecture, architecture), nameof(architecture)).Message, e.Message); throw; } }
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); } } } } } } } } }