-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_activations.py
151 lines (127 loc) · 4.38 KB
/
test_activations.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
147
148
149
150
151
import numpy as np
from mlcvlab.nn.activations import relu, relu_grad, sigmoid, sigmoid_grad, softmax, softmax_grad, tanh, tanh_grad
RESULTS = {}
def test_sigmoid_1():
x = np.array([1])
sigm = sigmoid(x)
expected_sigm = [0.73105858]
#print(f"sigmoid_1 : ",sigm)
RESULTS["TEST_SIGMOID_1"] = np.allclose(sigm.round(8), expected_sigm)
def test_sigmoid_2():
x = np.array([3, 2, 1])
sigm = sigmoid(x)
expected_sigm = [0.95257413, 0.88079708, 0.73105858]
#print(f"sigmoid_2 : ",sigm)
RESULTS["TEST_SIGMOID_2"] = np.allclose(sigm.round(8), expected_sigm)
def test_sigmoid_grad_1():
x = np.array([1])
y = sigmoid(x)
sigm_grad = sigmoid_grad(y)
expected_sigm_grad = [0.19661193]
#print(f"sigmoid_grad_1 : ",sigm_grad)
RESULTS["TEST_SIGMOID_GRAD_1"] = np.allclose(sigm_grad.round(8), expected_sigm_grad)
def test_sigmoid_grad_2():
x = np.array([3, 2, 1])
y = sigmoid(x)
sigm_grad = sigmoid_grad(y)
expected_sigm_grad = [0.04517666, 0.10499359, 0.19661193]
#print(f"sigmoid_grad_2 : ",sigm_grad)
RESULTS["TEST_SIGMOID_GRAD_2"] = np.allclose(sigm_grad.round(8), expected_sigm_grad)
def test_softmax_1():
x = np.array([
[7, 4, 5, 1, 0],
[4, 9, 1, 0 ,5]])
z = softmax(x)
exp_softmax = np.array([
[8.41387525e-01, 4.18902183e-02, 1.13869419e-01, 2.08559116e-03, 7.67246110e-04],
[6.57032193e-03, 9.75122235e-01, 3.27117067e-04, 1.20339644e-04,1.78599867e-02]
], dtype='f')
# print("test_softmax_1 : ",z)
RESULTS["TEST_SOFTMAX_1"] = np.allclose(z.round(8), exp_softmax)
def test_softmax_grad_1():
#softmax value for [1, 3, 5, 7]
x = np.array([
[1, 3, 5, 7]])
sm=softmax(x)
# exp_sm = [2.14400878e-03, 1.58422012e-02, 1.17058913e-01, 8.64954877e-01]
# print("sm :", sm)
sm_grad = softmax_grad(sm)
# print("softmax grad:",sm_grad)
exp_sm_grad = np.array([
[ 2.13941201e-03, -3.39658185e-05, -2.50975338e-04, -1.85447085e-03],
[-3.39658185e-05, 1.55912258e-02, -1.85447085e-03, -1.37027892e-02],
[-2.50975338e-04, -1.85447085e-03, 1.03356124e-01, -1.01250678e-01],
[-1.85447085e-03, -1.37027892e-02, -1.01250678e-01, 1.16807938e-01]], dtype='f')
RESULTS["TEST_SOFTMAX_GRAD_1"] = np.allclose(sm_grad.round(8), exp_sm_grad)
def test_tanh_1():
x = np.array([
[-4, -3],
[-2, -1],
[ 0, 1],
[ 2, 3]], dtype="f")
z = tanh(x)
#print("tanh :",z)
expected_tanh = np.array([
[-0.9993, -0.9951],
[-0.964, -0.7616],
[ 0., 0.7616],
[ 0.964, 0.9951]], dtype='f')
RESULTS["TEST_TANH_1"] = np.allclose(z.round(4), expected_tanh)
def test_tanh_grad_1():
x = np.array([
[-4, -3],
[-2, -1],
[ 0, 1],
[ 2, 3]], dtype="f")
output = tanh_grad(x)
#print("tanh grad :",output)
expected_tanh_grad = np.array([
[0.0013, 0.0099],
[0.0707, 0.42 ],
[1. , 0.42 ],
[0.0707, 0.0099]], dtype="f")
RESULTS["TEST_TANH_GRAD_1"] = np.allclose(output.round(4), expected_tanh_grad)
def test_relu():
A = np.array([
[-4, -3],
[-2, -1],
[ 0, 1],
[ 2, 3]], dtype="f")
res = relu(A)
#print(f"relu : {res}")
expected_relu = np.array([
[0., 0.],
[0., 0.],
[0., 1.],
[2., 3.]], dtype="f")
RESULTS["TEST_RELU"] = np.allclose(res, expected_relu)
def test_relu_grad():
Z = np.array([
[0., 0.],
[0., 0.],
[0., 1.],
[2., 3.]], dtype='f')
relu_grad_ = relu_grad(Z)
#print(f"relu_grad : {relu_grad_}")
expected_relu_grad = np.array([
[0., 0.],
[0., 0.],
[0., 1.],
[1., 1.]], dtype="f")
RESULTS["TEST_RELU_GRAD"] = np.allclose(relu_grad_, expected_relu_grad)
if __name__ == "__main__":
test_sigmoid_1()
test_sigmoid_2()
test_sigmoid_grad_1()
test_sigmoid_grad_2()
test_softmax_1()
test_softmax_grad_1()
test_tanh_1()
test_tanh_grad_1()
test_relu()
test_relu_grad()
result = True
for k,v in RESULTS.items():
print(f"{k.rjust(30,' ')} : {str(v).ljust(15,' ')}")
result = result and v
print(f"\n\nTEST_ACTIVATIONS : {result}")