-
Notifications
You must be signed in to change notification settings - Fork 0
/
grad-check-activation.py
72 lines (53 loc) · 1.74 KB
/
grad-check-activation.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
"""
Gradient check to verify backprop of activation functions
"""
import argparse
import numpy as np
from numpy.linalg import norm
from ml.layers.activations import ReLU, Sigmoid
EPS = 1e-5
def check_sigmoid():
dim = int(input("Enter vector dimensions: "))
batch_size = int(input("Enter batch size: "))
x = np.random.randn(batch_size, dim)
sigmoid = Sigmoid()
_ = sigmoid.forward(x)
dy = np.ones((batch_size, dim))
dx = sigmoid.backward(dy)
dx_man = np.zeros([batch_size, dim])
for b in range(batch_size):
for i in range(dim):
h = np.zeros([batch_size, dim])
h[b, i] = EPS
dx_man[b, i] = (
(sigmoid.forward(x + h) - sigmoid.forward(x - h)) / (2 * EPS)
)[b, i]
diff = dx_man - dx
print("Norm of difference:")
print(norm(diff))
def check_relu():
dim = int(input("Enter vector dimensions: "))
batch_size = int(input("Enter batch size: "))
x = np.random.randn(batch_size, dim)
sigmoid = ReLU()
_ = sigmoid.forward(x)
dy = np.ones((batch_size, dim))
dx = sigmoid.backward(dy)
dx_man = np.zeros([batch_size, dim])
for b in range(batch_size):
for i in range(dim):
h = np.zeros([batch_size, dim])
h[b, i] = EPS
dx_man[b, i] = (
(sigmoid.forward(x + h) - sigmoid.forward(x - h)) / (2 * EPS)
)[b, i]
diff = dx_man - dx
print("Norm of difference:")
print(norm(diff))
args_to_fn = {"sigmoid": check_sigmoid, "relu": check_relu}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--function", required=True)
args = parser.parse_args()
check_fn = args_to_fn[args.function]
check_fn()