-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLP_classes.py
382 lines (293 loc) · 13.1 KB
/
MLP_classes.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
379
380
381
382
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 22 17:27:55 2017
@author: bettmensch
"""
import numpy as np
import math
import dill
import os
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
def L_2_norm(matrix):
"""Takes a matrix and calculates its L^2 norm.
Returns the matrix's L^2 norm."""
return np.linalg.norm(matrix)
def sigmoid_mat(matrix):
"""Takes a matrix and applies the sigmoid function element_wise.
Returns transformed matrix."""
return np.divide(1, 1 + np.exp(-matrix))
def D_sigmoid_mat(matrix):
"""Takes a matrix and applies the sigmoid function's derivative element_wise.
Returns transformed matrix."""
return np.multiply(sigmoid_mat(matrix), 1 - sigmoid_mat(matrix))
def sigmoid_entropy(t,y):
"""Takes a target vector and a prediction vector y and calculates the
sigmoidal cross entropy.
Returns the sigmoidal cross entropy (scalar)."""
return np.sum(-np.multiply(t, np.log(y)) - np.multiply(1 - t, np.log(1 - y)))
def sigmoid_entropy_mat(T,Y):
"""Takes a matrix T of target row vectors and a matrix Y of prediction row
vectors and calculates the sigmoidal cross entropy for each row vector pair.
Returns the total sigmoidal cross entropy (scalar)."""
m = T.shape[0]
return 1 / m * sum([sigmoid_entropy(t,y) for t,y in zip(T,Y)])
def tanh_mat(matrix):
"""Takes a matrix and applies the tanh function element_wise.
Returns transformed matrix."""
return np.tanh(matrix)
def D_tanh_mat(matrix):
"""Takes a matrix and applies the tanh function's derivative element_wise.
Returns transformed matrix."""
return 1 - np.multiply(tanh_mat(matrix), tanh_mat(matrix))
def softmax(vector):
"""Takes a vector and applies the softmax.
Returns transformed vector."""
return np.exp(vector) / np.sum(np.exp(vector))
def softmax_mat(matrix):
"""Takes a matrix and applies the softmax to each row.
Returns transformed matrix."""
return np.matrix(np.vstack([softmax(row) for row in matrix]))
def D_softmax_mat(matrix):
""""""
pass
def softmax_entropy(t,y):
"""Takes a target vector t and a prediction vector y and calculates the
cross entropy.
Returns the cross entropy (scalar)."""
return np.sum(-np.multiply(t,np.log(y)))
def softmax_entropy_mat(T,Y):
"""Takes a matrix T where "one row = one target label".
Takes a matrix Y where "one row = one predicted label".
Returns the total cross entropy for a softmax output layer."""
m = T.shape[0]
return 1 / m * sum([softmax_entropy(t,y) for t,y in zip(T,Y)])
def relu_mat(matrix):
""""""
pass
def D_relu_mat(matrix):
""""""
pass
class Layer(object):
"""Multifunctional layer class.
Will be attached to the model during model initialization."""
def __init__(self, size, activation):
self.size = size
self.type = activation
self.A = None
self.B = None
# by default, layer is a hidden layer
self.first = False
self.last = False
if activation == 'sigmoid':
self.activate = sigmoid_mat
self.D_activate = D_sigmoid_mat
elif activation == 'tanh':
self.activate = tanh_mat
self.D_activate = D_tanh_mat
elif activation == 'softmax':
self.activate = softmax_mat
self.D_activate = D_softmax_mat
#elif activation == 'relu':
# self.activate = relu_mat
# self.D_activate = D_relu_mat
elif activation == 'softmax':
self.activate = softmax_mat
self.D_activate = D_softmax_mat
def __str__(self):
"""The object as it presents itself to the console."""
bio = "Size: " + str(self.size) + "\n" + "Type: " + str(self.type) + "\n"
return bio
def fix(self, position):
"""Takes a layer position within model (type string).
Updates models settings accordingly."""
if position == 'first':
self.activate = None
self.D_activate = None
self.first = True
elif position == 'last':
self.D_activate = None
self.last = True
class MLP(object):
"""MultiLayerPerceptron class"""
def __init__(self):
"""Takes an iterable layers containing the layer_sizes. The length of
the iterable determines the depth of the MLP."""
self.layers = []
self.lb = LabelBinarizer()
self.loss_function = None
self.Ws = None
self.fs = None
def add_layer(self, size, activation = 'tanh'):
"""Takes a layer size (type int) and an activation (type string).
Adds a layer object to the blue_print attribute."""
self.layers.append(Layer(size, activation))
def fix(self):
"""Marks the end of the model design process.
Sets the input and output layers and the loss function. Model training
can begin after this method is called."""
first_layer = self.layers[0]
first_layer.fix('first')
last_layer = self.layers[-1]
if last_layer.type == 'softmax':
last_layer.fix('last')
self.loss_function = softmax_entropy_mat
elif last_layer.type== 'sigmoid':
last_layer.fix('last')
self.loss_function = sigmoid_entropy_mat
else:
print("Last layer must be either sigmoid or softmax!")
def fit(self, X_train, y_train,
batch_size = 100, max_iter = 700,
learning_rate = 0.3, reg_param = 0):
"""Takes training data X_train (type array or matrix).
Takes training labels (type list, array or matrix) in categorical format.
Takes maximum number of iterations max_iter (type int).
Takes regularization parameter reg_param (type float)."""
m = X_train.shape[0]
Ws, fs = self.init_params(batch_size)
Y_train = self.lb.fit_transform(y_train)
for i in range(max_iter):
# create batch for current forward-backward-loss calculation
idx = np.random.randint(m, size = batch_size)
X_batch = X_train[idx,:]
Y_batch = Y_train[idx,:]
L_i = self.forward_prop(X = X_batch, Y = Y_batch, Ws = Ws, fs = fs, mode = 'fit') \
+ self.reg_term(m, Ws, reg_param)
print("Iteration ", i)
print("Loss: ", L_i)
DWs, Dfs = self.backward_prop(Y_batch, Ws, fs)
Ws = [(1 - (reg_param * learning_rate) / m) * W - learning_rate * DW for W, DW in zip(Ws, DWs)]
fs = [f - learning_rate * Df for f, Df in zip(fs, Dfs)]
self.Ws = Ws
self.fs = [f[0,:] for f in fs]
def reg_term(self, m, Ws, reg_param = 0):
"""Takes a batch size m (type int).
Takes weights Ws (type iterable, contains weight matrices).
Takes regularization parameter reg_param (type float).
Returns regularization penalty term (type float)."""
if reg_param:
reg_term = reg_param / (2*m) * sum([np.linalg.norm(W) for W in Ws])
return reg_term
else:
return 0
def transform(self, X):
"""Takes data (type array or matrix).
Returns class predictions (type array or list)."""
m = X.shape[0]
if self.Ws:
Ws = self.Ws
fs = [np.matrix(np.vstack([f for i in range(m)])) for f in self.fs]
y_pred = self.forward_prop(X = X, Ws = Ws, fs = fs, mode = 'transform')
return y_pred
else:
print("Model needs to be fitted first!")
def fit_transform(self, X_train, y_train,
batch_size = 100, max_iter = 700,
learning_rate = 0.3, reg_param = 0):
"""Takes training data X_train (type array or matrix).
Takes training labels (type list, array or matrix) in categorical format.
Takes maximum number of iterations max_iter (type int).
Takes regularization parameter reg_param (type float)."""
self.fit(X_train, y_train, batch_size, max_iter, learning_rate, reg_param)
y_train_pred = self.transform(X_train)
return y_train_pred
def evaluate(self, X_test, y_test):
"""Takes a matrix of test samples where "one row = one sample".
Takes a matrix of test labels where "one row = one label".
Calculates predictions and evaluates accuracy based on predictions
and test labels.
Returns an accuracy score."""
# calculate model predictions
y_pred = self.transform(X = X_test)
print(confusion_matrix(y_test, y_pred, labels = self.lb.classes_))
print(classification_report(y_test, y_pred, labels = self.lb.classes_))
def init_params(self, m):
"""Randomly initializes weights Ws (type list, contains matrices) and
biases fs (type list, contains matrices)."""
layer_sizes = [layer.size for layer in self.layers]
Ws, fs = [], []
for i in range(len(layer_sizes)-1):
rows, columns = layer_sizes[i], layer_sizes[i+1]
epsilon = math.sqrt(6) / math.sqrt(rows + columns)
high = epsilon * np.matrix(np.ones((rows, columns)))
low = - high
Ws.append(np.matrix(np.random.uniform(low, high)))
fs.append(np.matrix(np.ones((m, columns))))
return Ws, fs
def forward_prop(self, X, Ws, fs, mode, Y = None):
"""Takes samples X (type matrix) and labels Y (type matrix).
Takes weights Ws (type list, containing matrices).
Takes biases fs (type list, contains matrices).
Takes propagation mode (type string).
Returns loss function value L_i (type float) if in fit mode.
Returns model prediction P (type matrix) if in transform mode."""
first_layer = self.layers[0]
first_layer.B = X
for i in range(len(Ws)):
current_layer = self.layers[i+1]
previous_layer = self.layers[i]
current_layer.A = np.dot(previous_layer.B, Ws[i]) + fs[i]
current_layer.B = current_layer.activate(current_layer.A)
last_layer = self.layers[-1]
P = last_layer.B
if mode == 'transform':
y_pred = self.lb.inverse_transform(P)
return y_pred
elif mode == 'fit':
L_i = self.loss_function(Y, P)
return L_i
def backward_prop(self, Y, Ws, learning_rate):
"""Takes lables Y (type matrix).
Takes weights Ws (type list, containing matrices).
Takes biases fs (type list, contains matrices).
Applies backpropagation.
Returns updated weights Ws (type list, contains matrices).
Returns updated biases fs (type list, contains matrices)."""
m = Y.shape[0]
DWs, Dfs = [], []
last_layer = self.layers[-1]
P = last_layer.B
Delta = (P - Y) / m
for i in range(len(Ws),0,-1):
previous_layer = self.layers[i-1]
DW = np.dot(previous_layer.B.T, Delta)
Df = np.matrix(np.vstack([np.sum(Delta,0) for i in range(m)]))
DWs.append(DW)
Dfs.append(Df)
if i != 1:
Delta = np.multiply(np.dot(Delta, Ws[i-1].T),
previous_layer.D_activate(previous_layer.A))
DWs.reverse()
Dfs.reverse()
return DWs, Dfs
def __str__(self):
"""The way the model presents itself to the console."""
bio = ""
for layer in self.layers:
if layer.first:
bio += "Input Layer \n"
elif layer.last:
bio += "Output layer \n"
else:
bio += "Hidden Layer \n"
bio += layer.__str__() + "------------\n"
return bio
def save(self, file_name, directory):
"""Uses dill to save current model to disk at the specified location."""
work_dir = os.getcwd()
os.chdir(directory)
with open(file_name, 'wb') as dump_file:
dill.dump(self, dump_file)
os.chdir(work_dir)
def clone(self):
"""Returns a copy of current model."""
clone = MLP()
clone.layers = self.layers
clone.lb = self.lb
clone.loss_function = self.loss_function
clone.Ws = self.Ws
clone.fs = self.fs
return clone