-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnn01.py
285 lines (217 loc) · 10.7 KB
/
nn01.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
# -*- coding: utf-8 -*-
"""
A DIY Neural Net code for basic concepts
@author: whatdhack
Download MNIST csv data from https://pjreddie.com/projects/mnist-in-csv/
Ref:
1. The Deep Learning Book by Ian Goodfellow, Yoshua Bengio and Aaron Courville
(http://www.deeplearningbook.org/)
2. Make Your Own Neural Networks by Tariq Rashid
(https://github.com/makeyourownneuralnetwork/makeyourownneuralnetwork )
3. Machine Learning by Andrew Ng on Coursera
(https://www.coursera.org/learn/machine-learning)
"""
import numpy as np
# scipy.special for the sigmoid function expit()
import scipy.special
from random import shuffle
import time
# neural network class definition
class NeuralNetwork:
# initialise the neural network
def __init__(self, layernodes, learningrate, mbsize=3, training_data_file_name="data/mnist_train.csv", test_data_file_name="data/mnist_test.csv"):
# set number of layers , nodss per layer
self.layernodes = layernodes
self.numlayers = len(layernodes)
# link weight matrices, w
self.w = []
for i in range (len(layernodes) -1) :
self.w.append (np.random.normal(0.0, pow(self.layernodes[i], -0.5), (self.layernodes[i+1], self.layernodes[i])))
print ('w[%d].shape'% i, self.w[i].shape)
# learning rate
self.lr = learningrate
# mini-batch size
self.mbsize = mbsize
# activation function is the sigmoid function
self.activation_function = lambda x: scipy.special.expit(x)
# load the training data CSV file into a list
training_data_file = open(training_data_file_name, 'r')
self.training_data = training_data_file.readlines()
training_data_file.close()
# shuffle the trainign data
shuffle(self.training_data)
# load the mnist test data CSV file into a list
test_data_file = open(test_data_file_name, 'r')
self.test_data = test_data_file.readlines()
test_data_file.close()
self.train = self.trainMBSGD
return
# forward pass through the neural network
# Ni Wi Nj Wj Nk
# Ii Oi Ij Oj Ik Ok
def forward (self, inputs_list):
# convert inputs list to 2d array
# calculate signals into a layer
# calculate the signals emerging from a layer
inputsl = [np.array(inputs_list, ndmin=2).T]
outputsl = [np.array(inputs_list, ndmin=2).T]
for l in range(1,self.numlayers):
inputsl.append(np.dot(self.w[l-1], outputsl[l-1]))
outputsl.append(self.activation_function(inputsl[l]))
return outputsl
# brack prop errors and weight adjustment the neural network
# Ni Wi Nj Wj Nk
# Ei Ej Ek
def backpropSGD(self, inputs_list, targets_list):
# convert inputs list to 2d array
targets = np.array(targets_list, ndmin=2).T
outputsl = self.forward(inputs_list)
errorsl = [None]*(self.numlayers)
errorsl[-1] = targets - outputsl[-1]
#print ('range(self.numlayers-2,0,-1)', range(self.numlayers-2,0,-1))
for l in range(self.numlayers-2,-1,-1):# W1,W0
errorsl[l] = np.dot(self.w[l].T, errorsl[l+1]) # error bp is proportinal to weight
errorp = errorsl[l+1] * outputsl[l+1] * (1.0 - outputsl[l+1]) # dE/dw = E*do/dw for sigmoid
opprev = np.transpose(outputsl[l])
dw = self.lr * np.dot( errorp, opprev)
#print ('w[l].shape', self.w[l].shape, 'dw.shape', dw.shape)
self.w[l] += dw
#print ('w[%d]'%l, self.w[l])
# brack prop errors and weight adjustment the neural network
# Ni Wi Nj Wj Nk
# Ei Ej Ek
def backprop1a(self, inputs_list, targets_list):
# convert inputs list to 2d array
targets = np.array(targets_list, ndmin=2).T
outputsl = self.forward(inputs_list)
errorsl = [None]*(self.numlayers)
errorsl[-1] = targets - outputsl[-1]
#print ('range(self.numlayers-2,0,-1)', range(self.numlayers-2,0,-1))
dwl = []
for l in range(self.numlayers-2,-1,-1):# W1,W0
errorsl[l] = np.dot(self.w[l].T, errorsl[l+1])
errorp = errorsl[l+1] * outputsl[l+1] * (1.0 - outputsl[l+1])
opprev = np.transpose(outputsl[l])
dw = self.lr * np.dot( errorp, opprev)
#print ('w[l].shape', self.w[l].shape, 'dw.shape', dw.shape)
#self.w[l] += dw
dwl.append(dw)
return dwl
# brack prop errors and weight adjustment the neural network
# Ni Wi Nj Wj Nk
# Ei Ej Ek
def backpropMBSGD(self, inputs_list, targets_list):
# convert inputs list to 2d array
targets = np.array(targets_list, ndmin=2).T
outputsl = self.forward(inputs_list)
errorsl = [None]*(self.numlayers)
errorsl[-1] = targets - outputsl[-1]
errl = []
opprevl = []
for l in range(self.numlayers-2,-1,-1):# W1,W0
errorsl[l] = np.dot(self.w[l].T, errorsl[l+1])
errorp = errorsl[l+1] * outputsl[l+1] * (1.0 - outputsl[l+1])
opprev = outputsl[l]
#print ('errorp.shape', errorp.shape, 'opprev.shape', opprev.shape)
errl.append(errorp)
opprevl.append(opprev)
return errl, opprevl
# train the neural network with SGD and minibatch size 1
def trainSGD(self, numepochs):
# epochs is the number of times the training data set is used for training
for e in range(numepochs):
# go through all records in the training data set
starttime = time.time()
for record in self.training_data:
# split the record by the ',' commas
all_values = record.split(',')
# scale and shift the inputs
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# create the target output values (all 0.01, except the desired label which is 0.99)
targets = np.zeros(self.layernodes[-1]) + 0.01
# all_values[0] is the target label for this record
targets[int(all_values[0])] = 0.99
self.backpropSGD(inputs, targets)
print ('epoch ', e, 'completed in %d s'%(time.time()- starttime))
# train the neural network with SGD and minibatch not 1
def trainMBSGD(self, numepochs):
# epochs is the number of times the training data set is used for training
minibatch_size = self.mbsize
for e in range(numepochs):
# go through all records in the training data set
num_minibatch = int (len(self.training_data) / minibatch_size)
if num_minibatch > 100000:
num_minibatch = 100000
starttime = time.time()
for i in range(0, num_minibatch*minibatch_size, minibatch_size):
errlmb = []
opprevlmb=[]
for record in self.training_data[i:i+minibatch_size]:
# split the record by the ',' commas
all_values = record.split(',')
# scale and shift the inputs
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# create the target output values (all 0.01, except the desired label which is 0.99)
targets = np.zeros(self.layernodes[-1]) + 0.01
# all_values[0] is the target label for this record
targets[int(all_values[0])] = 0.99
errl, opprevl = self.backpropMBSGD(inputs, targets)
errlmb.append(errl)
opprevlmb.append(opprevl)
errnd = np.array(errlmb)
errf = np.mean(errnd, axis=0)
opprevnd = np.array(opprevlmb)
opprevf = np.mean(opprevnd, axis=0)
for l in range ( len(self.w) ):
l1 = len(self.w)-1-l
dw = self.lr * np.dot( errf[l1], np.transpose(opprevf[l1]))
self.w[l] += dw
# self.w[l] += dwf[len(self.w)-1-l]
print ('epoch ', e, 'completed in %d s'%(time.time()- starttime))
# run test
def test(self):
# test the neural network
# score for how well the network performs, initially empty
score = []
# go through all the records in the test data set
for record in self.test_data:
# split the record by the ',' commas
all_values = record.split(',')
# correct answer is first value
correct_label = int(all_values[0])
# scale and shift the inputs
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# forward pass through the network
outputsl = self.forward (inputs)
# the index of the highest value corresponds to the label
label = np.argmax(outputsl[-1])
# append correct or incorrect to list
if (label == correct_label):
# network's answer matches correct answer, add 1 to score
score.append(1)
else:
# network's answer doesn't match correct answer, add 0 to score
score.append(0)
pass
pass
# calculate the performance score, the fraction of correct answers
print ("performance = ", np.asarray(score).mean())
def main():
# number of layers and nodes in each layer
# input, hidden, output
layernodes = [784,200,10]
# learning rate
learning_rate = 0.1
#mini-batch size
mbsize = 3
# download MNIST csv data from https://pjreddie.com/projects/mnist-in-csv/
# create the neural network
n = NeuralNetwork(layernodes, learning_rate, mbsize, "data/mnist_train.csv", "data/mnist_test.csv")
# train the neural network
n.train(5)
# test the neural network
n.test()
if __name__ == '__main__':
start = time.time()
main()
print ( 'compeleted in %d s'%(time.time()- start))