-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function_test.py
107 lines (93 loc) · 2.28 KB
/
Function_test.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
from Function import Square, Exp, square, Add, add
from Variable import Variable
import numpy as np
import unittest
import Utils as u
def Test1():
x = Variable(11)
f = Square()
print(f(x))
def Test2():
A = Square()
B = Exp()
C = Square()
x = Variable(np.array(0.5))
a = A(x)
b = B(a)
y = C(b)
# <-----| forward
# C(B(A(x)))
# |-----> backward
y.grad = np.array(1.0)
b.grad = C.backward(y.grad)
a.grad = B.backward(b.grad)
x.grad = A.backward(a.grad)
print(x.grad)
def Test3():
A = Square()
B = Exp()
C = Square()
x = Variable(np.array(0.5))
a = A(x)
b = B(a)
y = C(b)
# C(B(A(x)))
# |-----> backward
# y b a
assert y.creator == C
assert y.creator.input == b
assert y.creator.input.creator == B
assert y.creator.input.creator.input == a
assert y.creator.input.creator.input.creator == A
assert y.creator.input.creator.input.creator.input == x
print('Pass Assert, try BP')
y.grad = np.array(1.)
C = y.creator
b = C.input
b.grad = C.backward(y.grad)
B = b.creator
a = B.input
a.grad = B.backward(b.grad)
A = a.creator
x = A.input
x.grad = A.backward(a.grad)
print(x.grad)
def Test4():
A = Square()
B = Exp()
C = Square()
x = Variable(np.array(0.5))
a = A(x)
b = B(a)
y = C(b)
y.grad = np.array(1.)
y.backward()
print(x.grad)
class SquareTest(unittest.TestCase):
def test_forward(self):
X = Variable(np.array(2.))
y = square(X)
expected = np.array(4.)
self.assertEqual(y.data, expected)
def test_backward(self):
X = Variable(np.array(3.))
y = square(X)
y.backward()
expected = np.array(6.)
self.assertEqual(X.grad, expected)
def test_backward_auto(self):
X = Variable(np.array(np.random.rand(1)))
y = square(X)
y.backward()
expected = u.numerical_diff(square, X)
print(expected)
print(X.grad)
flag = np.allclose(X.grad, expected)
self.assertTrue(flag)
def Test5():
f = Add()
y = f(Variable(np.array(2.)),Variable(np.array(3.)))
assert y.data == 5
print(y.data)
assert add(Variable(np.array(1.)),Variable(np.array(1.))).data == 2
Test5()