MCPcopy Create free account
hub / github.com/apache/singa / test_conv2d

Method test_conv2d

test/python/test_mkldnn.py:24–94  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

22class TestPythonOperation(unittest.TestCase):
23
24 def test_conv2d(self):
25 print("TEST CONV2D FORWARD")
26 x_shape = [2, 1, 3, 3]
27 x = singa_wrap.Tensor(x_shape)
28 x.CopyFloatDataFromHostPtr(
29 [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9])
30
31 W_shape = [1, 1, 3, 3]
32 W = singa_wrap.Tensor(W_shape)
33 W.CopyFloatDataFromHostPtr([1, 1, 0, 0, 0, -1, 0, 1, 0])
34
35 b_shape = [1]
36 b = singa_wrap.Tensor(b_shape)
37 b.CopyFloatDataFromHostPtr([1])
38
39 dy_shape = [2, 1, 2, 2]
40 dy = singa_wrap.Tensor(dy_shape)
41 dy.CopyFloatDataFromHostPtr([0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4])
42
43 handle = singa_wrap.ConvHandle(x, (3, 3), (2, 2), (1, 1), 1, 1, True, 1)
44 y = singa_wrap.CpuConvForward(x, W, b, handle)
45
46 self.assertListEqual([2, 1, 2, 2], list(y.shape()))
47
48 _y = y.GetFloatValue(int(y.Size()))
49 self.assertAlmostEqual(3.0, _y[0])
50 self.assertAlmostEqual(7.0, _y[1])
51 self.assertAlmostEqual(-3.0, _y[2])
52 self.assertAlmostEqual(12.0, _y[3])
53 self.assertAlmostEqual(3.0, _y[4])
54 self.assertAlmostEqual(7.0, _y[5])
55 self.assertAlmostEqual(-3.0, _y[6])
56 self.assertAlmostEqual(12.0, _y[7])
57
58 print("TEST CONV2D DATA BACKWARD")
59
60 dx = singa_wrap.CpuConvBackwardx(dy, W, x, handle)
61 self.assertListEqual([2, 1, 3, 3], list(dx.shape()))
62
63 _dx = dx.GetFloatValue(int(dx.Size()))
64 self.assertAlmostEqual(0.0, _dx[0])
65 self.assertAlmostEqual(-0.1, _dx[1])
66 self.assertAlmostEqual(0.0, _dx[2])
67 self.assertAlmostEqual(0.4, _dx[3])
68 self.assertAlmostEqual(0.4, _dx[4])
69 self.assertAlmostEqual(0.6, _dx[5])
70 self.assertAlmostEqual(0.0, _dx[6])
71 self.assertAlmostEqual(-0.3, _dx[7])
72
73 print("TEST CONV2D WEIGHT BACKWARD")
74 dW = singa_wrap.CpuConvBackwardW(dy, x, W, handle)
75 self.assertListEqual([1, 1, 3, 3], list(dW.shape()))
76
77 _dW = dW.GetFloatValue(int(dW.Size()))
78 self.assertAlmostEqual(4.0, _dW[0], places=5)
79 self.assertAlmostEqual(7.2, _dW[1], places=5)
80 self.assertAlmostEqual(3.0, _dW[2], places=5)
81 self.assertAlmostEqual(7.2, _dW[3], places=5)

Callers

nothing calls this directly

Calls 4

TensorMethod · 0.80
ConvHandleMethod · 0.80
shapeMethod · 0.80
SizeMethod · 0.45

Tested by

no test coverage detected