-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathnn.py
378 lines (317 loc) · 13.9 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# -*- coding: utf-8 -*-
# Author: Ahmed BESBES
# <ahmed.besbes@hotmail.com>
#
# matplotlib for plotting
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 10.0)
from matplotlib import pyplot as plt
# numpy for vector and matrix manipulations
import numpy as np
# we won't use scikit per se but we'll use some help functions in it
# such as accuracy_score, shuffle, and train_test_split
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
# tqdm is progress-bar. make sure it's installed: pip install tqdm
from tqdm import tqdm
from IPython import display
def activation(z, derivative=False):
"""
Sigmoid activation function:
It handles two modes: normal and derivative mode.
Applies a pointwize operation on vectors
Parameters:
---
z: pre-activation vector at layer l
shape (n[l], batch_size)
Returns:
pontwize activation on each element of the input z
"""
if derivative:
return activation(z) * (1 - activation(z))
else:
return 1 / (1 + np.exp(-z))
def cost_function(y_true, y_pred):
"""
Computes the Mean Square Error between a ground truth vector and a prediction vector
Parameters:
---
y_true: ground-truth vector
y_pred: prediction vector
Returns:
---
cost: a scalar value representing the loss
"""
n = y_pred.shape[1]
cost = (1./(2*n)) * np.sum((y_true - y_pred) ** 2)
return cost
def cost_function_prime(y_true, y_pred):
"""
Computes the derivative of the loss function w.r.t the activation of the output layer
Parameters:
---
y_true: ground-truth vector
y_pred: prediction vector
Returns:
---
cost_prime: derivative of the loss w.r.t. the activation of the output
shape: (n[L], batch_size)
"""
cost_prime = y_pred - y_true
return cost_prime
class NeuralNetwork(object):
'''
This is a custom neural netwok package built from scratch with numpy.
It allows training using SGD, inference and live plotting of the decision boundary.
This code is not optimized and should not be used with real-world examples.
It's written for educational purposes only.
The Neural Network as well as its parameters and training method and procedure will
reside in this class.
Parameters
---
size: list of number of neurons per layer
Examples
---
>>> import NeuralNetwork
>>> nn = NeuralNetword([2, 3, 4, 1])
This means :
1 input layer with 2 neurons
1 hidden layer with 3 neurons
1 hidden layer with 4 neurons
1 output layer with 1 neuron
'''
def __init__(self, size, seed=42):
'''
Instantiate the weights and biases of the network
weights and biases are attributes of the NeuralNetwork class
They are updated during the training
'''
self.seed = seed
np.random.seed(self.seed)
self.size = size
self.weights = [np.random.randn(self.size[i], self.size[i-1]) * np.sqrt(1 / self.size[i-1]) for i in range(1, len(self.size))]
self.biases = [np.random.rand(n, 1) for n in self.size[1:]]
def forward(self, input):
'''
Perform a feed forward computation
Parameters
---
input: data to be fed to the network with
shape: (input_shape, batch_size)
Returns
---
a: ouptut activation (output_shape, batch_size)
pre_activations: list of pre-activations per layer
each of shape (n[l], batch_size), where n[l] is the number
of neuron at layer l
activations: list of activations per layer
each of shape (n[l], batch_size), where n[l] is the number
of neuron at layer l
'''
a = input
pre_activations = []
activations = [a]
for w, b in zip(self.weights, self.biases):
z = np.dot(w, a) + b
a = activation(z)
pre_activations.append(z)
activations.append(a)
return a, pre_activations, activations
def compute_deltas(self, pre_activations, y_true, y_pred):
"""
Computes a list containing the values of delta for each layer using
a recursion
Parameters:
---
pre_activations: list of of pre-activations. each corresponding to a layer
y_true: ground truth values of the labels
y_pred: prediction values of the labels
Returns:
---
deltas: a list of deltas per layer
"""
delta_L = cost_function_prime(y_true, y_pred) * activation(pre_activations[-1], derivative=True)
deltas = [0] * (len(self.size) - 1)
deltas[-1] = delta_L
for l in range(len(deltas) - 2, -1, -1):
delta = np.dot(self.weights[l + 1].transpose(), deltas[l + 1]) * activation(pre_activations[l], derivative=True)
deltas[l] = delta
return deltas
def backpropagate(self, deltas, pre_activations, activations):
"""
Applies back-propagation and computes the gradient of the loss
w.r.t the weights and biases of the network
Parameters:
---
deltas: list of deltas computed by compute_deltas
pre_activations: a list of pre-activations per layer
activations: a list of activations per layer
Returns:
---
dW: list of gradients w.r.t. the weight matrices of the network
db: list of gradients w.r.t. the biases (vectors) of the network
"""
dW = []
db = []
deltas = [0] + deltas
for l in range(1, len(self.size)):
dW_l = np.dot(deltas[l], activations[l-1].transpose())
db_l = deltas[l]
dW.append(dW_l)
db.append(np.expand_dims(db_l.mean(axis=1), 1))
return dW, db
def plot_decision_regions(self, X, y, iteration, train_loss, val_loss, train_acc, val_acc, res=0.01):
"""
Plots the decision boundary at each iteration (i.e. epoch) in order to inspect the performance
of the model
Parameters:
---
X: the input data
y: the labels
iteration: the epoch number
train_loss: value of the training loss
val_loss: value of the validation loss
train_acc: value of the training accuracy
val_acc: value of the validation accuracy
res: resolution of the plot
Returns:
---
None: this function plots the decision boundary
"""
X, y = X.T, y.T
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, res),
np.arange(y_min, y_max, res))
Z = self.predict(np.c_[xx.ravel(), yy.ravel()].T)
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.5)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.scatter(X[:, 0], X[:, 1], c=y.reshape(-1), alpha=0.2)
message = 'iteration: {} | train loss: {} | val loss: {} | train acc: {} | val acc: {}'.format(iteration,
train_loss,
val_loss,
train_acc,
val_acc)
plt.title(message)
def train(self, X, y, batch_size, epochs, learning_rate, validation_split=0.2, print_every=10, tqdm_=True, plot_every=None):
"""
Trains the network using the gradients computed by back-propagation
Splits the data in train and validation splits
Processes the training data by batches and trains the network using batch gradient descent
Parameters:
---
X: input data
y: input labels
batch_size: number of data points to process in each batch
epochs: number of epochs for the training
learning_rate: value of the learning rate
validation_split: percentage of the data for validation
print_every: the number of epochs by which the network logs the loss and accuracy metrics for train and validations splits
tqdm_: use tqdm progress-bar
plot_every: the number of epochs by which the network plots the decision boundary
Returns:
---
history: dictionary of train and validation metrics per epoch
train_acc: train accuracy
test_acc: validation accuracy
train_loss: train loss
test_loss: validation loss
This history is used to plot the performance of the model
"""
history_train_losses = []
history_train_accuracies = []
history_test_losses = []
history_test_accuracies = []
x_train, x_test, y_train, y_test = train_test_split(X.T, y.T, test_size=validation_split, )
x_train, x_test, y_train, y_test = x_train.T, x_test.T, y_train.T, y_test.T
if tqdm_:
epoch_iterator = tqdm(range(epochs))
else:
epoch_iterator = range(epochs)
for e in epoch_iterator:
if x_train.shape[1] % batch_size == 0:
n_batches = int(x_train.shape[1] / batch_size)
else:
n_batches = int(x_train.shape[1] / batch_size ) - 1
x_train, y_train = shuffle(x_train.T, y_train.T)
x_train, y_train = x_train.T, y_train.T
batches_x = [x_train[:, batch_size*i:batch_size*(i+1)] for i in range(0, n_batches)]
batches_y = [y_train[:, batch_size*i:batch_size*(i+1)] for i in range(0, n_batches)]
train_losses = []
train_accuracies = []
test_losses = []
test_accuracies = []
dw_per_epoch = [np.zeros(w.shape) for w in self.weights]
db_per_epoch = [np.zeros(b.shape) for b in self.biases]
for batch_x, batch_y in zip(batches_x, batches_y):
batch_y_pred, pre_activations, activations = self.forward(batch_x)
deltas = self.compute_deltas(pre_activations, batch_y, batch_y_pred)
dW, db = self.backpropagate(deltas, pre_activations, activations)
for i, (dw_i, db_i) in enumerate(zip(dW, db)):
dw_per_epoch[i] += dw_i / batch_size
db_per_epoch[i] += db_i / batch_size
batch_y_train_pred = self.predict(batch_x)
train_loss = cost_function(batch_y, batch_y_train_pred)
train_losses.append(train_loss)
train_accuracy = accuracy_score(batch_y.T, batch_y_train_pred.T)
train_accuracies.append(train_accuracy)
batch_y_test_pred = self.predict(x_test)
test_loss = cost_function(y_test, batch_y_test_pred)
test_losses.append(test_loss)
test_accuracy = accuracy_score(y_test.T, batch_y_test_pred.T)
test_accuracies.append(test_accuracy)
# weight update
for i, (dw_epoch, db_epoch) in enumerate(zip(dw_per_epoch, db_per_epoch)):
self.weights[i] = self.weights[i] - learning_rate * dw_epoch
self.biases[i] = self.biases[i] - learning_rate * db_epoch
history_train_losses.append(np.mean(train_losses))
history_train_accuracies.append(np.mean(train_accuracies))
history_test_losses.append(np.mean(test_losses))
history_test_accuracies.append(np.mean(test_accuracies))
if not plot_every:
if e % print_every == 0:
print('Epoch {} / {} | train loss: {} | train accuracy: {} | val loss : {} | val accuracy : {} '.format(
e, epochs, np.round(np.mean(train_losses), 3), np.round(np.mean(train_accuracies), 3),
np.round(np.mean(test_losses), 3), np.round(np.mean(test_accuracies), 3)))
else:
if e % plot_every == 0:
self.plot_decision_regions(x_train, y_train, e,
np.round(np.mean(train_losses), 4),
np.round(np.mean(test_losses), 4),
np.round(np.mean(train_accuracies), 4),
np.round(np.mean(test_accuracies), 4),
)
plt.show()
display.display(plt.gcf())
display.clear_output(wait=True)
self.plot_decision_regions(X, y, e,
np.round(np.mean(train_losses), 4),
np.round(np.mean(test_losses), 4),
np.round(np.mean(train_accuracies), 4),
np.round(np.mean(test_accuracies), 4),
)
history = {'epochs': epochs,
'train_loss': history_train_losses,
'train_acc': history_train_accuracies,
'test_loss': history_test_losses,
'test_acc': history_test_accuracies
}
return history
def predict(self, a):
'''
Use the current state of the network to make predictions
Parameters:
---
a: input data, shape: (input_shape, batch_size)
Returns:
---
predictions: vector of output predictions
'''
for w, b in zip(self.weights, self.biases):
z = np.dot(w, a) + b
a = activation(z)
predictions = (a > 0.5).astype(int)
return predictions