forked from eriklindernoren/ML-From-Scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.py
263 lines (208 loc) · 8.7 KB
/
neural_network.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
from __future__ import print_function
from sklearn import datasets
from terminaltables import AsciiTable
import sys
import os
import math
import copy
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import progressbar
# Import helper functions
from mlfromscratch.utils.data_manipulation import train_test_split, to_categorical, normalize
from mlfromscratch.utils.data_manipulation import get_random_subsets, shuffle_data, normalize
from mlfromscratch.utils.data_operation import accuracy_score
from mlfromscratch.utils.optimizers import GradientDescent, Adam, RMSprop, Adagrad, Adadelta
from mlfromscratch.utils.loss_functions import CrossEntropy, SquareLoss
from mlfromscratch.unsupervised_learning import PCA
from mlfromscratch.utils.misc import bar_widgets
from mlfromscratch.utils import Plot
from mlfromscratch.utils.layers import Dense, Dropout, Conv2D, Flatten, Activation, MaxPooling2D
from mlfromscratch.utils.layers import AveragePooling2D, ZeroPadding2D, BatchNormalization
class NeuralNetwork():
"""Neural Network.
Parameters:
-----------
optimizer: class
The weight optimizer that will be used to tune the weights in order of minimizing
the loss.
loss: class
Loss function used to measure the model's performance. SquareLoss or CrossEntropy.
validation: tuple
A tuple containing validation data and labels
"""
def __init__(self, optimizer, loss=CrossEntropy, validation_data=None):
self.optimizer = optimizer
self.layers = []
self.errors = {"training": [], "validation": []}
self.loss_function = loss()
self.validation_set = False
if validation_data:
self.validation_set = True
self.X_val, self.y_val = validation_data
# One-hot encoding of y
self.y_val = to_categorical(self.y_val.astype("int"))
# Method which enables freezing of the weights of the network's layers.
def set_trainable(self, trainable):
for layer in self.layers:
layer.trainable = trainable
def add(self, layer):
""" Method which adds a layer to the neural network """
# If this is not the first layer added then set the input shape
# as the output shape of the last added layer
if self.layers:
layer.set_input_shape(shape=self.layers[-1].output_shape())
# If the layer has weights that needs to be initialized
if hasattr(layer, 'initialize'):
layer.initialize(optimizer=self.optimizer)
# Add layer to the network
self.layers.append(layer)
def train_on_batch(self, X, y):
# Calculate output
y_pred = self._forward_pass(X)
# Calculate the training loss
loss = np.mean(self.loss_function.loss(y, y_pred))
# Calculate the gradient of the loss function wrt y_pred
loss_grad = self.loss_function.gradient(y, y_pred)
# Calculate the accuracy of the prediction
acc = self.loss_function.acc(y, y_pred)
# Backprop. Update weights
self._backward_pass(loss_grad=loss_grad)
return loss, acc
def fit(self, X, y, n_epochs, batch_size):
# Convert to one-hot encoding
y = to_categorical(y.astype("int"))
n_samples = np.shape(X)[0]
n_batches = int(n_samples / batch_size)
bar = progressbar.ProgressBar(widgets=bar_widgets)
for _ in bar(range(n_epochs)):
idx = range(n_samples)
np.random.shuffle(idx)
batch_t_error = 0 # Mean batch training error
for i in range(n_batches):
X_batch = X[idx[i*batch_size:(i+1)*batch_size]]
y_batch = y[idx[i*batch_size:(i+1)*batch_size]]
loss, _ = self.train_on_batch(X_batch, y_batch)
batch_t_error += loss
# Save the epoch mean error
self.errors["training"].append(batch_t_error / n_batches)
if self.validation_set:
# Determine validation error
y_val_p = self._forward_pass(self.X_val)
validation_loss = np.mean(self.loss_function.loss(self.y_val, y_val_p))
self.errors["validation"].append(validation_loss)
return self.errors["training"], self.errors["validation"]
def _forward_pass(self, X, training=True):
""" Calculate the output of the NN. The output of layer l1 becomes the
input of the following layer l2 """
layer_output = X
for layer in self.layers:
layer_output = layer.forward_pass(layer_output, training)
return layer_output
def _backward_pass(self, loss_grad):
""" Propogate the gradient 'backwards' and update the weights
in each layer. The output of l2 becomes the input of l1. """
acc_grad = loss_grad
for layer in reversed(self.layers):
acc_grad = layer.backward_pass(acc_grad)
def summary(self, name="Model Summary"):
# Print model name
print (AsciiTable([[name]]).table)
# Network input shape (first layer's input shape)
print ("Input Shape: %s" % str(self.layers[0].input_shape))
# Get each layer's configuration
table_data = [["Layer Type", "Parameters", "Output Shape"]]
tot_params = 0
for layer in self.layers:
layer_name = layer.layer_name()
params = layer.parameters()
out_shape = layer.output_shape()
table_data.append([layer_name, str(params), str(out_shape)])
tot_params += params
# Print network configuration table
print (AsciiTable(table_data).table)
print ("Total Parameters: %d\n" % tot_params)
def predict(self, X):
""" Use the trained model to predict labels of X """
return self._forward_pass(X, training=False)
def main():
data = datasets.load_digits()
X = data.data
y = data.target
n_samples = np.shape(X)
n_hidden = 512
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, seed=1)
optimizer = Adam()
#-----
# MLP
#-----
# clf = NeuralNetwork(optimizer=optimizer,
# loss=CrossEntropy,
# validation_data=(X_test, y_test))
# clf.add(Dense(n_hidden, input_shape=(8*8,)))
# clf.add(Activation('leaky_relu'))
# clf.add(Dense(n_hidden))
# clf.add(Activation('leaky_relu'))
# clf.add(Dropout(0.25))
# clf.add(Dense(n_hidden))
# clf.add(Activation('leaky_relu'))
# clf.add(Dropout(0.25))
# clf.add(Dense(n_hidden))
# clf.add(Activation('leaky_relu'))
# clf.add(Dropout(0.25))
# clf.add(Dense(10))
# clf.add(Activation('softmax'))
# print ()
# clf.summary(name="MLP")
# clf.fit(X_train, y_train, n_epochs=50, batch_size=256)
# clf.plot_errors()
# y_pred = np.argmax(clf.predict(X_test), axis=1)
# accuracy = accuracy_score(y_test, y_pred)
# print ("Accuracy:", accuracy)
#----------
# Conv Net
#----------
# Reshape X to (n_samples, channels, height, width)
X_train = X_train.reshape((-1,1,8,8))
X_test = X_test.reshape((-1,1,8,8))
clf = NeuralNetwork(optimizer=optimizer,
loss=CrossEntropy,
validation_data=(X_test, y_test))
clf.add(Conv2D(n_filters=16, filter_shape=(3,3), input_shape=(1,8,8), padding='same'))
clf.add(Activation('relu'))
clf.add(Dropout(0.25))
clf.add(BatchNormalization())
clf.add(Conv2D(n_filters=32, filter_shape=(3,3), padding='same'))
clf.add(Activation('relu'))
clf.add(Dropout(0.25))
clf.add(BatchNormalization())
clf.add(Flatten())
clf.add(Dense(256))
clf.add(Activation('relu'))
clf.add(Dropout(0.5))
clf.add(BatchNormalization())
clf.add(Dense(10))
clf.add(Activation('softmax'))
print ()
clf.summary(name="ConvNet")
train_err, val_err = clf.fit(X_train, y_train, n_epochs=50, batch_size=256)
# Training and validation error plot
n = len(train_err)
training, = plt.plot(range(n), train_err, label="Training Error")
validation, = plt.plot(range(n), val_err, label="Validation Error")
plt.legend(handles=[training, validation])
plt.title("Error Plot")
plt.ylabel('Error')
plt.xlabel('Iterations')
plt.show()
# Predict labels of the test data
y_pred = np.argmax(clf.predict(X_test), axis=1)
accuracy = accuracy_score(y_test, y_pred)
print ("Accuracy:", accuracy)
# Flatten data set
X_test = X_test.reshape(-1, 8*8)
# Reduce dimension to 2D using PCA and plot the results
Plot().plot_in_2d(X_test, y_pred, title="Convolutional Neural Network", accuracy=accuracy, legend_labels=np.unique(y))
if __name__ == "__main__":
main()