-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAFTest.py
306 lines (186 loc) · 6.49 KB
/
AFTest.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 15 16:18:37 2019
@author: rohrdr
"""
import numpy as np
from ActivationFunctions import Sigmoid, TanH, ReLU, LeakyRelu, Softplus
from Tools import eval_err, grad_num
npoints = 4
nsamples = 3
###
# some text snippets for the error messages
###
err1 = "Error in the get_activation"
err2 = " Function"
def test_suite():
"""
Runs all the tests available:
- SigmoidTest
- TanHTest
- ReLUTest
- leaky_ReLUTest
- SoftplusTest
"""
res = True
x = np.random.randn(npoints, nsamples) * 0.01
ret = sigmoid_test(x)
if not np.array(ret).any():
res = False
print("The results of the SigmoidTest are")
print(ret)
ret = tanh_test(x)
if not np.array(ret).any():
res = False
print("The results of the TanHTest are")
print(ret)
ret = relu_test(x)
if not np.array(ret).any():
res = False
print("The results of the ReLUTest are")
print(ret)
ret = leaky_relu_test(x)
if not np.array(ret).any():
res = False
print("The results of the leaky_ReLUTest are")
print(ret)
ret = softplus_test(x)
if not np.array(ret).any():
res = False
print("The results of the Softplus are")
print(ret)
if res: print('All tests on Activation Functions ran successfully')
return res
def sigmoid_test(x):
def test_activation(x, sig):
y = sig.get_activation(x)
z = np.exp(x) / (1.0 + np.exp(x))
errmsg = err1 + " function of the Sigmoid" + err2
res = eval_err(z, y, errmsg)
return res
def test_derivative(x, sig):
y = sig.get_activation_der(x)
z = np.exp(x) / np.power(1.0 + np.exp(x), 2)
errmsg = err1 + "_der function of the Sigmoid" + err2
res = eval_err(z, y, errmsg)
return res
result = []
sig = Sigmoid()
result.append(test_activation(x, sig))
result.append(test_derivative(x, sig))
result.append(num_derivative(x, sig))
return result
def tanh_test(x):
def test_activation(x, tan):
y = tan.get_activation(x)
z = np.sinh(x) / np.cosh(x)
errmsg = err1 + " function of the TanH" + err2
res = eval_err(z, y, errmsg)
return res
def test_derivative(x, tan):
y = tan.get_activation_der(x)
z = 1.0 / np.power(np.cosh(x), 2)
errmsg = err1 + "_der function of the TanH" + err2
res = eval_err(z, y, errmsg)
return res
result = []
tan = TanH()
result.append(test_activation(x, tan))
result.append(test_derivative(x, tan))
result.append(num_derivative(x, tan))
return result
def relu_test(x):
def test_activation(x, re_l):
y = re_l.get_activation(x)
z = np.where(x <= 0.0, 0.0, x)
errmsg = err1 + " function of the ReLU" + err2
res = eval_err(z, y, errmsg)
return res
def test_derivative(x, re_l):
y = re_l.get_activation_der(x)
z = np.where(x <= 0.0, 0.0, 1.0)
errmsg = err1 + "_der function of the ReLU" + err2
res = eval_err(z, y, errmsg)
return res
result = []
re_l = ReLU()
result.append(test_activation(x, re_l))
result.append(test_derivative(x, re_l))
result.append(num_derivative(x, re_l))
return result
def leaky_relu_test(x):
def test_activation(x, l_re_l):
y = l_re_l.get_activation(x)
z = np.where(x <= 0.0, 0.01 * x, x)
errmsg = err1 + " function of the LeakyRelu" + err2
res = eval_err(z, y, errmsg)
return res
def test_derivative(x, l_re_l):
y = l_re_l.get_activation_der(x)
z = np.where(x <= 0.0, 0.01, 1.0)
errmsg = err1 + "_der function of the LeakyRelu" + err2
res = eval_err(z, y, errmsg)
return res
def test_inverse_activation(x, l_re_l):
y = l_re_l.get_inverse_activation(x)
z = np.where(x <= 0.0, 100.0 * x, x)
errmsg = err1 + " function of the inverse LeakyRelu" + err2
res = eval_err(z, y, errmsg)
return res
def test_inverse_derivative(x, l_re_l):
y = l_re_l.get_inverse_activation_der(x)
z = np.where(x <= 0.0, 100.0, 1.0)
errmsg = err1 + "_der function of the inverse LeakyRelu" + err2
res = eval_err(z, y, errmsg)
return res
result = []
l_re_l = LeakyRelu()
result.append(test_activation(x, l_re_l))
result.append(test_derivative(x, l_re_l))
result.append(num_derivative(x, l_re_l))
result.append(test_inverse_activation(x, l_re_l))
result.append(test_inverse_derivative(x, l_re_l))
result.append(num_inv_derivative(x, l_re_l))
return result
def softplus_test(x):
def test_activation(x, l_re_l):
y = l_re_l.get_activation(x)
z = np.log(1.0) + np.log(1.0 + np.exp(x))
errmsg = err1 + " function of the SoftPlus" + err2
res = eval_err(z, y, errmsg)
return res
def test_derivative(x, l_re_l):
y = l_re_l.get_activation_der(x)
z = np.exp(x) / (1.0 + np.exp(x))
errmsg = err1 + "_der function of the Softplus" + err2
res = eval_err(z, y, errmsg)
return res
result = []
sop = Softplus()
result.append(test_activation(x, sop))
result.append(test_derivative(x, sop))
result.append(num_derivative(x, sop))
return result
def num_derivative(x, actfunc):
y = actfunc.get_activation_der(x)
nx = x.shape[0]
ny = x.shape[1]
new_x = x.reshape((nx * ny, 1))
z = grad_num(new_x, actfunc.get_activation)
new_z = z.reshape((nx, ny))
errmsg = "error in the numerical gradient of " + str(actfunc)
res = eval_err(new_z, y, errmsg)
return res
def num_inv_derivative(x, actfunc):
y = actfunc.get_inverse_activation_der(x)
nx = x.shape[0]
ny = x.shape[1]
new_x = x.reshape((nx * ny, 1))
z = grad_num(new_x, actfunc.get_inverse_activation)
new_z = z.reshape((nx, ny))
errmsg = "error in the numerical gradient of inverse " + str(actfunc)
res = eval_err(new_z, y, errmsg)
return res
if __name__ == '__main__':
test_suite()