-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnnScript.py
More file actions
378 lines (266 loc) · 14.2 KB
/
nnScript.py
File metadata and controls
378 lines (266 loc) · 14.2 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
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
import numpy as np
from scipy.optimize import minimize
from scipy.io import loadmat
from math import sqrt
import pickle
indicesOfUsefulColumns = []
def initializeWeights(n_in, n_out):
"""
# initializeWeights return the random weights for Neural Network given the
# number of node in the input layer and output layer
# Input:
# n_in: number of nodes of the input layer
# n_out: number of nodes of the output layer
# Output:
# W: matrix of random initial weights with size (n_out x (n_in + 1))"""
epsilon = sqrt(6) / sqrt(n_in + n_out + 1)
W = (np.random.rand(n_out, n_in + 1) * 2 * epsilon) - epsilon
return W
def sigmoid(z):
"""# Notice that z can be a scalar, a vector or a matrix
# return the sigmoid of input z"""
output = np.multiply(-1,z)
output = np.exp(output)
output = np.add(1,output)
output = np.divide(1,output)
# print("sigmoid shape: " + str(output.shape))
return output
def preprocess():
""" Input:
Although this function doesn't have any input, you are required to load
the MNIST data set from file 'mnist_all.mat'.
Output:
train_data: matrix of training set. Each row of train_data contains
feature vector of a image
train_label: vector of label corresponding to each image in the training
set
validation_data: matrix of training set. Each row of validation_data
contains feature vector of a image
validation_label: vector of label corresponding to each image in the
training set
test_data: matrix of training set. Each row of test_data contains
feature vector of a image
test_label: vector of label corresponding to each image in the testing
set
Some suggestions for preprocessing step:
- feature selection"""
mat = loadmat('mnist_all.mat') # loads the MAT object as a Dictionary
# Pick a reasonable size for validation data
# ------------Initialize preprocess arrays----------------------#
train_preprocess = np.zeros(shape=(50000, 784))
validation_preprocess = np.zeros(shape=(10000, 784))
test_preprocess = np.zeros(shape=(10000, 784))
train_label_preprocess = np.zeros(shape=(50000,))
validation_label_preprocess = np.zeros(shape=(10000,))
test_label_preprocess = np.zeros(shape=(10000,))
# ------------Initialize flag variables----------------------#
train_len = 0
validation_len = 0
test_len = 0
train_label_len = 0
validation_label_len = 0
# ------------Start to split the data set into 6 arrays-----------#
for key in mat:
# -----------when the set is training set--------------------#
if "train" in key:
label = key[-1] # record the corresponding label
tup = mat.get(key)
sap = range(tup.shape[0])
tup_perm = np.random.permutation(sap)
tup_len = len(tup) # get the length of current training set
tag_len = tup_len - 1000 # defines the number of examples which will be added into the training set
# ---------------------adding data to training set-------------------------#
train_preprocess[train_len:train_len + tag_len] = tup[tup_perm[1000:], :]
train_len += tag_len
train_label_preprocess[train_label_len:train_label_len + tag_len] = label
train_label_len += tag_len
# ---------------------adding data to validation set-------------------------#
validation_preprocess[validation_len:validation_len + 1000] = tup[tup_perm[0:1000], :]
validation_len += 1000
validation_label_preprocess[validation_label_len:validation_label_len + 1000] = label
validation_label_len += 1000
# ---------------------adding data to test set-------------------------#
elif "test" in key:
label = key[-1]
tup = mat.get(key)
sap = range(tup.shape[0])
tup_perm = np.random.permutation(sap)
tup_len = len(tup)
test_label_preprocess[test_len:test_len + tup_len] = label
test_preprocess[test_len:test_len + tup_len] = tup[tup_perm]
test_len += tup_len
# ---------------------Shuffle,double and normalize-------------------------#
train_size = range(train_preprocess.shape[0])
train_perm = np.random.permutation(train_size)
train_data = train_preprocess[train_perm]
train_data = np.double(train_data)
train_data = train_data / 255.0
train_label = train_label_preprocess[train_perm]
validation_size = range(validation_preprocess.shape[0])
vali_perm = np.random.permutation(validation_size)
validation_data = validation_preprocess[vali_perm]
validation_data = np.double(validation_data)
validation_data = validation_data / 255.0
validation_label = validation_label_preprocess[vali_perm]
test_size = range(test_preprocess.shape[0])
test_perm = np.random.permutation(test_size)
test_data = test_preprocess[test_perm]
test_data = np.double(test_data)
test_data = test_data / 255.0
test_label = test_label_preprocess[test_perm]
#Feature selection
unique_matrix = np.apply_along_axis(unique_identifier,0,train_data)
indicesOfRedundantColumns = np.where(unique_matrix==1)
indicesOfUsefulColumns = (np.where(unique_matrix!=1))[0]
train_data = np.delete(train_data,indicesOfRedundantColumns,1)
validation_data = np.delete(validation_data,indicesOfRedundantColumns,1)
test_data = np.delete(test_data,indicesOfRedundantColumns,1)
print('preprocess done')
return train_data, train_label, validation_data, validation_label, test_data, test_label
def unique_identifier(a):
temp = np.unique(a).shape[0]
return temp
def nnObjFunction(params, *args):
"""% nnObjFunction computes the value of objective function (negative log
% likelihood error function with regularization) given the parameters
% of Neural Networks, thetraining data, their corresponding training
% labels and lambda - regularization hyper-parameter.
% Input:
% params: vector of weights of 2 matrices w1 (weights of connections from
% input layer to hidden layer) and w2 (weights of connections from
% hidden layer to output layer) where all of the weights are contained
% in a single vector.
% n_input: number of node in input layer (not include the bias node)
% n_hidden: number of node in hidden layer (not include the bias node)
% n_class: number of node in output layer (number of classes in
% classification problem
% training_data: matrix of training data. Each row of this matrix
% represents the feature vector of a particular image
% training_label: the vector of truth label of training images. Each entry
% in the vector represents the truth label of its corresponding image.
% lambda: regularization hyper-parameter. This value is used for fixing the
% overfitting problem.
% Output:
% obj_val: a scalar value representing value of error function
% obj_grad: a SINGLE vector of gradient value of error function
% NOTE: how to compute obj_grad
% Use backpropagation algorithm to compute the gradient of error function
% for each weights in weight matrices.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reshape 'params' vector into 2 matrices of weight w1 and w2
% w1: matrix of weights of connections from input layer to hidden layers.
% w1(i, j) represents the weight of connection from unit j in input
% layer to unit i in hidden layer.
% w2: matrix of weights of connections from hidden layer to output layers.
% w2(i, j) represents the weight of connection from unit j in hidden
% layer to unit i in output layer."""
n_input, n_hidden, n_class, training_data, training_label, lambdaval = args
w1 = params[0:n_hidden * (n_input + 1)].reshape((n_hidden, (n_input + 1)))
w2 = params[(n_hidden * (n_input + 1)):].reshape((n_class, (n_hidden + 1)))
obj_val = 0
# Your code here
trainingData_size = training_data.shape[0]
nnOutputValues, hidden_output_withBias, hidden_output, input_matrix = nnOutput(w1,w2,training_data)
training_output = np.zeros((trainingData_size,10))
for i in range(len(training_label)):
training_output[i,int(training_label[i])] = 1
t1 = np.multiply(training_output, np.log(nnOutputValues))
t2 = np.multiply(np.subtract(1,training_output),np.log(np.subtract(1,nnOutputValues)))
t3 = np.add(t1,t2)
obj_val = np.divide(np.sum(t3),-1*trainingData_size)
reg = np.sum(np.power(w1,2)) + np.sum(np.power(w2,2))
# Value of error function
obj_val = obj_val + np.divide(np.multiply(reg,lambdaval),2*trainingData_size)
# Gradient matrix calculation
delta_l = np.subtract(nnOutputValues,training_output)
Z_j = hidden_output_withBias
# delta_Jmatrix is a 10*51 matrix, i.e, output layer neurons * hidden layer neurons
delta_Jmatrix = np.dot(delta_l.T,Z_j)
temp = np.add(delta_Jmatrix,np.multiply(lambdaval,w2))
grad_w2 = np.divide(temp,trainingData_size)
# We remove the 51st column of the hidden layer since we are moving the opposite direction, i.e, back propagation
w2_mod = np.delete(w2,n_hidden,1)
# Same reason as above
Z_j = hidden_output
t4 = np.subtract(1,Z_j)
t5 = np.multiply(t4,Z_j)
t6 = np.dot(delta_l,w2_mod)
t7 = np.multiply(t5,t6)
delta_Jmatrix = np.dot(t7.T,input_matrix)
grad_w1 = np.divide(np.add(delta_Jmatrix,np.multiply(lambdaval,w1)),trainingData_size)
# Make sure you reshape the gradient matrices to a 1D array. for instance if your gradient matrices are grad_w1 and grad_w2
# you would use code similar to the one below to create a flat array
obj_grad = np.concatenate((grad_w1.flatten(), grad_w2.flatten()),0)
return (obj_val, obj_grad)
def nnOutput(w1,w2,data):
ones_matrix = np.ones((data.shape[0],1))
input_matrix = np.concatenate((data,ones_matrix),1)
hidden_output = np.dot(input_matrix,w1.T)
hidden_output = sigmoid(hidden_output)
hidden_output_withBias = np.concatenate((hidden_output,ones_matrix),1)
final_output = np.dot(hidden_output_withBias,w2.T)
final_output = sigmoid(final_output)
return final_output, hidden_output_withBias, hidden_output, input_matrix
def nnPredict(w1, w2, data):
"""% nnPredict predicts the label of data given the parameter w1, w2 of Neural
% Network.
% Input:
% w1: matrix of weights of connections from input layer to hidden layers.
% w1(i, j) represents the weight of connection from unit i in input
% layer to unit j in hidden layer.
% w2: matrix of weights of connections from hidden layer to output layers.
% w2(i, j) represents the weight of connection from unit i in input
% layer to unit j in hidden layer.
% data: matrix of data. Each row of this matrix represents the feature
% vector of a particular image
% Output:
% label: a column vector of predicted labels"""
ones_matrix = np.ones((data.shape[0],1))
input_matrix = np.concatenate((data,ones_matrix),1)
hidden_output = np.dot(input_matrix,w1.T)
hidden_output = sigmoid(hidden_output)
hidden_output_withBias = np.concatenate((hidden_output,ones_matrix),1)
final_output = np.dot(hidden_output_withBias,w2.T)
final_output = sigmoid(final_output)
labels = (np.argmax(final_output,axis=1))
return labels
"""**************Neural Network Script Starts here********************************"""
train_data, train_label, validation_data, validation_label, test_data, test_label = preprocess()
# Train Neural Network
# set the number of nodes in input unit (not including bias unit)
n_input = train_data.shape[1]
# set the number of nodes in hidden unit (not including bias unit)
n_hidden = 50
# set the number of nodes in output unit
n_class = 10
# initialize the weights into some random matrices
initial_w1 = initializeWeights(n_input, n_hidden)
initial_w2 = initializeWeights(n_hidden, n_class)
# unroll 2 weight matrices into single column vector
initialWeights = np.concatenate((initial_w1.flatten(), initial_w2.flatten()), 0)
# set the regularization hyper-parameter
lambdaval = 0
args = (n_input, n_hidden, n_class, train_data, train_label, lambdaval)
# Train Neural Network using fmin_cg or minimize from scipy,optimize module. Check documentation for a working example
opts = {'maxiter': 50} # Preferred value.
nn_params = minimize(nnObjFunction, initialWeights, jac=True, args=args, method='CG', options=opts)
# In Case you want to use fmin_cg, you may have to split the nnObjectFunction to two functions nnObjFunctionVal
# and nnObjGradient. Check documentation for this function before you proceed.
# nn_params, cost = fmin_cg(nnObjFunctionVal, initialWeights, nnObjGradient,args = args, maxiter = 50)
# Reshape nnParams from 1D vector into w1 and w2 matrices
w1 = nn_params.x[0:n_hidden * (n_input + 1)].reshape((n_hidden, (n_input + 1)))
w2 = nn_params.x[(n_hidden * (n_input + 1)):].reshape((n_class, (n_hidden + 1)))
# Dump the details in the pickle file
selected_features = indicesOfUsefulColumns
obj = [selected_features, n_hidden, w1, w2, lambdaval]
pickle.dump(obj, open('params.pickle', 'wb'))
# Test the computed parameters
predicted_label = nnPredict(w1, w2, train_data)
# find the accuracy on Training Dataset
print('\nTraining set Accuracy:' + str(100 * np.mean((predicted_label == train_label).astype(float))) + '%')
predicted_label = nnPredict(w1, w2, validation_data)
# find the accuracy on Validation Dataset
print('\nValidation set Accuracy:' + str(100 * np.mean((predicted_label == validation_label).astype(float))) + '%')
predicted_label = nnPredict(w1, w2, test_data)
# find the accuracy on Test Dataset
print('\nTest set Accuracy:' + str(100 * np.mean((predicted_label == test_label).astype(float))) + '%')