-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
188 lines (146 loc) · 7.63 KB
/
test.py
File metadata and controls
188 lines (146 loc) · 7.63 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
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
import numpy as np
import test_utils
from framework.activation_layers import ReLU, Sigmoid, Softmax, Tanh
from framework.input_layer import DefaultInputLayer
from framework.layers import FullyConnectedLayer
from framework.losses import CrossEntropy, MeanSquaredError
from framework.network import NeuralNetwork
from framework.optimizer import SGD
from framework.tensor import Tensor
from framework.utils import classes_to_one_hot_vector, one_hot_vector_to_classes
from test_utils import get_inputs, in_shape, out_shape
def test_mse():
input, _ = get_inputs()
target = np.ones(input.shape)
exp_outs = test_utils.Exp_Outputs_MSE()
mse_loss = MeanSquaredError()
output = mse_loss.forward(Tensor(input), Tensor(target))
assert np.abs(output - exp_outs.get_exp_forward_out()) < 1e-7
output = mse_loss.backward(Tensor(input), Tensor(target))
assert np.all(np.abs(output.get_deltas() - exp_outs.get_exp_gradients()) < 1e-7)
def test_CrossEntropy():
input, _ = get_inputs()
target = np.zeros(input.shape)
target[np.argmax(input)] = 1
exp_outs = test_utils.Exp_Outputs_CrossEntropy()
mse_loss = CrossEntropy()
output = mse_loss.forward(Tensor(input), Tensor(target))
assert np.abs(output - exp_outs.get_exp_forward_out()) < 1e-7
output = mse_loss.backward(Tensor(input), Tensor(target))
assert np.all(np.abs(output.get_deltas() - exp_outs.get_exp_gradients()) < 1e-7)
def test_Dense_MSE():
input, weights = get_inputs()
exp_outs = test_utils.Exp_Outputs_Dense_MSE()
layers = [
FullyConnectedLayer(in_shape, out_shape),
]
layers[0].weights = Tensor(weights)
nn = NeuralNetwork(DefaultInputLayer(), layers)
out_tensors = nn.forward(input)
assert np.all(np.abs(out_tensors[-1].get_elements() - exp_outs.get_exp_forward_out()) < 1e-7)
print("Forward pass is correct for the given example!")
SGD.update(
nn=nn,
loss=MeanSquaredError(),
lr=0.01,
epochs=1,
data=(np.array([input]), np.ones((1, 1, 8))),
)
assert np.all(np.abs(nn.layers[0].bias.get_elements() - exp_outs.get_exp_updated_bias()) < 1e-7)
assert np.all(np.abs(nn.layers[0].weights.get_elements() - exp_outs.get_exp_updated_weights()) < 1e-7)
print("Weight and Bias gradients are correct for the given example!")
def test_Dense_Softmax_CrossEntropy():
input, weights = get_inputs()
exp_outs = test_utils.Exp_Outputs_Dense_Softmax_CrossEntropy()
layers = [FullyConnectedLayer(in_shape, out_shape), Softmax()]
layers[0].weights = Tensor(weights)
nn = NeuralNetwork(DefaultInputLayer(), layers)
out_tensors = nn.forward(input)
print(out_tensors[-1])
assert np.all(np.abs(out_tensors[-1].get_elements() - exp_outs.get_exp_forward_out()) < 1e-7)
print("Forward pass is correct for the given example!")
SGD.update(
nn=nn,
loss=CrossEntropy(),
lr=0.01,
epochs=1,
data=(np.array([input]), np.ones((1, 1, 8))),
)
assert np.all(np.abs(nn.layers[0].bias.get_elements() - exp_outs.get_exp_updated_bias()) < 1e-7)
assert np.all(np.abs(nn.layers[0].weights.get_elements() - exp_outs.get_exp_updated_weights()) < 1e-7)
print("Weight and Bias gradients are correct for the given example!")
def test_Dense_Sigmoid_Dense_Softmax_CrossEntropy():
input, weights = get_inputs(2)
exp_outs = test_utils.Exp_Outputs_Dense_Sigmoid_Dense_Softmax_CrossEntropy()
layers = [FullyConnectedLayer(in_shape, out_shape), Sigmoid(), FullyConnectedLayer(out_shape, out_shape), Softmax()]
layers[0].weights = Tensor(weights[0])
layers[2].weights = Tensor(weights[1])
nn = NeuralNetwork(DefaultInputLayer(), layers)
out_tensors = nn.forward(input)
assert np.all(np.abs(out_tensors[-1].get_elements() - exp_outs.get_exp_forward_out()) < 1e-7)
print("Forward pass is correct for the given example!")
SGD.update(
nn=nn,
loss=CrossEntropy(),
lr=0.01,
epochs=1,
data=(np.array([input]), np.ones((1, 1, 8))),
)
assert np.all(np.abs(nn.layers[0].bias.get_elements() - exp_outs.get_exp_updated_bias_1()) < 1e-7)
assert np.all(np.abs(nn.layers[0].weights.get_elements() - exp_outs.get_exp_updated_weights_1()) < 1e-7)
assert np.all(np.abs(nn.layers[2].bias.get_elements() - exp_outs.get_exp_updated_bias_2()) < 1e-7)
assert np.all(np.abs(nn.layers[2].weights.get_elements() - exp_outs.get_exp_updated_weights_2()) < 1e-7)
print("Weight and Bias gradients are correct for the given example!")
def test_Dense_ReLU_Dense_Softmax_CrossEntropy():
input, weights = get_inputs(2)
exp_outs = test_utils.Exp_Outputs_Dense_ReLU_Dense_Softmax_CrossEntropy()
layers = [FullyConnectedLayer(in_shape, out_shape), ReLU(), FullyConnectedLayer(out_shape, out_shape), Softmax()]
layers[0].weights = Tensor(weights[0])
layers[2].weights = Tensor(weights[1])
nn = NeuralNetwork(DefaultInputLayer(), layers)
out_tensors = nn.forward(input)
assert np.all(np.abs(out_tensors[-1].get_elements() - exp_outs.get_exp_forward_out()) < 1e-7)
print("Forward pass is correct for the given example!")
SGD.update(
nn=nn,
loss=CrossEntropy(),
lr=0.01,
epochs=1,
data=(np.array([input]), np.ones((1, 1, 8))),
)
assert np.all(np.abs(nn.layers[0].bias.get_elements() - exp_outs.get_exp_updated_bias_1()) < 1e-7)
assert np.all(np.abs(nn.layers[0].weights.get_elements() - exp_outs.get_exp_updated_weights_1()) < 1e-7)
assert np.all(np.abs(nn.layers[2].bias.get_elements() - exp_outs.get_exp_updated_bias_2()) < 1e-7)
assert np.all(np.abs(nn.layers[2].weights.get_elements() - exp_outs.get_exp_updated_weights_2()) < 1e-7)
print("Weight and Bias gradients are correct for the given example!")
def test_Dense_Tanh_Dense_Softmax_CrossEntropy():
input, weights = get_inputs(2)
exp_outs = test_utils.Exp_Outputs_Dense_Tanh_Dense_Softmax_CrossEntropy()
layers = [FullyConnectedLayer(in_shape, out_shape), Tanh(), FullyConnectedLayer(out_shape, out_shape), Softmax()]
layers[0].weights = Tensor(weights[0])
layers[2].weights = Tensor(weights[1])
nn = NeuralNetwork(DefaultInputLayer(), layers)
out_tensors = nn.forward(input)
assert np.all(np.abs(out_tensors[-1].get_elements() - exp_outs.get_exp_forward_out()) < 1e-7)
print("Forward pass is correct for the given example!")
SGD.update(
nn=nn,
loss=CrossEntropy(),
lr=0.01,
epochs=1,
data=(np.array([input]), np.ones((1, 1, 8))),
)
assert np.all(np.abs(nn.layers[0].bias.get_elements() - exp_outs.get_exp_updated_bias_1()) < 1e-7)
assert np.all(np.abs(nn.layers[0].weights.get_elements() - exp_outs.get_exp_updated_weights_1()) < 1e-7)
assert np.all(np.abs(nn.layers[2].bias.get_elements() - exp_outs.get_exp_updated_bias_2()) < 1e-7)
assert np.all(np.abs(nn.layers[2].weights.get_elements() - exp_outs.get_exp_updated_weights_2()) < 1e-7)
print("Weight and Bias gradients are correct for the given example!")
def test_one_hot_conversion():
class_names = ["fish", "frog", "bird", "kangaroo"]
y_s = np.array(["fish", "bird", "bird", "kangaroo", "frog"])
exp_result = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
assert np.all(classes_to_one_hot_vector(y_s, class_names) == exp_result)
print("Conversion from class_names to one hot vector was successful!")
assert np.all(one_hot_vector_to_classes(exp_result, class_names, sparse=False) == y_s)
assert np.all(one_hot_vector_to_classes(np.argmax(exp_result, axis=1), class_names) == y_s)
print("Conversion from (sparse) one hot vector to class names was successful!")