forked from MichalDanielDobrzanski/DeepLearningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiedVersion4.py
More file actions
216 lines (185 loc) · 7.28 KB
/
Copy pathmodifiedVersion4.py
File metadata and controls
216 lines (185 loc) · 7.28 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
def testTheano():
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time
print("Testing Theano library...")
vlen = 10 * 30 * 768 # 10 x #cores x # threads per core
iters = 1000
rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')
# Perform check:
#testTheano()
def write_file(listt, name):
f= open(name+".csv","w+")
for i in listt:
for j in i:
f.write(str(j)+"; ")
f.write("\n")
def dump_file(listt, name):
f= open(name+".pickle","wb+")
pickle.dump(listt, f)
# ----------------------
# - network3.py example:
import network3
from network3 import Network, ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer # softmax plus log-likelihood cost is more common in modern image classification networks.
# read data:
training_data, validation_data, test_data = network3.load_data_shared()
# chapter 6 - rectified linear units and some l2 regularization (lmbda=0.1) => even better accuracy
from network3 import ReLU
import pickle
import numpy as np
#%%
result = []
filter_size=1
best_result=0
#mini_batch_size = 10
#epochs = 90
#learning_rate = 0.03
#regularization_factor = 0.1
#topology = [
# ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
# filter_shape=(20, 1, 1, 1),
# poolsize=(2, 2),
# activation_fn=ReLU),
# ConvPoolLayer(image_shape=(mini_batch_size, 20, 14, 14),
# filter_shape=(40, 20, 1, 1),
# poolsize=(2, 2),
# activation_fn=ReLU),
# FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
# SoftmaxLayer(n_in=100, n_out=10)]
#
#net = Network(topology, mini_batch_size)
#result.append(net.SGD(training_data, epochs, mini_batch_size, learning_rate, validation_data, test_data, lmbda=regularization_factor))
#dump_file(result, "result_pickle"+str(filter_size))
#result_mini_batch = []
#best_number_mini_batch = 0
#mini_batch_size = 1
#epochs = 5
#learning_rate = 0.03
#regularization_factor = 0.1
#for j in range(0,10):
# mini_batch_size = mini_batch_size + j
# result = []
# topology = [
# ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
# filter_shape=(20, 1, 5, 5),
# poolsize=(2, 2),
# activation_fn=ReLU),
# ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 12),
# filter_shape=(40, 20, 5, 5),
# poolsize=(2, 2),
# activation_fn=ReLU),
# FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
# SoftmaxLayer(n_in=100, n_out=10)]
#
# net = Network(topology, mini_batch_size)
# result.append(net.SGD(training_data, epochs, mini_batch_size, learning_rate, validation_data, test_data, lmbda=regularization_factor))
# dump_file(result, "result_pickle_mini_batch"+str(j))
# result_mini_batch.append(0)
# for d in range(0, len(result[0])):
# if result[0][d]>result_mini_batch[j-1]:
# result_mini_batch[j-1] = result[0][d]
#
#temp_batch = 1
#for temp in range(0, len(result_mini_batch)):
# temp_batch = temp_batch + temp
# if result_mini_batch[temp]>result_mini_batch[best_number_mini_batch]:
# best_number_mini_batch = temp_batch
#
#
#result_learning = []
#best_number_learning = 0
#mini_batch_size = best_number_mini_batch + 1
#epochs = 5
#learning_rate = 0.03
#regularization_factor = 0.1
#for j in range(1,15):
# learning_rate = j*0.03
# result = []
# topology = [
# ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
# filter_shape=(20, 1, 5, 5),
# poolsize=(2, 2),
# activation_fn=ReLU),
# ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 12),
# filter_shape=(40, 20, 5, 5),
# poolsize=(2, 2),
# activation_fn=ReLU),
# FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
# SoftmaxLayer(n_in=100, n_out=10)]
#
# net = Network(topology, mini_batch_size)
# result.append(net.SGD(training_data, epochs, mini_batch_size, learning_rate, validation_data, test_data, lmbda=regularization_factor))
# dump_file(result, "result_pickle_learning"+str(j))
# result_learning.append(0)
# for d in range(0, len(result[0])):
# if result[0][d]>result_learning[j-1]:
# result_learning[j-1] = result[0][d]
#
#for temp in range(0, len(result_learning)):
# if result_learning[temp]>result_learning[best_number_learning]:
# best_number_learning = temp
result_regularization = []
best_number_regularization = 0
learning_rate = 0.03*(best_number_learning+1)
regularization_factor = 0.1
for j in range(1,15):
regularization_factor = j*0.1
result = []
topology = [
ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
filter_shape=(20, 1, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 12),
filter_shape=(40, 20, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
SoftmaxLayer(n_in=100, n_out=10)]
net = Network(topology, mini_batch_size)
result.append(net.SGD(training_data, epochs, mini_batch_size, learning_rate, validation_data, test_data, lmbda=regularization_factor))
dump_file(result, "result_pickle_regularization"+str(j))
result_regularization.append(0)
for d in range(0, len(result[0])):
if result[0][d]>result_regularization[j-1]:
result_regularization[j-1] = result[0][d]
for temp in range(0, len(result_regularization)):
if result_regularization[temp]>result_regularization[best_number_regularization]:
best_number_regularization = temp
best_epoch_number = 0
best_epoch_result = 0
epochs = 160
result = []
topology = [
ConvPoolLayer(image_shape=(mini_batch_size, 1, 28, 28),
filter_shape=(20, 1, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
ConvPoolLayer(image_shape=(mini_batch_size, 20, 12, 12),
filter_shape=(40, 20, 5, 5),
poolsize=(2, 2),
activation_fn=ReLU),
FullyConnectedLayer(n_in=40*4*4, n_out=100, activation_fn=ReLU),
SoftmaxLayer(n_in=100, n_out=10)]
net = Network(topology, mini_batch_size)
result.append(net.SGD(training_data, epochs, mini_batch_size, learning_rate, validation_data, test_data, lmbda=regularization_factor))
dump_file(result, "result_pickle_epoch"+str(j))
result_regularization.append(0)
for d in range(0, len(result[0])):
if result[0][d]>best_epoch_result:
best_epoch_result = result[0][d]
best_epoch_number = d+1