-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pooling.py
89 lines (68 loc) · 3.02 KB
/
test_pooling.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
import unittest
import numpy as np
import dezero.functions as F
from dezero.utils import gradient_check, array_allclose
import chainer.functions as CF
class TestPooling_simple(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.pooling_simple(x, ksize, stride, pad)
expected = CF.max_pooling_2d(x, ksize, stride, pad)
self.assertTrue(array_allclose(expected.data, y.data))
def test_forward2(self):
n, c, h, w = 1, 5, 15, 15
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.pooling_simple(x, ksize, stride, pad)
expected = CF.max_pooling_2d(x, ksize, stride, pad, cover_all=False)
self.assertTrue(array_allclose(expected.data, y.data))
def test_backward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f') * 100
f = lambda x: F.pooling_simple(x, ksize, stride, pad)
self.assertTrue(gradient_check(f, x))
class TestPooling(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.pooling(x, ksize, stride, pad)
expected = CF.max_pooling_2d(x, ksize, stride, pad)
self.assertTrue(array_allclose(expected.data, y.data))
def test_forward2(self):
n, c, h, w = 1, 5, 15, 15
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.pooling(x, ksize, stride, pad)
expected = CF.max_pooling_2d(x, ksize, stride, pad, cover_all=False)
self.assertTrue(array_allclose(expected.data, y.data))
def test_backward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f') * 1000
f = lambda x: F.pooling(x, ksize, stride, pad)
self.assertTrue(gradient_check(f, x))
class TestAveragePooling(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.average_pooling(x, ksize, stride, pad)
expected = CF.average_pooling_2d(x, ksize, stride, pad)
self.assertTrue(array_allclose(expected.data, y.data))
def test_forward2(self):
n, c, h, w = 1, 5, 15, 15
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f')
y = F.average_pooling(x, ksize, stride, pad)
expected = CF.average_pooling_2d(x, ksize, stride, pad)
self.assertTrue(array_allclose(expected.data, y.data))
def test_backward1(self):
n, c, h, w = 1, 5, 16, 16
ksize, stride, pad = 2, 2, 0
x = np.random.randn(n, c, h, w).astype('f') * 1000
f = lambda x: F.average_pooling(x, ksize, stride, pad)
self.assertTrue(gradient_check(f, x))