-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradTest.py
More file actions
141 lines (119 loc) · 4.48 KB
/
gradTest.py
File metadata and controls
141 lines (119 loc) · 4.48 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
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
import numpy as np
import matplotlib.pyplot as plt
from ActivationFuncs import Softmax
def normalize(v):
norm = np.linalg.norm(v, axis=0)
# print("v", v)
# print("norm", norm)
# print("v/norm", v / norm)
check = v / norm
return v / norm
def gradient_checkX(W, X, Y, b, check):
d = normalize(np.random.randn(X.shape[0], X.shape[1]))
# print("d", d)
# print(W.shape)
epsilon = 1
epsilons = []
differences = []
differences_with_grad = []
for i in range(1, 20):
epsilon = (0.5 ** i)
dw, dx, db = check.gradient(X, Y)
# print("epsilon", epsilon)
loss_epsilon = check.loss(X + epsilon * d, Y, W, b)
# loss_epsilon_check = loss2(W, X + epsilon * d, y)
loss_regular = check.loss(X, Y)
# loss_regular_check = loss2(W, X, y)
difference = np.abs(loss_epsilon - loss_regular)
# print("loss_epsilon", loss_epsilon - loss_regular)
# print("loss_shape", (loss_epsilon - loss_regular).shape)
# print("d", d)
# print("dx", dx)
# print("check vdot", np.vdot(epsilon * d.T, dx))
# print("dx", dx)
difference_with_grad = np.abs(loss_epsilon - loss_regular - np.vdot(epsilon * d, dx))
epsilons.append(epsilon)
differences.append(difference)
differences_with_grad.append(difference_with_grad)
print("epsilon", epsilon)
print("difference", difference)
print("difference_with_grad", difference_with_grad)
# print("difference_with_grad", np.mean(difference_with_grad))
# print()
# print("difference", difference)
# print("difference_with_grad", difference_with_grad)
# print(type(differences_with_grad))
# Plotting
plt.loglog(epsilons, differences, label='Without Gradient')
plt.loglog(epsilons, differences_with_grad, label='With Gradient')
# x_ticks = np.logspace(-20, 0, num=21, base=10)
# plt.xticks(x_ticks, [f"{tick:.0e}" for tick in x_ticks])
plt.xlabel('Epsilon')
plt.ylabel('Difference')
plt.legend()
plt.title('Difference by Epsilon')
plt.gca().invert_xaxis()
plt.show()
def gradient_checkW(W, X, Y, b, check):
d = normalize(np.random.randn(W.shape[0], W.shape[1]))
epsilon = 1
epsilons = []
differences = []
grad_diff = []
for i in range(1, 20):
epsilon = (0.5 ** i)
print("epsilon", epsilon)
loss_epsilon = check.loss(X,Y, W + epsilon * d, b)
dw, dx, db = check.gradient(X, Y)
loss_regular = check.loss(X, Y)
difference = np.abs(loss_epsilon - loss_regular)
difference_with_grad = np.abs(loss_epsilon - loss_regular - np.vdot(epsilon * d, dw))
epsilons.append(epsilon)
differences.append(difference)
grad_diff.append(difference_with_grad)
plt.loglog(epsilons, differences, label='Without Gradient')
plt.loglog(epsilons, grad_diff, label='With Gradient')
plt.xlabel('Epsilon')
plt.ylabel('Difference')
plt.title('Difference by Epsilon')
plt.gca().invert_xaxis()
plt.show()
def gradient_checkb(W, X, Y, b, check):
d = normalize(np.random.randn(b.shape[0], b.shape[1]))
print("d", d)
epsilon = 1
epsilons = []
differences = []
grad_diff = []
for i in range(1, 20):
epsilon = (0.5 ** i)
print("epsilon", epsilon)
loss_epsilon = check.loss(X, Y, W, b + epsilon * d)
dw, dx, db = check.gradient(X, Y)
loss_regular = check.loss(X, Y)
difference = np.abs(loss_epsilon - loss_regular)
difference_with_grad = np.abs(loss_epsilon - loss_regular - np.vdot(epsilon * d, db))
epsilons.append(epsilon)
differences.append(difference)
grad_diff.append(difference_with_grad)
print("difference", difference)
print("difference_with_grad", difference_with_grad)
plt.loglog(epsilons, differences, label='Without Gradient')
plt.loglog(epsilons, grad_diff, label='With Gradient')
plt.xlabel('Epsilon')
plt.ylabel('Difference')
plt.title('Difference by Epsilon')
plt.gca().invert_xaxis()
plt.show()
# Example usage
# np.random.seed(42)
def main():
W = np.random.randn(5, 5) # Example weights
X = np.random.randn(5, 3) # Example input
b = np.random.randn(1, 5) # Example biases
# y is supposed to be 3 5
y = np.array([[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0]]) # Example one-hot encoded labels
check = Softmax(W, b)
gradient_checkW(W, X, y, b, check)
if __name__ == '__main__':
main()