-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_max.py
63 lines (52 loc) · 1.98 KB
/
test_max.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
import unittest
import numpy as np
from dezero import Variable
import dezero.functions as F
from dezero.utils import gradient_check, array_allclose
class TestMax(unittest.TestCase):
def test_forward1(self):
x = Variable(np.random.rand(10))
y = F.max(x)
expected = np.max(x.data)
self.assertTrue(array_allclose(y.data, expected))
def test_forward2(self):
shape = (10, 20, 30)
axis = 1
x = Variable(np.random.rand(*shape))
y = F.max(x, axis=axis)
expected = np.max(x.data, axis=axis)
self.assertTrue(array_allclose(y.data, expected))
def test_forward3(self):
shape = (10, 20, 30)
axis = (0, 1)
x = Variable(np.random.rand(*shape))
y = F.max(x, axis=axis)
expected = np.max(x.data, axis=axis)
self.assertTrue(array_allclose(y.data, expected))
def test_forward4(self):
shape = (10, 20, 30)
axis = (0, 1)
x = Variable(np.random.rand(*shape))
y = F.max(x, axis=axis, keepdims=True)
expected = np.max(x.data, axis=axis, keepdims=True)
self.assertTrue(array_allclose(y.data, expected))
def test_backward1(self):
x_data = np.random.rand(10)
f = lambda x: F.max(x)
self.assertTrue(gradient_check(f, x_data))
def test_backward2(self):
x_data = np.random.rand(10, 10) * 100
f = lambda x: F.max(x, axis=1)
self.assertTrue(gradient_check(f, x_data))
def test_backward3(self):
x_data = np.random.rand(10, 20, 30) * 100
f = lambda x: F.max(x, axis=(1, 2))
self.assertTrue(gradient_check(f, x_data))
def test_backward4(self):
x_data = np.random.rand(10, 20, 20) * 100
f = lambda x: F.sum(x, axis=None)
self.assertTrue(gradient_check(f, x_data))
def test_backward5(self):
x_data = np.random.rand(10, 20, 20) * 100
f = lambda x: F.sum(x, axis=None, keepdims=True)
self.assertTrue(gradient_check(f, x_data))