-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML.py
More file actions
285 lines (198 loc) · 6.81 KB
/
ML.py
File metadata and controls
285 lines (198 loc) · 6.81 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
## Written by Lucas Olsen for CSCI5105
## do not modify this file
import csv
import numpy as np
##
### Matrix functions
##
def scale_matricies(mat, scalar):
return np.multiply(mat, scalar)
def sum_matricies(mat, mat2):
return np.add(mat, mat2)
def calc_gradient(curr, orig):
return np.subtract(curr, orig)
##
### mlp class
##
class mlp:
# initialize the model
# read in the data in file fname
def __init__(self):
self.initialized = False
def is_initialized(self):
return self.initialized
# initialize the training model with random weights of dimensions _k and _h
# returns self.initialized, false on error, true on success
def init_training_random(self, fname, _k, _h):
X = []
labels = []
X, labels = self.read_data(fname)
# test if datafile was valid
if np.size(X) < 1:
self.initialized = False
return self.initialized
# set class variables for X, labels, n, and d
self.X = X
self.labels = labels
self.n, self.d = np.shape(X)
# set k and h variables
self.k = _k
self.h = _h
# randomly fill W and V
np.random.seed(1) # seed for reproducability
self.V = (np.random.rand(self.h+1, self.k) * 0.02) - 0.01
self.W = (np.random.rand(self.d+1, self.h) * 0.02) - 0.01
# forward propogate to build Z and Y
self.forward_propogate(self.X)
self.initialized = True
return self.initialized
# initialize the training model with input weights matricies
# returns self.initialized, false on error, true on success
def init_training_model(self, fname, V, W):
X = []
labels = []
X, labels = self.read_data(fname)
# test if datafile is valid
if np.size(X) < 1:
self.initialized = False
return self.initialized
# set class variables for X, labels, n, and d
self.X = X
self.labels = labels
self.n, self.d = np.shape(X)
# set the model's weights
self.set_weights(V, W)
# forward propogation to build Z and Y
self.forward_propogate(self.X)
self.initialized = True
return self.initialized
# train the MLP model
# returns -1 on error, training error rate on success
def train(self, eta, epochs):
if not(self.initialized):
return -1
# forwards propogate, initialize Y and Z
self.forward_propogate(self.X)
err = error_func(self.Y, self.labels)
for i in range(epochs):
# backwards propogate
dV, dW = self.backward_propogate(eta)
# update weights
self.update_weights(dV, dW)
# forwards propogate
self.forward_propogate(self.X)
# re-calc error, exit if the difference is too small
err_upd = error_func(self.Y, self.labels)
if(abs(err - err_upd) <= 0.2):
break
err = err_upd
# return the error rate in predictions
return error_rate(self.Y, self.labels)
# run the current model on validation data
# returns -1 on error, validation error rate on success
def validate(self, fname):
if not(self.initialized):
return -1
# return error on no validation data
X, labels = self.read_data(fname)
if np.size(labels) < 1:
return -1
self.forward_propogate(X)
return error_rate(self.Y, labels)
# run the current model on labelless data to make predictions
# returns -1 on error, array of predicitons on success
def predict(self, fname):
if not(self.initialized):
return -1
# return error on no prediciton data
X, labels = self.read_data(fname)
if np.size(labels) < 1:
return -1
# append the labels column (prediciton data won't have labels)
X = np.append(X, labels)
self.forward_propogate(X)
return np.argmax(self.Y, axis=1)
##
### Weights functions
##
# set the model's weights
def set_weights(self, V, W):
# set h and k
h, k = np.shape(V)
h = h - 1
self.h = h
self.k = k
# set W and V
self.W = W
self.V = V
# get the model's current weights
def get_weights(self):
return self.V, self.W
# update the model's weights
def update_weights(self, dV, dW):
self.V = self.V + dV
self.W = self.W + dW
##
### Propogation functions
### do not call these in your thrift code
##
# forwards propogate, build Z and Y
def forward_propogate(self, _X):
_n, _d = np.shape(_X)
_X = np.append(np.ones((_n, 1)), _X, axis=1)
Z = [[ReLU(val) for val in row] for row in np.dot(_X, self.W)]
self.Z = np.append(np.ones((_n, 1)), Z, axis=1)
O = np.dot(self.Z, self.V)
self.Y = np.zeros((_n, self.k))
for t in range(_n):
for i in range(self.k):
self.Y[t,i] = 1/np.sum(np.exp(O[t,:] - O[t,i]))
# backwards propogate, Return dV and dW
def backward_propogate(self, eta):
R = np.zeros((self.n, self.k))
for k in range(self.k):
R[:,k] = (self.labels == k)
X = np.append(np.ones((self.n, 1)), self.X, axis=1)
dV = eta*np.transpose((np.dot(np.transpose((R - self.Y)), self.Z)))
# dW = eta*(((R - Y_pred)*V(2:end,:)' .* (X*W >= 0))'*X)';
XW = [[val >= 0 for val in row] for row in np.dot(X, self.W)]
# Vt = np.transpose(self.V[1:,:])
Vt = np.transpose(np.delete(self.V, 0, 0))
dW = np.transpose(np.multiply(np.dot((R-self.Y),Vt), XW))
dW = eta * np.transpose(np.dot(dW,X))
return dV, dW
# read data, do not call this in your thrift code
def read_data(self, fname):
X = []
labels = []
try:
file = open(fname, 'r')
data = csv.reader(file)
for line in data:
labels.append(int(line[-1]))
X.append([int(item) for item in line[:-1]])
X = np.array(X)
labels = np.array(labels)
except:
print("Failed to open file %s" % fname)
return X, labels
##
### "Private" functions
### do not use these in your thrift code
##
# ReLU activation function
def ReLU(x):
if 0 > x:
return x
return x
# Calculate current error of predictions
def error_func(Y, labels):
_n, _k = np.shape(Y)
R = np.zeros((_n, _k))
for k in range(_k):
R[:,k] = (labels == k)
return -np.sum(np.multiply(R, np.log(Y+0.000001)))
# Calculate the % of wrongly classified samples
def error_rate(Y, labels):
_n = np.shape(labels)
return (np.sum(np.not_equal(np.argmax(Y, axis=1), labels)) / _n)[0]