-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nn.py
More file actions
53 lines (40 loc) · 1.41 KB
/
Copy pathtest_nn.py
File metadata and controls
53 lines (40 loc) · 1.41 KB
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
import numpy as np
import neural_nets as nn
def green(s):
return '\033[1;32m%s\033[m' % s
def yellow(s):
return '\033[1;33m%s\033[m' % s
def red(s):
return '\033[1;31m%s\033[m' % s
def check_rectified_linear_unit():
try:
x = 1.
nn.rectified_linear_unit(x)
except NotImplementedError:
print(yellow("FAIL"), ": ReLU not implemented!")
return
# Test for different values.
x = np.array([-5., 0., 5])
for i in range(len(x)):
if not nn.rectified_linear_unit(x[i]) == np.maximum(0,x[i]):
print(red("FAIL"), ": ReLU gives wrong output!")
return
print(green("PASS"), ": ReLU Implemented.")
def check_rectified_linear_unit_derivative():
try:
x = 1.
nn.rectified_linear_unit_derivative(x)
except NotImplementedError:
print(yellow("FAIL"), ": ReLU derivative not implemented!")
x = np.array([-5., 0., 5])
y = np.array([0., 0., 1. ])
for i in range(len(x)):
if not nn.rectified_linear_unit_derivative(x[i]) == y[i]:
print(red("FAIL"), ": ReLU derivative gives wrong output!")
return
print(green("PASS"), ": ReLU derivative Implemented.")
def main():
check_rectified_linear_unit()
check_rectified_linear_unit_derivative()
if __name__ == "__main__":
main()