-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_conv2d.py
146 lines (128 loc) · 5.15 KB
/
test_conv2d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import unittest
import numpy as np
import dezero.layers as L
import dezero.functions as F
from dezero.utils import gradient_check, array_equal
import chainer.functions as CF
class TestConv2d_simple(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 5, 15, 15
o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d_simple(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward2(self):
n, c, h, w = 1, 5, 15, 15
o, k, s, p = 8, (3, 3), (3, 1), (2, 1)
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d_simple(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward3(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d_simple(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward4(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = np.random.randn(o).astype('f')
y = F.conv2d_simple(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_backward1(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda x: F.conv2d_simple(x, W, b, s, p)
self.assertTrue(gradient_check(f, x))
def test_backward2(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda b: F.conv2d_simple(x, W, b, s, p)
self.assertTrue(gradient_check(f, b))
def test_backward3(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda W: F.conv2d_simple(x, W, b, s, p)
self.assertTrue(gradient_check(f, W))
class TestConv2d(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 5, 15, 15
o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward2(self):
n, c, h, w = 1, 5, 15, 15
o, k, s, p = 8, (3, 3), (3, 1), (2, 1)
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward3(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = None
y = F.conv2d(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_forward4(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w).astype('f')
W = np.random.randn(o, c, k[0], k[1]).astype('f')
b = np.random.randn(o).astype('f')
y = F.conv2d(x, W, b, s, p)
expected = CF.convolution_2d(x, W, b, s, p)
self.assertTrue(array_equal(expected.data, y.data))
def test_backward1(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda x: F.conv2d(x, W, b, s, p)
self.assertTrue(gradient_check(f, x))
def test_backward2(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda b: F.conv2d(x, W, b, s, p)
self.assertTrue(gradient_check(f, b))
def test_backward3(self):
n, c, h, w = 1, 5, 20, 15
o, k, s, p = 3, (5, 3), 1, 3
x = np.random.randn(n, c, h, w)
W = np.random.randn(o, c, k[0], k[1])
b = np.random.randn(o)
f = lambda W: F.conv2d(x, W, b, s, p)
self.assertTrue(gradient_check(f, W))