-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrad-check-loss.py
74 lines (53 loc) · 1.78 KB
/
grad-check-loss.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
"""
Gradient check to verify backprop of loss functions
"""
import argparse
import numpy as np
from numpy.linalg import norm
from ml.loss import BCELoss, MSELoss
EPS = 1e-5
def check_mse():
dim = int(input("Enter vector dimensions: "))
batch_size = int(input("Enter batch size: "))
true = np.random.randn(batch_size, dim)
pred = np.random.randn(batch_size, dim)
mse = MSELoss()
_ = mse.forward(true, pred)
dpred = mse.backward()
dpred_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
dpred_man[b, i] = (
mse.forward(true, pred + h) - mse.forward(true, pred - h)
) / (2 * EPS)
diff = dpred_man - dpred
print("Norm of difference:")
print(norm(diff))
def check_bce():
dim = int(input("Enter vector dimensions: "))
batch_size = int(input("Enter batch size: "))
true = np.random.choice([0, 1], (batch_size, dim))
pred = np.random.uniform(0, 1, (batch_size, dim))
bce = BCELoss()
_ = bce.forward(true, pred)
dpred = bce.backward()
dpred_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
dpred_man[b, i] = (
bce.forward(true, pred + h) - bce.forward(true, pred - h)
) / (2 * EPS)
diff = dpred_man - dpred
print("Norm of difference:")
print(norm(diff))
args_to_fn = {"mse": check_mse, "bce": check_bce}
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()