forked from MichalDanielDobrzanski/DeepLearningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiedVersion2.py
More file actions
455 lines (387 loc) · 15.5 KB
/
modifiedVersion2.py
File metadata and controls
455 lines (387 loc) · 15.5 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import os
import datetime as date
dirCin="E:\\Users\\amft\\Documents\\DeepLearningPython35"
dirHome = "C:\\Users\\jeong\\Documents\\Python Scripts\\DeepLearningPython35"
dirLuiz = ""
os.chdir(dirHome)
# %load mnist_loader.py
"""
mnist_loader
~~~~~~~~~~~~
A library to load the MNIST image data. For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``. In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""
#### Libraries
# Standard library
import pickle
import gzip
# Third-party libraries
import numpy as np
def load_data():
"""Return the MNIST data as a tuple containing the training data,
the validation data, and the test data.
The ``training_data`` is returned as a tuple with two entries.
The first entry contains the actual training images. This is a
numpy ndarray with 50,000 entries. Each entry is, in turn, a
numpy ndarray with 784 values, representing the 28 * 28 = 784
pixels in a single MNIST image.
The second entry in the ``training_data`` tuple is a numpy ndarray
containing 50,000 entries. Those entries are just the digit
values (0...9) for the corresponding images contained in the first
entry of the tuple.
The ``validation_data`` and ``test_data`` are similar, except
each contains only 10,000 images.
This is a nice data format, but for use in neural networks it's
helpful to modify the format of the ``training_data`` a little.
That's done in the wrapper function ``load_data_wrapper()``, see
below.
"""
f = gzip.open('mnist.pkl.gz', 'rb')
training_data, validation_data, test_data = pickle.load(f, encoding="latin1")
f.close()
return (training_data, validation_data, test_data)
def load_data_wrapper():
"""Return a tuple containing ``(training_data, validation_data,
test_data)``. Based on ``load_data``, but the format is more
convenient for use in our implementation of neural networks.
In particular, ``training_data`` is a list containing 50,000
2-tuples ``(x, y)``. ``x`` is a 784-dimensional numpy.ndarray
containing the input image. ``y`` is a 10-dimensional
numpy.ndarray representing the unit vector corresponding to the
correct digit for ``x``.
``validation_data`` and ``test_data`` are lists containing 10,000
2-tuples ``(x, y)``. In each case, ``x`` is a 784-dimensional
numpy.ndarry containing the input image, and ``y`` is the
corresponding classification, i.e., the digit values (integers)
corresponding to ``x``.
Obviously, this means we're using slightly different formats for
the training data and the validation / test data. These formats
turn out to be the most convenient for use in our neural network
code."""
tr_d, va_d, te_d = load_data()
training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]
training_results = [vectorized_result(y) for y in tr_d[1]]
training_data = zip(training_inputs, training_results)
validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]
validation_data = zip(validation_inputs, va_d[1])
test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]
test_data = zip(test_inputs, te_d[1])
return (training_data, validation_data, test_data)
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere. This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((10, 1))
e[j] = 1.0
return e
def write_file(list, name):
f= open(name+".csv","w+")
for i in list:
for j in i:
f.write(str(j)+"; ")
f.write("\n")
import network
totalResult = []
hidden_layer_maxsize = 17 #increasing one layer at a time
mini_batch_high_times = 10 #increasing 2 items at a time
epoch_high_times = 15 #increasing 5 epochs at a time
eta_change_times = 15 #increasing 0.02 at a time
best_accuracy = 0.0
best_topology = []
best_batch_size = 10
best_epoch_size = 40
best_eta = 0.03
mini_batch_size = 30
epochs=40
eta=0.05
t_data, validation_data, t_data = load_data_wrapper()
out_data = list(t_data)
#%%
#testando diferentes composições de topologia
for i in range(0, hidden_layer_maxsize):
topology = [784]
for j in range(0,i):
topology.append(50)
topology.append(10)
net = network.Network(topology)
print("inicio do treinamento")
startTime = date.datetime.now()
net.SGD(training_data=out_data, epochs=epochs, mini_batch_size=mini_batch_size, eta=eta, test_data=validation_data)
endTime = date.datetime.now()
print("fim do treinamento")
print("tempo total de treinamento"+str(endTime-startTime))
t_data, validation_data, t_data = load_data_wrapper()
final_test_data = list(t_data)
accuracy = np.zeros((11,1))
hits = np.zeros((11,1))
total = np.zeros((11,1))
n_test = len(final_test_data)
#result = net.evaluate(t_data)
test_results = [(np.argmax(net.feedforward(x)), y)
for (x, y) in final_test_data]
for (x, y) in test_results:
total[y] = total[y] +1
total[10] = total[10] +1
if (int(x == y)):
hits[y] = hits[y] + 1
hits[10] = hits[10] + 1
for i in range(0,11):
accuracy[i] = hits[i]/total[i]
if final_test_data:
print("result: {} / {}".format(net.evaluate(final_test_data),n_test));
resultByTest = []
print("final result: ")
print("mini_batch_size: "+str(mini_batch_size))
resultByTest.append(str(mini_batch_size))
print("topology: "+str(topology))
resultByTest.append(str(topology))
print("epochs: "+str(epochs))
resultByTest.append(str(epochs))
print("eta: "+str(eta))
resultByTest.append(str(eta))
print("general accuracy: "+str(accuracy[10]))
resultByTest.append(str(accuracy[10]))
print("accuracy 0: "+str(accuracy[0]))
resultByTest.append(str(accuracy[0]))
print("accuracy 1: "+str(accuracy[1]))
resultByTest.append(str(accuracy[1]))
print("accuracy 2: "+str(accuracy[2]))
resultByTest.append(str(accuracy[2]))
print("accuracy 3: "+str(accuracy[3]))
resultByTest.append(str(accuracy[3]))
print("accuracy 4: "+str(accuracy[4]))
resultByTest.append(str(accuracy[4]))
print("accuracy 5: "+str(accuracy[5]))
resultByTest.append(str(accuracy[5]))
print("accuracy 6: "+str(accuracy[6]))
resultByTest.append(str(accuracy[7]))
print("accuracy 7: "+str(accuracy[7]))
resultByTest.append(str(accuracy[7]))
print("accuracy 8: "+str(accuracy[8]))
resultByTest.append(str(accuracy[8]))
print("accuracy 9: "+str(accuracy[9]))
resultByTest.append(str(accuracy[9]))
resultByTest.append(str(endTime-startTime))
if (hits[10]/total[10]) > best_accuracy:
best_accuracy = hits[10]/total[10]
best_topology = topology
totalResult.append(resultByTest)
write_file(totalResult, "1ChangingTopology")
#%%
temp_counter = 0
#testando diferentes composições de mini_batch_size
while temp_counter != mini_batch_high_times:
net = network.Network(best_topology)
#atualizando o tamanho do batch
mini_batch_size = mini_batch_size + temp_counter*2
print("inicio do treinamento")
startTime = date.datetime.now()
net.SGD(training_data=out_data, epochs=epochs, mini_batch_size=mini_batch_size, eta=eta, test_data=validation_data)
endTime = date.datetime.now()
print("fim do treinamento")
print("tempo total de treinamento"+str(endTime-startTime))
t_data, validation_data, t_data = load_data_wrapper()
final_test_data = list(t_data)
accuracy = np.zeros((11,1))
hits = np.zeros((11,1))
total = np.zeros((11,1))
n_test = len(final_test_data)
#result = net.evaluate(t_data)
test_results = [(np.argmax(net.feedforward(x)), y)
for (x, y) in final_test_data]
for (x, y) in test_results:
total[y] = total[y] +1
total[10] = total[10] +1
if (int(x == y)):
hits[y] = hits[y] + 1
hits[10] = hits[10] + 1
for i in range(0,11):
accuracy[i] = hits[i]/total[i]
if final_test_data:
print("result: {} / {}".format(net.evaluate(final_test_data),n_test));
resultByTest = []
print("final result: ")
print("mini_batch_size: "+str(mini_batch_size))
resultByTest.append(str(mini_batch_size))
print("topology: "+str(topology))
resultByTest.append(str(topology))
print("epochs: "+str(epochs))
resultByTest.append(str(epochs))
print("eta: "+str(eta))
resultByTest.append(str(eta))
print("general accuracy: "+str(accuracy[10]))
resultByTest.append(str(accuracy[10]))
print("accuracy 0: "+str(accuracy[0]))
resultByTest.append(str(accuracy[0]))
print("accuracy 1: "+str(accuracy[1]))
resultByTest.append(str(accuracy[1]))
print("accuracy 2: "+str(accuracy[2]))
resultByTest.append(str(accuracy[2]))
print("accuracy 3: "+str(accuracy[3]))
resultByTest.append(str(accuracy[3]))
print("accuracy 4: "+str(accuracy[4]))
resultByTest.append(str(accuracy[4]))
print("accuracy 5: "+str(accuracy[5]))
resultByTest.append(str(accuracy[5]))
print("accuracy 6: "+str(accuracy[6]))
resultByTest.append(str(accuracy[7]))
print("accuracy 7: "+str(accuracy[7]))
resultByTest.append(str(accuracy[7]))
print("accuracy 8: "+str(accuracy[8]))
resultByTest.append(str(accuracy[8]))
print("accuracy 9: "+str(accuracy[9]))
resultByTest.append(str(accuracy[9]))
resultByTest.append(str(endTime-startTime))
if (hits[10]/total[10]) > best_accuracy:
best_accuracy = hits[10]/total[10]
best_batch_size = mini_batch_size
totalResult.append(resultByTest)
#aumentando
temp_counter = temp_counter + 1
write_file(totalResult, "2ChangingBatch_size")
#%%
temp_counter = 0
#testando diferentes composições de epochs
while temp_counter != epoch_high_times:
net = network.Network(best_topology)
epochs = epochs + temp_counter*5
print("inicio do treinamento")
startTime = date.datetime.now()
net.SGD(training_data=out_data, epochs=epochs, mini_batch_size=best_batch_size, eta=eta, test_data=validation_data)
endTime = date.datetime.now()
print("fim do treinamento")
print("tempo total de treinamento"+str(endTime-startTime))
t_data, validation_data, t_data = load_data_wrapper()
final_test_data = list(t_data)
accuracy = np.zeros((11,1))
hits = np.zeros((11,1))
total = np.zeros((11,1))
n_test = len(final_test_data)
#result = net.evaluate(t_data)
test_results = [(np.argmax(net.feedforward(x)), y)
for (x, y) in final_test_data]
for (x, y) in test_results:
total[y] = total[y] +1
total[10] = total[10] +1
if (int(x == y)):
hits[y] = hits[y] + 1
hits[10] = hits[10] + 1
for i in range(0,11):
accuracy[i] = hits[i]/total[i]
if final_test_data:
print("result: {} / {}".format(net.evaluate(final_test_data),n_test));
resultByTest = []
print("final result: ")
print("mini_batch_size: "+str(mini_batch_size))
resultByTest.append(str(mini_batch_size))
print("topology: "+str(topology))
resultByTest.append(str(topology))
print("epochs: "+str(epochs))
resultByTest.append(str(epochs))
print("eta: "+str(eta))
resultByTest.append(str(eta))
print("general accuracy: "+str(accuracy[10]))
resultByTest.append(str(accuracy[10]))
print("accuracy 0: "+str(accuracy[0]))
resultByTest.append(str(accuracy[0]))
print("accuracy 1: "+str(accuracy[1]))
resultByTest.append(str(accuracy[1]))
print("accuracy 2: "+str(accuracy[2]))
resultByTest.append(str(accuracy[2]))
print("accuracy 3: "+str(accuracy[3]))
resultByTest.append(str(accuracy[3]))
print("accuracy 4: "+str(accuracy[4]))
resultByTest.append(str(accuracy[4]))
print("accuracy 5: "+str(accuracy[5]))
resultByTest.append(str(accuracy[5]))
print("accuracy 6: "+str(accuracy[6]))
resultByTest.append(str(accuracy[7]))
print("accuracy 7: "+str(accuracy[7]))
resultByTest.append(str(accuracy[7]))
print("accuracy 8: "+str(accuracy[8]))
resultByTest.append(str(accuracy[8]))
print("accuracy 9: "+str(accuracy[9]))
resultByTest.append(str(accuracy[9]))
resultByTest.append(str(endTime-startTime))
if (hits[10]/total[10]) > best_accuracy:
best_accuracy = hits[10]/total[10]
best_epoch_size = epochs
totalResult.append(resultByTest)
#aumentando
temp_counter = temp_counter + 1
write_file(totalResult, "3Changing_epoch_size")
#%%
temp_counter = 0
#testando diferentes composições de eta
while temp_counter != eta_change_times:
net = network.Network(best_topology)
#modificanto o eta
eta = eta + temp_counter*0.02
print("inicio do treinamento")
startTime = date.datetime.now()
net.SGD(training_data=out_data, epochs=best_epoch_size, mini_batch_size=best_batch_size, eta=eta, test_data=validation_data)
endTime = date.datetime.now()
print("fim do treinamento")
print("tempo total de treinamento"+str(endTime-startTime))
t_data, validation_data, t_data = load_data_wrapper()
final_test_data = list(t_data)
accuracy = np.zeros((11,1))
hits = np.zeros((11,1))
total = np.zeros((11,1))
n_test = len(final_test_data)
#result = net.evaluate(t_data)
test_results = [(np.argmax(net.feedforward(x)), y)
for (x, y) in final_test_data]
for (x, y) in test_results:
total[y] = total[y] +1
total[10] = total[10] +1
if (int(x == y)):
hits[y] = hits[y] + 1
hits[10] = hits[10] + 1
for i in range(0,11):
accuracy[i] = hits[i]/total[i]
if final_test_data:
print("result: {} / {}".format(net.evaluate(final_test_data),n_test));
resultByTest = []
print("final result: ")
print("mini_batch_size: "+str(mini_batch_size))
resultByTest.append(str(mini_batch_size))
print("topology: "+str(topology))
resultByTest.append(str(topology))
print("epochs: "+str(epochs))
resultByTest.append(str(epochs))
print("eta: "+str(eta))
resultByTest.append(str(eta))
print("general accuracy: "+str(accuracy[10]))
resultByTest.append(str(accuracy[10]))
print("accuracy 0: "+str(accuracy[0]))
resultByTest.append(str(accuracy[0]))
print("accuracy 1: "+str(accuracy[1]))
resultByTest.append(str(accuracy[1]))
print("accuracy 2: "+str(accuracy[2]))
resultByTest.append(str(accuracy[2]))
print("accuracy 3: "+str(accuracy[3]))
resultByTest.append(str(accuracy[3]))
print("accuracy 4: "+str(accuracy[4]))
resultByTest.append(str(accuracy[4]))
print("accuracy 5: "+str(accuracy[5]))
resultByTest.append(str(accuracy[5]))
print("accuracy 6: "+str(accuracy[6]))
resultByTest.append(str(accuracy[7]))
print("accuracy 7: "+str(accuracy[7]))
resultByTest.append(str(accuracy[7]))
print("accuracy 8: "+str(accuracy[8]))
resultByTest.append(str(accuracy[8]))
print("accuracy 9: "+str(accuracy[9]))
resultByTest.append(str(accuracy[9]))
resultByTest.append(str(endTime-startTime))
if (hits[10]/total[10]) > best_accuracy:
best_accuracy = hits[10]/total[10]
best_eta = eta
totalResult.append(resultByTest)
#aumentando
temp_counter = temp_counter + 1
write_file(totalResult, "4Changing_eta")