-
Notifications
You must be signed in to change notification settings - Fork 1
/
nn.py
171 lines (132 loc) · 6.16 KB
/
nn.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
"""
Algorithm is based on Tom M. Mitchell - Machine Learning.
The book explains neural networks and the backpropagation algorithm.
"""
import numpy as np
np.random.seed(44)
class FCLayer():
""" Represents a fully connected layer. """
def __init__(self, name, input_size, output_size):
self.weights = (np.random.randn(input_size, output_size))/10.0
self.biases = np.zeros(output_size)
self.name = name
def forward(self, input_data):
return self.activation(np.dot(input_data, self.weights) + self.biases)
def activation(self, vector):
return np.tanh(vector)
def activation_derivative(self, vector):
return 1.0 - np.multiply(np.tanh(vector), np.tanh(vector))
class NN():
""" Represents a Neural network, in this case consisting of \
fully connected layers. """
def __init__(self, layers):
self._layers = layers
def predict(self, X):
""" Perform inference on X. """
ys = []
for x in X:
inx = x
for layer in self._layers:
inx = layer.forward(inx)
ys.append(inx.copy())
return ys
def metric(self, prediction, truth):
return np.linalg.norm(prediction - truth)
def train(self, learning_rate, max_epochs, x_train, y_train, x_val, y_val,
batch_size=1, momentum=0.6, shuffle=True, threshold=0.03):
""" Train using mini-batch gradient descent with momentum. """
# we're storing metrics to return them
mse_train = []
mse_val = []
print("starting training...")
# Iterate over epochs
for epoch in range(max_epochs):
# check we're above the threshold
if len(mse_val) > 0 and mse_val[-1] <= threshold:
break
# every 10 epochs, print metrics
if epoch % 10 == 0 and len(mse_val) > 0:
print(f"epoch: {epoch}, train RMSE: {mse_train[-1]:.3E}, \
val RMSE: {mse_val[-1]:.3E}")
# we store the metrics for every batch in this array
epoch_train_mse = []
# shuffle the data for each epoch
if shuffle:
indices = np.arange(len(x_train))
np.random.shuffle(indices)
x = x_train[indices]
y = y_train[indices]
else:
x = x_train
y = y_train
# initialize velocities for momentum
velocities_weights = [np.zeros_like(layer.weights) for layer
in self._layers]
velocities_biases = [np.zeros_like(layer.biases) for layer
in self._layers]
# Iterate over batches
for i in range(0, len(x), batch_size):
X_batch = x[i:i + batch_size]
Y_batch = y[i:i + batch_size]
batch_train_mse = []
# cccumulate gradients over the batch
accum_gradients = [np.zeros_like(layer.weights) for layer
in self._layers]
accum_biases = [np.zeros_like(layer.biases) for layer
in self._layers]
# for each sample in the batch
for x_s, y_s in zip(X_batch, Y_batch):
outputs = []
inputs = []
input_data = x_s.copy()
# Forward pass
for layer in self._layers:
inputs.append(input_data.copy())
outputs.append(layer.forward(inputs[-1]))
input_data = outputs[-1]
deltas = []
# add the error to this batch's metrics
batch_train_mse.append(self.metric(outputs[-1], y_s))
# backward pass
output_error = y_s - outputs[-1]
delta = self._layers[-1].\
activation_derivative(outputs[-1]) * output_error
deltas.append(delta.copy())
layer_index = len(self._layers) - 2
while layer_index >= 0:
sigma = np.dot(deltas[-1], self._layers
[layer_index + 1].weights.transpose())
delta = self._layers[layer_index].\
activation_derivative(outputs[layer_index])
delta = np.multiply(delta, sigma)
deltas.append(delta.copy())
layer_index -= 1
deltas.reverse()
# accumulate gradients
for index, layer in enumerate(self._layers):
accum_gradients[index] += np.outer(inputs[index],
deltas[index])
accum_biases[index] += learning_rate * deltas[index].\
reshape(-1)
# update weights and biases after processing the batch with
# momentum
for index, layer in enumerate(self._layers):
velocities_weights[index] = momentum * \
velocities_weights[index] + \
learning_rate * (accum_gradients[index] / batch_size)
velocities_biases[index] = momentum * \
velocities_biases[index] + \
learning_rate * (accum_biases[index] / batch_size)
layer.weights = layer.weights + velocities_weights[index]
layer.biases = layer.biases + velocities_biases[index]
# add the metrics values for this batch
epoch_train_mse.append(np.mean(batch_train_mse))
# add this epoch's error for training set
mse_train.append(np.mean(epoch_train_mse))
# add the validation error
y_val_pred = self.predict(x_val)
temp = []
for a, b in zip(y_val, y_val_pred):
temp.append(self.metric(a, b))
mse_val.append(np.mean(temp))
return mse_train, mse_val