-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1_tests.py
198 lines (159 loc) · 4.9 KB
/
part1_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import numpy as np
import part1_nn_lib as nn
def linear():
print("--------------------")
print("Testing Linear Layer")
print("--------------------")
dat = np.loadtxt("iris.dat")
np.random.shuffle(dat)
x = dat[:, :4]
y = dat[:, 4:]
split_idx = int(0.8 * len(x))
x_train = x[:split_idx]
y_train = y[:split_idx]
x_val = x[split_idx:]
y_val = y[split_idx:]
prep_input = nn.Preprocessor(x_train)
x_train_pre = prep_input.apply(x_train)
x_val_pre = prep_input.apply(x_val)
print("\n>> Testing Constructor\n")
layer = nn.LinearLayer(4, 4)
print(layer._W)
print(layer._b)
print(layer._cache_current)
assert layer._grad_W_current.shape == layer._W.shape
assert layer._grad_b_current.shape == layer._b.shape
print("\n>> Testing Forward Propagation\n")
d_in = x_train_pre[:4, :]
out = layer(d_in)
print(out)
assert np.isclose(layer._cache_current.transpose(), d_in).all()
print("\n>> Testing Backwards Propagation")
grad_z = np.array(
[
[1, -3, 4, 1.2],
[0.6, -2.7, 3.3, -0.3],
[1.2, -5.2, 1.9, 2.4],
[2.8, -2.9, 5.1, -1.2],
]
)
print("\n- Previous Gradient Values:")
print(layer._grad_W_current)
print(layer._grad_b_current)
grad_x = layer.backward(grad_z)
assert grad_x.shape == (4, 4)
print("\n- New Gradient Values:")
print(layer._grad_W_current)
print(layer._grad_b_current)
print("\n>> Testing update_params Function")
print("\n- Previous param:")
print(layer._W)
print(layer._b)
layer.update_params(0.001)
print("\n- New param:")
print(layer._W)
print(layer._b)
def activation():
print("-------------------------")
print("Testing Activation Layer")
print("-------------------------")
print("\n>> Testing Sigmoid Activation")
dat = np.loadtxt("iris.dat")
np.random.shuffle(dat)
x = dat[:, :4]
y = dat[:, 4:]
split_idx = int(0.8 * len(x))
x_train = x[:split_idx]
y_train = y[:split_idx]
x_val = x[split_idx:]
y_val = y[split_idx:]
prep_input = nn.Preprocessor(x_train)
x_train_pre = prep_input.apply(x_train)
x_val_pre = prep_input.apply(x_val)
print("\n> Testing Constructor")
layer = nn.SigmoidLayer()
print("\n> Testing Forward")
d_in = x_train_pre[:4, :]
out = layer(d_in)
print(out)
assert np.isclose(layer._cache_current, d_in).all()
assert np.isclose(out, np.reciprocal(np.exp(-d_in) + 1)).all()
print("\n> Testing Backward")
grad_z = np.array(
[
[1, -3, 4, 1.2],
[0.6, -2.7, 3.3, -0.3],
[1.2, -5.2, 1.9, 2.4],
[2.8, -2.9, 5.1, -1.2],
]
)
grad_x = layer.backward(grad_z)
assert grad_x.shape == grad_z.shape
print(grad_x)
sig = np.reciprocal(np.exp(-layer._cache_current) + 1)
assert np.isclose(grad_x, grad_z * sig * (1 - sig)).all()
print("\n\n>> Testing Relu Activation")
print("\n> Testing Constructor")
layer = nn.ReluLayer()
print("\n> Testing Forward")
d_in = x_train_pre[:4, :]
out = layer(d_in)
print(out)
assert out.shape == d_in.shape
assert np.isclose(layer._cache_current, d_in).all()
test_out = d_in
test_out[test_out < 0] = 0
assert np.isclose(out, test_out).all()
print("\n> Testing Backward")
grad_z = np.array(
[
[1, -3, 4, 1.2],
[0.6, -2.7, 3.3, -0.3],
[1.2, -5.2, 1.9, 2.4],
[2.8, -2.9, 5.1, -1.2],
]
)
grad_x = layer.backward(grad_z)
assert grad_x.shape == grad_z.shape
print(grad_x)
assert np.isclose(grad_x, grad_z * (layer._cache_current > 0).astype(int)).all()
def multilayer():
print("-------------------------")
print("Testing Multi Layer Network")
print("-------------------------")
dat = np.loadtxt("iris.dat")
np.random.shuffle(dat)
x = dat[:, :4]
y = dat[:, 4:]
split_idx = int(0.8 * len(x))
x_train = x[:split_idx]
y_train = y[:split_idx]
x_val = x[split_idx:]
y_val = y[split_idx:]
prep_input = nn.Preprocessor(x_train)
x_train_pre = prep_input.apply(x_train)
x_val_pre = prep_input.apply(x_val)
print("\n>> Testing Constructor\n")
network = nn.MultiLayerNetwork(
input_dim=4, neurons=[16, 3], activations=["relu", "sigmoid"]
)
EPOCHS = 100
learning_rate = 0.01
for _ in range(EPOCHS):
y_pred = network.forward(x_train)
grad_z = y_pred - y_train
network.backward(grad_z)
network.update_params(learning_rate)
if __name__ == "__main__":
print("1 - Linear Layer Test")
print("2 - Activation Layer Test")
print("3 - Multi-Layer Network Test")
option = input()
if option == "1":
linear()
elif option == "2":
activation()
elif option == "3":
multilayer()
else:
print("invalid option")