-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_mlp.py
More file actions
executable file
·269 lines (219 loc) · 9.59 KB
/
functions_mlp.py
File metadata and controls
executable file
·269 lines (219 loc) · 9.59 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 19 08:46:58 2021
@author: joelmcfarlane
Building a multi_layer_perceptron from the ground up.
"""
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import utils
sns.set()
class Network:
"""
We will use a neural network to try and create a model that can read
numbers.
Images are 28x28 and range in brightness from 0 to 255.
"""
def __init__(self, list_nodes: list = [20, 10], l_rate: float = 10, act_list: list = ['sigmoid', 'softmax'],
epochs: int = 5):
self.learning_rate = l_rate
self.list_nodes = list_nodes
self.activation_list = act_list
self.epochs = epochs
self.func_dict = {'sigmoid': [self.sigmoid, self.sig_deriv],
'softmax': [self.softmax, self.linear_deriv], # Used linear deriv as applies here also.
'tanh': [self.tanh, self.tanh_deriv],
'relu': [self.relu, self.relu_deriv],
'linear': [self.linear, self.linear_deriv]}
if len(self.list_nodes) != len(self.activation_list):
raise Exception('Activation list length not equal to size of neural network.')
self.read_data()
self.create_network()
def read_data(self):
"""
Read in the data from the csv files that should be saved in the same directory as this file.
"""
self.test_matrix, self.test_labels = utils.read_data(csv_path='mnist_test.csv')
self.train_matrix, self.train_labels = utils.read_data(csv_path='mnist_train.csv')
def create_network(self):
"""
Automatically create the weights and biases from self.list_nodes.
"""
self.w = {}
self.b = {}
n_last = 784
for i in range(len(self.list_nodes)):
self.w['w' + str(i + 1)] = np.random.randn(self.list_nodes[i], n_last)
self.b['b' + str(i + 1)] = np.random.randn(self.list_nodes[i])
n_last = self.list_nodes[i]
def feedforward(self, image_data: np.ndarray) -> dict:
res = {}
res['a0'] = image_data
for i in range(len(self.list_nodes)):
n = i + 1
res['z' + str(n)] = np.dot(self.w['w' + str(n)], res['a' + str(n - 1)]) + self.b['b' + str(n)]
act_func = self.func_dict[self.activation_list[i]][0]
res['a' + str(n)] = act_func(res['z' + str(n)])
return res
@staticmethod
def sigmoid(x_array):
return 1 / (1 + np.exp(-x_array))
def sig_deriv(self, x_array: np.ndarray) -> np.ndarray:
out = self.sigmoid(x_array) * (1 - self.sigmoid(x_array))
return out
@staticmethod
def linear(x_array: np.ndarray) -> np.ndarray:
return x_array
@staticmethod
def linear_deriv(x_array: np.ndarray) -> np.ndarray:
return np.ones(len(x_array))
@staticmethod
def tanh(x_array: np.ndarray) -> np.ndarray:
return np.tanh(x_array)
@staticmethod
def tanh_deriv(x_array: np.ndarray) -> np.ndarray:
return 1 - np.tanh(x_array) ** 2
@staticmethod
def softmax(x_array: np.ndarray) -> np.ndarray:
out = np.exp(x_array) / sum(np.exp(x_array))
return out
@staticmethod
def relu(x_array: np.ndarray) -> np.ndarray:
out = np.maximum(0, x_array)
return out
@staticmethod
def relu_deriv(x_array: np.ndarray) -> np.ndarray:
out = np.heaviside(x_array, 0)
return out
@staticmethod
def one_hot(label_vec: np.ndarray) -> np.ndarray:
"""
One hot encode the labels to make use of them easier later.
:param label_vec:
:return: digit_mat
"""
digit_mat = np.zeros((10, label_vec.size))
for i in range(len(label_vec)):
digit_mat[label_vec[i], i] = 1
digit_mat = digit_mat
return digit_mat
def back_prop(self, array_data: np.ndarray, array_labels: np.ndarray) -> dict:
"""
Calculate the cost vector.
Backpropogate the errors through the network to adjust both the weights and the biases.
Automatically deal with the sizes and names of the weights and nodes.
:return: end_vals
"""
w = {'dw' + str(i + 1): [] for i in range(len(self.list_nodes))}
b = {'db' + str(i + 1): [] for i in range(len(self.list_nodes))}
dict_vals = {**w, **b, 'Cost': []}
for i in range(array_data.shape[1]):
labels = array_labels[:, i]
res = self.feedforward(image_data=array_data[:, i])
deriv_func = self.func_dict[self.activation_list[0]][1]
delta = deriv_func(res['z' + str(len(self.list_nodes))]) * 2 * (
res['a' + str(len(self.list_nodes))] - labels)
n = len(self.list_nodes)
dict_vals['dw' + str(n)].append(np.outer(delta, res['a' + str(len(self.list_nodes) - 1)].T))
dict_vals['db' + str(n)].append(delta)
for i in range(len(self.list_nodes) - 1):
n = len(self.list_nodes) - i - 1
deriv_func = self.func_dict[self.activation_list[i + 1]][1]
delta = np.dot(self.w['w' + str(n + 1)].T, delta) * deriv_func(res['z' + str(n)])
dict_vals['dw' + str(n)].append(np.outer(delta, res['a' + str(n - 1)].T))
dict_vals['db' + str(n)].append(delta)
dict_vals['Cost'].append(np.sum((res['a' + str(len(self.list_nodes))] - labels) ** 2))
end_vals = {'Average_Cost': np.mean(dict_vals["Cost"])}
for i in range(len(self.list_nodes)):
end_vals['dw' + str(i + 1)] = self.av_array(dict_vals['dw' + str(i + 1)])
end_vals['db' + str(i + 1)] = self.av_array(dict_vals['db' + str(i + 1)])
print(f'Average Cost: {end_vals["Average_Cost"]}')
return end_vals
@staticmethod
def av_array(list_array: list) -> np.ndarray:
"""
Calculate the average of a list of numpy vectors,
:param list_array:
:return: np.ndarray:
"""
out = np.zeros(shape=list_array[0].shape)
for arr in list_array:
out += arr
return 1 / len(list_array) * out
def sgd(self, num_per_iter: int, num_iter: int, draw_cost=False):
"""
Implement Stochastic Gradient Descent Algo.
Using the mini batch approach.
:param draw_cost: Option to Draw function of cost.
:param num_per_iter: Number of samples per batch
:param num_iter: Number of batches
"""
cost_list = []
for epoch in range(self.epochs): # Add epochs later.
for i in range(num_iter):
array_data = self.train_matrix[:, num_iter: num_iter + num_per_iter]
array_labels = self.one_hot(self.train_labels[num_iter: num_iter + num_per_iter])
vals_dict = self.back_prop(array_data=array_data, array_labels=array_labels)
# l = max(self.learning_rate / (i+1), 0.01)
l = self.learning_rate
for j in range(len(self.list_nodes)):
n = j + 1
self.w['w' + str(n)] = self.w['w' + str(n)] - vals_dict['dw' + str(n)] * l
self.b['b' + str(n)] = self.b['b' + str(n)] - vals_dict['db' + str(n)] * l
cost_list.append(vals_dict['Average_Cost'])
# print(f'Generation {i} complete.')
print(f'Epoch: {epoch + 1} Complete.')
if draw_cost:
plt.plot(range(num_iter * self.epochs), cost_list,
label='\u03B1 :' + str(self.learning_rate) + '\n N: ' + str(self.list_nodes))
plt.xlabel('Generation')
plt.ylabel('Cost')
plt.legend(loc='best')
plt.title('Average Cost as a function of Generation Trained')
def test_model(self, use_train: bool = False) -> pd.DataFrame:
"""
Test out the models accuracy on the test data provided.
:return: df
"""
if use_train:
matrix = self.train_matrix
labels = self.train_labels
else:
matrix = self.test_matrix
labels = self.test_labels
list_corr = []
list_preds = []
for i in range(matrix.shape[1]):
image = matrix[:, i]
actual_val = labels[i]
res = self.feedforward(image_data=image)
pred_val = np.argmax(res['a' + str(len(self.list_nodes))])
if pred_val == actual_val:
list_corr.append(1)
else:
list_corr.append(0)
list_preds.append([pred_val, actual_val])
df = pd.DataFrame(list_preds, columns=['Predictions', 'Actual'])
if use_train:
print(f'Percentage Correct is: {100 * sum(list_corr) / len(list_corr)}% \n On Training Data.')
else:
print(f'Percentage Correct is: {100 * sum(list_corr) / len(list_corr)}% \n On Testing Data.')
return df
def draw_number(self, val: int):
"""
Method that will draw the number trying to be read.
Method will also tell you our prediction and the actual value
:param val: Index of value you'd like to display
"""
image = self.test_matrix[:, val]
actual_val = self.test_labels[val]
res = self.feedforward(image_data=image)
pred_val = np.argmax(res['a' + str(len(self.list_nodes))])
print(f'Actual Value: {actual_val}')
print(f'Predicted Value: {pred_val}')
image = self.test_matrix[:, val].reshape(28, 28)
plt.imshow(image, cmap='gray')
plt.show()