-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests.py
149 lines (113 loc) · 3.35 KB
/
unit_tests.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
from linear import Linear
from torch import Tensor
from network import Network
from MSE import MSE
import matplotlib
import matplotlib.pyplot as plt
from activation import Relu, Tanh
### Unit Tests
def test_linear_weights():
w = Tensor([[2, 4, 8], [16, 32, 69]])
b = Tensor([0, 0, 0])
x = Tensor([3, 9, 27])
print(w.shape, b.shape)
l1 = Linear(2, 3)
l1.init_weights(1)
w, b = l1.param()
print(w.shape, b.shape)
def run_mini_example():
x = Tensor([1, 2, 3])
y = Tensor([7, 10])
print(x.shape, y.shape)
linear = Linear(x.shape[0], y.shape[0], weight_init='ones')
net = Network([linear])
pred = net.forward(x)
#loss.backward()
print("Pred is ")
print(pred)
#print(x.grad)
def run_bigger_example():
x = Tensor([1, 2, 3])
y = Tensor([7, 10])
print(x.shape, y.shape)
linear1 = Linear(x.shape[0], x.shape[0], weight_init='ones')
linear2 = Linear(x.shape[0], y.shape[0], weight_init='ones')
net_2layer = Network([linear1, linear2])
pred_2layer = net_2layer.forward(x)
#loss.backward()
print("pred_2layer is ")
print(pred_2layer)
mse = MSE()
loss = mse.forward(pred_2layer, y)
print("loss for 2 layer net is ")
print(loss)
# Should be 2*(18-7) = 22
loss_grad = mse.backward()
print("loss_grad for 2layer net is ")
print(loss_grad)
print("Printing params Grad before ")
for layer in net_2layer.layers:
for par_grad in layer.param_grad():
print(par_grad)
print("now setting param grad to zero")
net_2layer.zero_grad()
print("Printing params Grad after ")
for layer in net_2layer.layers:
for par_grad in layer.param_grad():
print(par_grad)
print("Printing params before backward")
for layer in net_2layer.layers:
for par in layer.param():
print(par)
print("Doing backward pass")
net_2layer.backward(loss_grad)
print("Printing params after backward")
for layer in net_2layer.layers:
for par in layer.param():
print(par)
print("Printing params Grad")
for layer in net_2layer.layers:
for par_grad in layer.param_grad():
print(par_grad)
print("Doing param update")
net_2layer.grad_step(lr=1e-3)
print("Printing params after update")
for layer in net_2layer.layers:
for par in layer.param():
print(par)
if __name__ == '__main__':
x = Tensor([[1, 2, 3], [1, 2, 3]])
y = Tensor([7, 10])
print(x.shape, y.shape)
#linear_a = Linear(x.shape[1], 4, weight_init='ones')
#linear_b = Linear(x.shape[0], y.shape[0], weight_init='ones')
#relu = Relu()
#net_2layer = Network([linear_a], 2)#, relu, linear_b])
#print(x.view(-1, 2).shape)
#print(net_2layer.forward(x.view(-1, 2)))
linear1 = Linear(x.shape[0], x.shape[0], weight_init='ones')
linear2 = Linear(x.shape[0], y.shape[0], weight_init='ones')
net_2layer = Network([linear1, linear2], 1)
mse = MSE()
lr = 1e-3
num_iter = 200
timesteps = []
loss_at_timesteps = []
for it in range(num_iter):
net_2layer.zero_grad()
pred_2layer = net_2layer.forward(x)
loss = mse.forward(pred_2layer, y)
print("At iteration ", str(it), " the loss is ", loss)
loss_grad = mse.backward()
net_2layer.backward(loss_grad)
net_2layer.grad_step(lr=1e-3)
timesteps.append(it)
loss_at_timesteps.append(loss)
print("Prediction at the end ", net_2layer.forward(x))
fig, ax = plt.subplots()
ax.plot(timesteps, loss_at_timesteps)
ax.set(xlabel='iteration (s)', ylabel='Training Loss',
title='The Loss curve')
ax.grid()
#fig.savefig("test.png")
plt.show()