Mercurial > ift6266
annotate baseline/mlp/mlp_nist.py @ 271:a92ec9939e4f
fixed a problem with maxsize when not provided
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Sun, 21 Mar 2010 19:30:05 -0400 |
parents | 9b6e0af062af |
children | 1e4bf5a5b46d |
rev | line source |
---|---|
110 | 1 """ |
2 This tutorial introduces the multilayer perceptron using Theano. | |
3 | |
4 A multilayer perceptron is a logistic regressor where | |
5 instead of feeding the input to the logistic regression you insert a | |
6 intermidiate layer, called the hidden layer, that has a nonlinear | |
7 activation function (usually tanh or sigmoid) . One can use many such | |
8 hidden layers making the architecture deep. The tutorial will also tackle | |
9 the problem of MNIST digit classification. | |
10 | |
11 .. math:: | |
12 | |
13 f(x) = G( b^{(2)} + W^{(2)}( s( b^{(1)} + W^{(1)} x))), | |
14 | |
15 References: | |
16 | |
17 - textbooks: "Pattern Recognition and Machine Learning" - | |
18 Christopher M. Bishop, section 5 | |
19 | |
20 TODO: recommended preprocessing, lr ranges, regularization ranges (explain | |
21 to do lr first, then add regularization) | |
22 | |
23 """ | |
24 __docformat__ = 'restructedtext en' | |
25 | |
26 import pdb | |
27 import numpy | |
28 import pylab | |
29 import theano | |
30 import theano.tensor as T | |
31 import time | |
32 import theano.tensor.nnet | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
33 import pylearn |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
34 import theano,pylearn.version |
110 | 35 from pylearn.io import filetensor as ft |
36 | |
37 data_path = '/data/lisa/data/nist/by_class/' | |
38 | |
39 class MLP(object): | |
40 """Multi-Layer Perceptron Class | |
41 | |
42 A multilayer perceptron is a feedforward artificial neural network model | |
43 that has one layer or more of hidden units and nonlinear activations. | |
44 Intermidiate layers usually have as activation function thanh or the | |
45 sigmoid function while the top layer is a softamx layer. | |
46 """ | |
47 | |
48 | |
49 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
50 def __init__(self, input, n_in, n_hidden, n_out,learning_rate): |
110 | 51 """Initialize the parameters for the multilayer perceptron |
52 | |
53 :param input: symbolic variable that describes the input of the | |
54 architecture (one minibatch) | |
55 | |
56 :param n_in: number of input units, the dimension of the space in | |
57 which the datapoints lie | |
58 | |
59 :param n_hidden: number of hidden units | |
60 | |
61 :param n_out: number of output units, the dimension of the space in | |
62 which the labels lie | |
63 | |
64 """ | |
65 | |
66 # initialize the parameters theta = (W1,b1,W2,b2) ; note that this | |
67 # example contains only one hidden layer, but one can have as many | |
68 # layers as he/she wishes, making the network deeper. The only | |
69 # problem making the network deep this way is during learning, | |
70 # backpropagation being unable to move the network from the starting | |
71 # point towards; this is where pre-training helps, giving a good | |
72 # starting point for backpropagation, but more about this in the | |
73 # other tutorials | |
74 | |
75 # `W1` is initialized with `W1_values` which is uniformely sampled | |
76 # from -6./sqrt(n_in+n_hidden) and 6./sqrt(n_in+n_hidden) | |
77 # the output of uniform if converted using asarray to dtype | |
78 # theano.config.floatX so that the code is runable on GPU | |
79 W1_values = numpy.asarray( numpy.random.uniform( \ | |
80 low = -numpy.sqrt(6./(n_in+n_hidden)), \ | |
81 high = numpy.sqrt(6./(n_in+n_hidden)), \ | |
82 size = (n_in, n_hidden)), dtype = theano.config.floatX) | |
83 # `W2` is initialized with `W2_values` which is uniformely sampled | |
84 # from -6./sqrt(n_hidden+n_out) and 6./sqrt(n_hidden+n_out) | |
85 # the output of uniform if converted using asarray to dtype | |
86 # theano.config.floatX so that the code is runable on GPU | |
87 W2_values = numpy.asarray( numpy.random.uniform( | |
88 low = -numpy.sqrt(6./(n_hidden+n_out)), \ | |
89 high= numpy.sqrt(6./(n_hidden+n_out)),\ | |
90 size= (n_hidden, n_out)), dtype = theano.config.floatX) | |
91 | |
92 self.W1 = theano.shared( value = W1_values ) | |
93 self.b1 = theano.shared( value = numpy.zeros((n_hidden,), | |
94 dtype= theano.config.floatX)) | |
95 self.W2 = theano.shared( value = W2_values ) | |
96 self.b2 = theano.shared( value = numpy.zeros((n_out,), | |
97 dtype= theano.config.floatX)) | |
98 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
99 #include the learning rate in the classifer so |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
100 #we can modify it on the fly when we want |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
101 lr_value=learning_rate |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
102 self.lr=theano.shared(value=lr_value) |
110 | 103 # symbolic expression computing the values of the hidden layer |
104 self.hidden = T.tanh(T.dot(input, self.W1)+ self.b1) | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
105 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
106 |
110 | 107 |
108 # symbolic expression computing the values of the top layer | |
109 self.p_y_given_x= T.nnet.softmax(T.dot(self.hidden, self.W2)+self.b2) | |
110 | |
111 # compute prediction as class whose probability is maximal in | |
112 # symbolic form | |
113 self.y_pred = T.argmax( self.p_y_given_x, axis =1) | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
114 self.y_pred_num = T.argmax( self.p_y_given_x[0:9], axis =1) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
115 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
116 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
117 |
110 | 118 |
119 # L1 norm ; one regularization option is to enforce L1 norm to | |
120 # be small | |
121 self.L1 = abs(self.W1).sum() + abs(self.W2).sum() | |
122 | |
123 # square of L2 norm ; one regularization option is to enforce | |
124 # square of L2 norm to be small | |
125 self.L2_sqr = (self.W1**2).sum() + (self.W2**2).sum() | |
126 | |
127 | |
128 | |
129 def negative_log_likelihood(self, y): | |
130 """Return the mean of the negative log-likelihood of the prediction | |
131 of this model under a given target distribution. | |
132 | |
133 .. math:: | |
134 | |
135 \frac{1}{|\mathcal{D}|}\mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = | |
136 \frac{1}{|\mathcal{D}|}\sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ | |
137 \ell (\theta=\{W,b\}, \mathcal{D}) | |
138 | |
139 | |
140 :param y: corresponds to a vector that gives for each example the | |
141 :correct label | |
142 """ | |
143 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) | |
144 | |
145 | |
146 | |
147 | |
148 def errors(self, y): | |
149 """Return a float representing the number of errors in the minibatch | |
150 over the total number of examples of the minibatch | |
151 """ | |
152 | |
153 # check if y has same dimension of y_pred | |
154 if y.ndim != self.y_pred.ndim: | |
155 raise TypeError('y should have the same shape as self.y_pred', | |
156 ('y', target.type, 'y_pred', self.y_pred.type)) | |
157 # check if y is of the correct datatype | |
158 if y.dtype.startswith('int'): | |
159 # the T.neq operator returns a vector of 0s and 1s, where 1 | |
160 # represents a mistake in prediction | |
161 return T.mean(T.neq(self.y_pred, y)) | |
162 else: | |
163 raise NotImplementedError() | |
164 | |
165 | |
166 def mlp_full_nist( verbose = False,\ | |
145
8ceaaf812891
changed adaptive lr flag from bool to int for jobman issues
XavierMuller
parents:
143
diff
changeset
|
167 adaptive_lr = 0,\ |
110 | 168 train_data = 'all/all_train_data.ft',\ |
169 train_labels = 'all/all_train_labels.ft',\ | |
170 test_data = 'all/all_test_data.ft',\ | |
171 test_labels = 'all/all_test_labels.ft',\ | |
172 learning_rate=0.01,\ | |
173 L1_reg = 0.00,\ | |
174 L2_reg = 0.0001,\ | |
175 nb_max_exemples=1000000,\ | |
176 batch_size=20,\ | |
177 nb_hidden = 500,\ | |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
178 nb_targets = 62, |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
179 tau=1e6): |
110 | 180 |
181 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
182 configuration = [learning_rate,nb_max_exemples,nb_hidden,adaptive_lr] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
183 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
184 #save initial learning rate if classical adaptive lr is used |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
185 initial_lr=learning_rate |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
186 |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
187 total_validation_error_list = [] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
188 total_train_error_list = [] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
189 learning_rate_list=[] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
190 best_training_error=float('inf'); |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
191 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
192 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
193 |
110 | 194 |
195 f = open(data_path+train_data) | |
196 g= open(data_path+train_labels) | |
197 h = open(data_path+test_data) | |
198 i= open(data_path+test_labels) | |
199 | |
200 raw_train_data = ft.read(f) | |
201 raw_train_labels = ft.read(g) | |
202 raw_test_data = ft.read(h) | |
203 raw_test_labels = ft.read(i) | |
204 | |
205 f.close() | |
206 g.close() | |
207 i.close() | |
208 h.close() | |
209 #create a validation set the same size as the test size | |
210 #use the end of the training array for this purpose | |
211 #discard the last remaining so we get a %batch_size number | |
212 test_size=len(raw_test_labels) | |
213 test_size = int(test_size/batch_size) | |
214 test_size*=batch_size | |
215 train_size = len(raw_train_data) | |
216 train_size = int(train_size/batch_size) | |
217 train_size*=batch_size | |
218 validation_size =test_size | |
219 offset = train_size-test_size | |
220 if verbose == True: | |
221 print 'train size = %d' %train_size | |
222 print 'test size = %d' %test_size | |
223 print 'valid size = %d' %validation_size | |
224 print 'offset = %d' %offset | |
225 | |
226 | |
227 train_set = (raw_train_data,raw_train_labels) | |
228 train_batches = [] | |
229 for i in xrange(0, train_size-test_size, batch_size): | |
230 train_batches = train_batches + \ | |
231 [(raw_train_data[i:i+batch_size], raw_train_labels[i:i+batch_size])] | |
232 | |
233 test_batches = [] | |
234 for i in xrange(0, test_size, batch_size): | |
235 test_batches = test_batches + \ | |
236 [(raw_test_data[i:i+batch_size], raw_test_labels[i:i+batch_size])] | |
237 | |
238 validation_batches = [] | |
239 for i in xrange(0, test_size, batch_size): | |
240 validation_batches = validation_batches + \ | |
241 [(raw_train_data[offset+i:offset+i+batch_size], raw_train_labels[offset+i:offset+i+batch_size])] | |
242 | |
243 | |
244 ishape = (32,32) # this is the size of NIST images | |
245 | |
246 # allocate symbolic variables for the data | |
247 x = T.fmatrix() # the data is presented as rasterized images | |
248 y = T.lvector() # the labels are presented as 1D vector of | |
249 # [long int] labels | |
250 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
251 if verbose==True: |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
252 print 'finished parsing the data' |
110 | 253 # construct the logistic regression class |
254 classifier = MLP( input=x.reshape((batch_size,32*32)),\ | |
255 n_in=32*32,\ | |
256 n_hidden=nb_hidden,\ | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
257 n_out=nb_targets, |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
258 learning_rate=learning_rate) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
259 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
260 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
261 |
110 | 262 |
263 # the cost we minimize during training is the negative log likelihood of | |
264 # the model plus the regularization terms (L1 and L2); cost is expressed | |
265 # here symbolically | |
266 cost = classifier.negative_log_likelihood(y) \ | |
267 + L1_reg * classifier.L1 \ | |
268 + L2_reg * classifier.L2_sqr | |
269 | |
270 # compiling a theano function that computes the mistakes that are made by | |
271 # the model on a minibatch | |
272 test_model = theano.function([x,y], classifier.errors(y)) | |
273 | |
274 # compute the gradient of cost with respect to theta = (W1, b1, W2, b2) | |
275 g_W1 = T.grad(cost, classifier.W1) | |
276 g_b1 = T.grad(cost, classifier.b1) | |
277 g_W2 = T.grad(cost, classifier.W2) | |
278 g_b2 = T.grad(cost, classifier.b2) | |
279 | |
280 # specify how to update the parameters of the model as a dictionary | |
281 updates = \ | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
282 { classifier.W1: classifier.W1 - classifier.lr*g_W1 \ |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
283 , classifier.b1: classifier.b1 - classifier.lr*g_b1 \ |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
284 , classifier.W2: classifier.W2 - classifier.lr*g_W2 \ |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
285 , classifier.b2: classifier.b2 - classifier.lr*g_b2 } |
110 | 286 |
287 # compiling a theano function `train_model` that returns the cost, but in | |
288 # the same time updates the parameter of the model based on the rules | |
289 # defined in `updates` | |
290 train_model = theano.function([x, y], cost, updates = updates ) | |
291 n_minibatches = len(train_batches) | |
292 | |
293 | |
294 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
295 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
296 |
110 | 297 |
298 #conditions for stopping the adaptation: | |
299 #1) we have reached nb_max_exemples (this is rounded up to be a multiple of the train size) | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
300 #2) validation error is going up twice in a row(probable overfitting) |
110 | 301 |
302 # This means we no longer stop on slow convergence as low learning rates stopped | |
303 # too fast. | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
304 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
305 # no longer relevant |
110 | 306 patience =nb_max_exemples/batch_size |
307 patience_increase = 2 # wait this much longer when a new best is | |
308 # found | |
309 improvement_threshold = 0.995 # a relative improvement of this much is | |
310 # considered significant | |
311 validation_frequency = n_minibatches/4 | |
312 | |
313 | |
314 | |
315 | |
316 best_params = None | |
317 best_validation_loss = float('inf') | |
318 best_iter = 0 | |
319 test_score = 0. | |
320 start_time = time.clock() | |
321 n_iter = nb_max_exemples/batch_size # nb of max times we are allowed to run through all exemples | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
322 n_iter = n_iter/n_minibatches + 1 #round up |
110 | 323 n_iter=max(1,n_iter) # run at least once on short debug call |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
324 time_n=0 #in unit of exemples |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
325 |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
326 |
110 | 327 |
328 if verbose == True: | |
329 print 'looping at most %d times through the data set' %n_iter | |
330 for iter in xrange(n_iter* n_minibatches): | |
331 | |
332 # get epoch and minibatch index | |
333 epoch = iter / n_minibatches | |
334 minibatch_index = iter % n_minibatches | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
335 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
336 |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
337 if adaptive_lr==2: |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
338 classifier.lr.value = tau*initial_lr/(tau+time_n) |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
339 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
340 |
110 | 341 # get the minibatches corresponding to `iter` modulo |
342 # `len(train_batches)` | |
343 x,y = train_batches[ minibatch_index ] | |
344 # convert to float | |
345 x_float = x/255.0 | |
346 cost_ij = train_model(x_float,y) | |
347 | |
348 if (iter+1) % validation_frequency == 0: | |
349 # compute zero-one loss on validation set | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
350 |
110 | 351 this_validation_loss = 0. |
352 for x,y in validation_batches: | |
353 # sum up the errors for each minibatch | |
354 x_float = x/255.0 | |
355 this_validation_loss += test_model(x_float,y) | |
356 # get the average by dividing with the number of minibatches | |
357 this_validation_loss /= len(validation_batches) | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
358 #save the validation loss |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
359 total_validation_error_list.append(this_validation_loss) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
360 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
361 #get the training error rate |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
362 this_train_loss=0 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
363 for x,y in train_batches: |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
364 # sum up the errors for each minibatch |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
365 x_float = x/255.0 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
366 this_train_loss += test_model(x_float,y) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
367 # get the average by dividing with the number of minibatches |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
368 this_train_loss /= len(train_batches) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
369 #save the validation loss |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
370 total_train_error_list.append(this_train_loss) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
371 if(this_train_loss<best_training_error): |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
372 best_training_error=this_train_loss |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
373 |
110 | 374 if verbose == True: |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
375 print('epoch %i, minibatch %i/%i, validation error %f, training error %f %%' % \ |
110 | 376 (epoch, minibatch_index+1, n_minibatches, \ |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
377 this_validation_loss*100.,this_train_loss*100)) |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
378 print 'learning rate = %f' %classifier.lr.value |
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
379 print 'time = %i' %time_n |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
380 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
381 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
382 #save the learning rate |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
383 learning_rate_list.append(classifier.lr.value) |
110 | 384 |
385 | |
386 # if we got the best validation score until now | |
387 if this_validation_loss < best_validation_loss: | |
388 # save best validation score and iteration number | |
389 best_validation_loss = this_validation_loss | |
390 best_iter = iter | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
391 # reset patience if we are going down again |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
392 # so we continue exploring |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
393 patience=nb_max_exemples/batch_size |
110 | 394 # test it on the test set |
395 test_score = 0. | |
396 for x,y in test_batches: | |
397 x_float=x/255.0 | |
398 test_score += test_model(x_float,y) | |
399 test_score /= len(test_batches) | |
400 if verbose == True: | |
401 print((' epoch %i, minibatch %i/%i, test error of best ' | |
402 'model %f %%') % | |
403 (epoch, minibatch_index+1, n_minibatches, | |
404 test_score*100.)) | |
405 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
406 # if the validation error is going up, we are overfitting (or oscillating) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
407 # stop converging but run at least to next validation |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
408 # to check overfitting or ocsillation |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
409 # the saved weights of the model will be a bit off in that case |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
410 elif this_validation_loss >= best_validation_loss: |
110 | 411 #calculate the test error at this point and exit |
412 # test it on the test set | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
413 # however, if adaptive_lr is true, try reducing the lr to |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
414 # get us out of an oscilliation |
145
8ceaaf812891
changed adaptive lr flag from bool to int for jobman issues
XavierMuller
parents:
143
diff
changeset
|
415 if adaptive_lr==1: |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
416 classifier.lr.value=classifier.lr.value/2.0 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
417 |
110 | 418 test_score = 0. |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
419 #cap the patience so we are allowed one more validation error |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
420 #calculation before aborting |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
421 patience = iter+validation_frequency+1 |
110 | 422 for x,y in test_batches: |
423 x_float=x/255.0 | |
424 test_score += test_model(x_float,y) | |
425 test_score /= len(test_batches) | |
426 if verbose == True: | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
427 print ' validation error is going up, possibly stopping soon' |
110 | 428 print((' epoch %i, minibatch %i/%i, test error of best ' |
429 'model %f %%') % | |
430 (epoch, minibatch_index+1, n_minibatches, | |
431 test_score*100.)) | |
432 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
433 |
110 | 434 |
435 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
436 if iter>patience: |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
437 print 'we have diverged' |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
438 break |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
439 |
110 | 440 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
441 time_n= time_n + batch_size |
110 | 442 end_time = time.clock() |
443 if verbose == True: | |
444 print(('Optimization complete. Best validation score of %f %% ' | |
445 'obtained at iteration %i, with test performance %f %%') % | |
446 (best_validation_loss * 100., best_iter, test_score*100.)) | |
447 print ('The code ran for %f minutes' % ((end_time-start_time)/60.)) | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
448 print iter |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
449 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
450 #save the model and the weights |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
451 numpy.savez('model.npy', config=configuration, W1=classifier.W1.value,W2=classifier.W2.value, b1=classifier.b1.value,b2=classifier.b2.value) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
452 numpy.savez('results.npy',config=configuration,total_train_error_list=total_train_error_list,total_validation_error_list=total_validation_error_list,\ |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
453 learning_rate_list=learning_rate_list) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
454 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
455 return (best_training_error*100.0,best_validation_loss * 100.,test_score*100.,best_iter*batch_size,(end_time-start_time)/60) |
110 | 456 |
457 | |
458 if __name__ == '__main__': | |
459 mlp_full_mnist() | |
460 | |
461 def jobman_mlp_full_nist(state,channel): | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
462 (train_error,validation_error,test_error,nb_exemples,time)=mlp_full_nist(learning_rate=state.learning_rate,\ |
110 | 463 nb_max_exemples=state.nb_max_exemples,\ |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
464 nb_hidden=state.nb_hidden,\ |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
465 adaptive_lr=state.adaptive_lr,\ |
237 | 466 tau=state.tau) |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
467 state.train_error=train_error |
110 | 468 state.validation_error=validation_error |
469 state.test_error=test_error | |
470 state.nb_exemples=nb_exemples | |
471 state.time=time | |
472 return channel.COMPLETE | |
473 | |
474 |