Mercurial > ift6266
annotate baseline/mlp/mlp_nist.py @ 408:0580b310dadb
merge
author | Xavier Glorot <glorotxa@iro.umontreal.ca> |
---|---|
date | Wed, 28 Apr 2010 14:35:30 -0400 |
parents | 195f95c3d461 |
children | 3dba84c0fbc1 |
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 | |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
26 import sys |
110 | 27 import pdb |
28 import numpy | |
29 import pylab | |
30 import theano | |
31 import theano.tensor as T | |
32 import time | |
33 import theano.tensor.nnet | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
34 import pylearn |
304
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
35 import theano,pylearn.version,ift6266 |
110 | 36 from pylearn.io import filetensor as ft |
322 | 37 from ift6266 import datasets |
110 | 38 |
39 data_path = '/data/lisa/data/nist/by_class/' | |
40 | |
41 class MLP(object): | |
42 """Multi-Layer Perceptron Class | |
43 | |
44 A multilayer perceptron is a feedforward artificial neural network model | |
45 that has one layer or more of hidden units and nonlinear activations. | |
46 Intermidiate layers usually have as activation function thanh or the | |
47 sigmoid function while the top layer is a softamx layer. | |
48 """ | |
49 | |
50 | |
51 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
52 def __init__(self, input, n_in, n_hidden, n_out,learning_rate): |
110 | 53 """Initialize the parameters for the multilayer perceptron |
54 | |
55 :param input: symbolic variable that describes the input of the | |
56 architecture (one minibatch) | |
57 | |
58 :param n_in: number of input units, the dimension of the space in | |
59 which the datapoints lie | |
60 | |
61 :param n_hidden: number of hidden units | |
62 | |
63 :param n_out: number of output units, the dimension of the space in | |
64 which the labels lie | |
65 | |
66 """ | |
67 | |
68 # initialize the parameters theta = (W1,b1,W2,b2) ; note that this | |
69 # example contains only one hidden layer, but one can have as many | |
70 # layers as he/she wishes, making the network deeper. The only | |
71 # problem making the network deep this way is during learning, | |
72 # backpropagation being unable to move the network from the starting | |
73 # point towards; this is where pre-training helps, giving a good | |
74 # starting point for backpropagation, but more about this in the | |
75 # other tutorials | |
76 | |
77 # `W1` is initialized with `W1_values` which is uniformely sampled | |
78 # from -6./sqrt(n_in+n_hidden) and 6./sqrt(n_in+n_hidden) | |
79 # the output of uniform if converted using asarray to dtype | |
80 # theano.config.floatX so that the code is runable on GPU | |
81 W1_values = numpy.asarray( numpy.random.uniform( \ | |
82 low = -numpy.sqrt(6./(n_in+n_hidden)), \ | |
83 high = numpy.sqrt(6./(n_in+n_hidden)), \ | |
84 size = (n_in, n_hidden)), dtype = theano.config.floatX) | |
85 # `W2` is initialized with `W2_values` which is uniformely sampled | |
86 # from -6./sqrt(n_hidden+n_out) and 6./sqrt(n_hidden+n_out) | |
87 # the output of uniform if converted using asarray to dtype | |
88 # theano.config.floatX so that the code is runable on GPU | |
89 W2_values = numpy.asarray( numpy.random.uniform( | |
90 low = -numpy.sqrt(6./(n_hidden+n_out)), \ | |
91 high= numpy.sqrt(6./(n_hidden+n_out)),\ | |
92 size= (n_hidden, n_out)), dtype = theano.config.floatX) | |
93 | |
94 self.W1 = theano.shared( value = W1_values ) | |
95 self.b1 = theano.shared( value = numpy.zeros((n_hidden,), | |
96 dtype= theano.config.floatX)) | |
97 self.W2 = theano.shared( value = W2_values ) | |
98 self.b2 = theano.shared( value = numpy.zeros((n_out,), | |
99 dtype= theano.config.floatX)) | |
100 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
101 #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
|
102 #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
|
103 lr_value=learning_rate |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
104 self.lr=theano.shared(value=lr_value) |
110 | 105 # symbolic expression computing the values of the hidden layer |
106 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
|
107 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
108 |
110 | 109 |
110 # symbolic expression computing the values of the top layer | |
111 self.p_y_given_x= T.nnet.softmax(T.dot(self.hidden, self.W2)+self.b2) | |
112 | |
113 # compute prediction as class whose probability is maximal in | |
114 # symbolic form | |
115 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
|
116 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
|
117 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
118 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
119 |
110 | 120 |
121 # L1 norm ; one regularization option is to enforce L1 norm to | |
122 # be small | |
123 self.L1 = abs(self.W1).sum() + abs(self.W2).sum() | |
124 | |
125 # square of L2 norm ; one regularization option is to enforce | |
126 # square of L2 norm to be small | |
127 self.L2_sqr = (self.W1**2).sum() + (self.W2**2).sum() | |
128 | |
129 | |
130 | |
131 def negative_log_likelihood(self, y): | |
132 """Return the mean of the negative log-likelihood of the prediction | |
133 of this model under a given target distribution. | |
134 | |
135 .. math:: | |
136 | |
137 \frac{1}{|\mathcal{D}|}\mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = | |
138 \frac{1}{|\mathcal{D}|}\sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ | |
139 \ell (\theta=\{W,b\}, \mathcal{D}) | |
140 | |
141 | |
142 :param y: corresponds to a vector that gives for each example the | |
143 :correct label | |
144 """ | |
145 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) | |
146 | |
147 | |
148 | |
149 | |
150 def errors(self, y): | |
151 """Return a float representing the number of errors in the minibatch | |
152 over the total number of examples of the minibatch | |
153 """ | |
154 | |
155 # check if y has same dimension of y_pred | |
156 if y.ndim != self.y_pred.ndim: | |
157 raise TypeError('y should have the same shape as self.y_pred', | |
158 ('y', target.type, 'y_pred', self.y_pred.type)) | |
159 # check if y is of the correct datatype | |
160 if y.dtype.startswith('int'): | |
161 # the T.neq operator returns a vector of 0s and 1s, where 1 | |
162 # represents a mistake in prediction | |
163 return T.mean(T.neq(self.y_pred, y)) | |
164 else: | |
165 raise NotImplementedError() | |
166 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
167 def mlp_get_nist_error(model_name='/u/mullerx/ift6266h10_sandbox_db/xvm_final_lr1_p073/8/best_model.npy.npz', |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
168 data_set=0): |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
169 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
170 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
171 |
404 | 172 |
173 | |
174 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
175 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
176 # load the data set and create an mlp based on the dimensions of the model |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
177 model=numpy.load(model_name) |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
178 W1=model['W1'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
179 W2=model['W2'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
180 b1=model['b1'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
181 b2=model['b2'] |
404 | 182 |
183 total_error_count=0.0 | |
184 total_exemple_count=0.0 | |
185 | |
186 nb_error_count=0.0 | |
187 nb_exemple_count=0.0 | |
188 | |
189 char_error_count=0.0 | |
190 char_exemple_count=0.0 | |
191 | |
192 min_error_count=0.0 | |
193 min_exemple_count=0.0 | |
194 | |
195 maj_error_count=0.0 | |
196 maj_exemple_count=0.0 | |
197 | |
198 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
199 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
200 if data_set==0: |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
201 dataset=datasets.nist_all() |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
202 elif data_set==1: |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
203 dataset=datasets.nist_P07() |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
204 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
205 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
206 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
207 #get the test error |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
208 #use a batch size of 1 so we can get the sub-class error |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
209 #without messing with matrices (will be upgraded later) |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
210 test_score=0 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
211 temp=0 |
404 | 212 for xt,yt in dataset.test(1): |
213 | |
214 total_exemple_count = total_exemple_count +1 | |
215 #get activation for layer 1 | |
216 a0=numpy.dot(numpy.transpose(W1),numpy.transpose(xt[0])) + b1 | |
217 #add non linear function to layer 1 activation | |
218 a0_out=numpy.tanh(a0) | |
219 | |
220 #get activation for output layer | |
221 a1= numpy.dot(numpy.transpose(W2),a0_out) + b2 | |
222 #add non linear function for output activation (softmax) | |
223 a1_exp = numpy.exp(a1) | |
224 sum_a1=numpy.sum(a1_exp) | |
225 a1_out=a1_exp/sum_a1 | |
226 | |
227 predicted_class=numpy.argmax(a1_out) | |
228 wanted_class=yt[0] | |
229 if(predicted_class!=wanted_class): | |
230 total_error_count = total_error_count +1 | |
231 | |
232 #treat digit error | |
233 if(wanted_class<10): | |
234 nb_exemple_count=nb_exemple_count + 1 | |
235 predicted_class=numpy.argmax(a1_out[0:10]) | |
236 if(predicted_class!=wanted_class): | |
237 nb_error_count = nb_error_count +1 | |
238 | |
239 if(wanted_class>9): | |
240 char_exemple_count=char_exemple_count + 1 | |
241 predicted_class=numpy.argmax(a1_out[10:62])+10 | |
242 if((predicted_class!=wanted_class) and ((predicted_class+26)!=wanted_class) and ((predicted_class-26)!=wanted_class)): | |
243 char_error_count = char_error_count +1 | |
405 | 244 |
245 #minuscule | |
246 if(wanted_class>9 and wanted_class<36): | |
247 maj_exemple_count=maj_exemple_count + 1 | |
248 predicted_class=numpy.argmax(a1_out[10:35])+10 | |
249 if(predicted_class!=wanted_class): | |
250 maj_error_count = maj_error_count +1 | |
251 #majuscule | |
252 if(wanted_class>35): | |
253 min_exemple_count=min_exemple_count + 1 | |
254 predicted_class=numpy.argmax(a1_out[36:62])+36 | |
255 if(predicted_class!=wanted_class): | |
256 min_error_count = min_error_count +1 | |
404 | 257 |
258 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
259 |
404 | 260 print (('total error = %f') % ((total_error_count/total_exemple_count)*100.0)) |
261 print (('number error = %f') % ((nb_error_count/nb_exemple_count)*100.0)) | |
262 print (('char error = %f') % ((char_error_count/char_exemple_count)*100.0)) | |
405 | 263 print (('min error = %f') % ((min_error_count/min_exemple_count)*100.0)) |
264 print (('maj error = %f') % ((maj_error_count/maj_exemple_count)*100.0)) | |
404 | 265 return (total_error_count/total_exemple_count)*100.0 |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
266 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
267 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
268 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
269 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
270 |
110 | 271 |
304
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
272 def mlp_full_nist( verbose = 1,\ |
145
8ceaaf812891
changed adaptive lr flag from bool to int for jobman issues
XavierMuller
parents:
143
diff
changeset
|
273 adaptive_lr = 0,\ |
322 | 274 data_set=0,\ |
110 | 275 learning_rate=0.01,\ |
276 L1_reg = 0.00,\ | |
277 L2_reg = 0.0001,\ | |
278 nb_max_exemples=1000000,\ | |
279 batch_size=20,\ | |
322 | 280 nb_hidden = 30,\ |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
281 nb_targets = 62, |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
282 tau=1e6,\ |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
283 lr_t2_factor=0.5,\ |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
284 init_model=0,\ |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
285 channel=0): |
110 | 286 |
287 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
288 if channel!=0: |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
289 channel.save() |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
290 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
|
291 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
292 #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
|
293 initial_lr=learning_rate |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
294 max_div_count=1000 |
323 | 295 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
296 |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
297 total_validation_error_list = [] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
298 total_train_error_list = [] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
299 learning_rate_list=[] |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
300 best_training_error=float('inf'); |
323 | 301 divergence_flag_list=[] |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
302 |
322 | 303 if data_set==0: |
378 | 304 print 'using nist' |
322 | 305 dataset=datasets.nist_all() |
323 | 306 elif data_set==1: |
378 | 307 print 'using p07' |
323 | 308 dataset=datasets.nist_P07() |
349
22efb4968054
added pnist support, will check in code for data set iterator later
xaviermuller
parents:
338
diff
changeset
|
309 elif data_set==2: |
378 | 310 print 'using pnist' |
349
22efb4968054
added pnist support, will check in code for data set iterator later
xaviermuller
parents:
338
diff
changeset
|
311 dataset=datasets.PNIST07() |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
312 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
313 |
110 | 314 |
315 | |
316 ishape = (32,32) # this is the size of NIST images | |
317 | |
318 # allocate symbolic variables for the data | |
319 x = T.fmatrix() # the data is presented as rasterized images | |
320 y = T.lvector() # the labels are presented as 1D vector of | |
321 # [long int] labels | |
322 | |
322 | 323 |
110 | 324 # construct the logistic regression class |
322 | 325 classifier = MLP( input=x,\ |
110 | 326 n_in=32*32,\ |
327 n_hidden=nb_hidden,\ | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
328 n_out=nb_targets, |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
329 learning_rate=learning_rate) |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
330 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
331 |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
332 # check if we want to initialise the weights with a previously calculated model |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
333 # dimensions must be consistent between old model and current configuration!!!!!! (nb_hidden and nb_targets) |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
334 if init_model!=0: |
378 | 335 print 'using old model' |
336 print init_model | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
337 old_model=numpy.load(init_model) |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
338 classifier.W1.value=old_model['W1'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
339 classifier.W2.value=old_model['W2'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
340 classifier.b1.value=old_model['b1'] |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
341 classifier.b2.value=old_model['b2'] |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
342 |
110 | 343 |
344 # the cost we minimize during training is the negative log likelihood of | |
345 # the model plus the regularization terms (L1 and L2); cost is expressed | |
346 # here symbolically | |
347 cost = classifier.negative_log_likelihood(y) \ | |
348 + L1_reg * classifier.L1 \ | |
349 + L2_reg * classifier.L2_sqr | |
350 | |
351 # compiling a theano function that computes the mistakes that are made by | |
352 # the model on a minibatch | |
353 test_model = theano.function([x,y], classifier.errors(y)) | |
354 | |
355 # compute the gradient of cost with respect to theta = (W1, b1, W2, b2) | |
356 g_W1 = T.grad(cost, classifier.W1) | |
357 g_b1 = T.grad(cost, classifier.b1) | |
358 g_W2 = T.grad(cost, classifier.W2) | |
359 g_b2 = T.grad(cost, classifier.b2) | |
360 | |
361 # specify how to update the parameters of the model as a dictionary | |
362 updates = \ | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
363 { 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
|
364 , 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
|
365 , 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
|
366 , classifier.b2: classifier.b2 - classifier.lr*g_b2 } |
110 | 367 |
368 # compiling a theano function `train_model` that returns the cost, but in | |
369 # the same time updates the parameter of the model based on the rules | |
370 # defined in `updates` | |
371 train_model = theano.function([x, y], cost, updates = updates ) | |
322 | 372 |
373 | |
374 | |
110 | 375 |
376 | |
377 | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
378 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
379 |
110 | 380 |
381 #conditions for stopping the adaptation: | |
323 | 382 #1) we have reached nb_max_exemples (this is rounded up to be a multiple of the train size so we always do at least 1 epoch) |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
383 #2) validation error is going up twice in a row(probable overfitting) |
110 | 384 |
385 # This means we no longer stop on slow convergence as low learning rates stopped | |
323 | 386 # too fast but instead we will wait for the valid error going up 3 times in a row |
387 # We save the curb of the validation error so we can always go back to check on it | |
388 # and we save the absolute best model anyway, so we might as well explore | |
389 # a bit when diverging | |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
390 |
323 | 391 #approximate number of samples in the nist training set |
322 | 392 #this is just to have a validation frequency |
323 | 393 #roughly proportionnal to the original nist training set |
322 | 394 n_minibatches = 650000/batch_size |
395 | |
396 | |
323 | 397 patience =2*nb_max_exemples/batch_size #in units of minibatch |
110 | 398 validation_frequency = n_minibatches/4 |
399 | |
400 | |
401 | |
402 | |
322 | 403 |
110 | 404 best_validation_loss = float('inf') |
405 best_iter = 0 | |
406 test_score = 0. | |
407 start_time = time.clock() | |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
408 time_n=0 #in unit of exemples |
322 | 409 minibatch_index=0 |
410 epoch=0 | |
411 temp=0 | |
323 | 412 divergence_flag=0 |
322 | 413 |
212
e390b0454515
added classic lr time decay and py code to calculate the error based on a saved model
xaviermuller
parents:
169
diff
changeset
|
414 |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
415 |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
416 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
417 print 'starting training' |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
418 sys.stdout.flush() |
322 | 419 while(minibatch_index*batch_size<nb_max_exemples): |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
420 |
322 | 421 for x, y in dataset.train(batch_size): |
110 | 422 |
323 | 423 #if we are using the classic learning rate deacay, adjust it before training of current mini-batch |
322 | 424 if adaptive_lr==2: |
425 classifier.lr.value = tau*initial_lr/(tau+time_n) | |
426 | |
427 | |
428 #train model | |
429 cost_ij = train_model(x,y) | |
430 | |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
431 if (minibatch_index) % validation_frequency == 0: |
322 | 432 #save the current learning rate |
433 learning_rate_list.append(classifier.lr.value) | |
323 | 434 divergence_flag_list.append(divergence_flag) |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
435 |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
436 |
322 | 437 |
438 # compute the validation error | |
439 this_validation_loss = 0. | |
440 temp=0 | |
441 for xv,yv in dataset.valid(1): | |
442 # sum up the errors for each minibatch | |
323 | 443 this_validation_loss += test_model(xv,yv) |
322 | 444 temp=temp+1 |
445 # get the average by dividing with the number of minibatches | |
446 this_validation_loss /= temp | |
447 #save the validation loss | |
448 total_validation_error_list.append(this_validation_loss) | |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
449 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
450 print(('epoch %i, minibatch %i, learning rate %f current validation error %f ') % |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
451 (epoch, minibatch_index+1,classifier.lr.value, |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
452 this_validation_loss*100.)) |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
453 sys.stdout.flush() |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
454 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
455 #save temp results to check during training |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
456 numpy.savez('temp_results.npy',config=configuration,total_validation_error_list=total_validation_error_list,\ |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
457 learning_rate_list=learning_rate_list, divergence_flag_list=divergence_flag_list) |
322 | 458 |
459 # if we got the best validation score until now | |
460 if this_validation_loss < best_validation_loss: | |
461 # save best validation score and iteration number | |
462 best_validation_loss = this_validation_loss | |
463 best_iter = minibatch_index | |
323 | 464 #reset divergence flag |
465 divergence_flag=0 | |
466 | |
467 #save the best model. Overwrite the current saved best model so | |
468 #we only keep the best | |
469 numpy.savez('best_model.npy', config=configuration, W1=classifier.W1.value, W2=classifier.W2.value, b1=classifier.b1.value,\ | |
470 b2=classifier.b2.value, minibatch_index=minibatch_index) | |
471 | |
322 | 472 # test it on the test set |
473 test_score = 0. | |
474 temp =0 | |
475 for xt,yt in dataset.test(batch_size): | |
476 test_score += test_model(xt,yt) | |
477 temp = temp+1 | |
478 test_score /= temp | |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
479 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
480 print(('epoch %i, minibatch %i, test error of best ' |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
481 'model %f %%') % |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
482 (epoch, minibatch_index+1, |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
483 test_score*100.)) |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
484 sys.stdout.flush() |
322 | 485 |
486 # if the validation error is going up, we are overfitting (or oscillating) | |
323 | 487 # check if we are allowed to continue and if we will adjust the learning rate |
322 | 488 elif this_validation_loss >= best_validation_loss: |
323 | 489 |
490 | |
491 # In non-classic learning rate decay, we modify the weight only when | |
492 # validation error is going up | |
493 if adaptive_lr==1: | |
494 classifier.lr.value=classifier.lr.value*lr_t2_factor | |
495 | |
496 | |
497 #cap the patience so we are allowed to diverge max_div_count times | |
498 #if we are going up max_div_count in a row, we will stop immediatelty by modifying the patience | |
499 divergence_flag = divergence_flag +1 | |
500 | |
501 | |
322 | 502 #calculate the test error at this point and exit |
503 # test it on the test set | |
504 test_score = 0. | |
505 temp=0 | |
506 for xt,yt in dataset.test(batch_size): | |
507 test_score += test_model(xt,yt) | |
508 temp=temp+1 | |
509 test_score /= temp | |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
510 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
511 print ' validation error is going up, possibly stopping soon' |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
512 print((' epoch %i, minibatch %i, test error of best ' |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
513 'model %f %%') % |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
514 (epoch, minibatch_index+1, |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
515 test_score*100.)) |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
516 sys.stdout.flush() |
322 | 517 |
518 | |
519 | |
323 | 520 # check early stop condition |
521 if divergence_flag==max_div_count: | |
522 minibatch_index=nb_max_exemples | |
523 print 'we have diverged, early stopping kicks in' | |
524 break | |
525 | |
526 #check if we have seen enough exemples | |
527 #force one epoch at least | |
528 if epoch>0 and minibatch_index*batch_size>nb_max_exemples: | |
322 | 529 break |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
530 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
531 |
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
532 |
322 | 533 |
534 | |
535 time_n= time_n + batch_size | |
323 | 536 minibatch_index = minibatch_index + 1 |
537 | |
538 # we have finished looping through the training set | |
322 | 539 epoch = epoch+1 |
110 | 540 end_time = time.clock() |
355
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
541 |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
542 print(('Optimization complete. Best validation score of %f %% ' |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
543 'obtained at iteration %i, with test performance %f %%') % |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
544 (best_validation_loss * 100., best_iter, test_score*100.)) |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
545 print ('The code ran for %f minutes' % ((end_time-start_time)/60.)) |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
546 print minibatch_index |
76b7182dd32e
added support for pnist in iterator. corrected a print bug in mlp
xaviermuller
parents:
349
diff
changeset
|
547 sys.stdout.flush() |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
548 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
549 #save the model and the weights |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
550 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
|
551 numpy.savez('results.npy',config=configuration,total_train_error_list=total_train_error_list,total_validation_error_list=total_validation_error_list,\ |
323 | 552 learning_rate_list=learning_rate_list, divergence_flag_list=divergence_flag_list) |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
553 |
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
554 return (best_training_error*100.0,best_validation_loss * 100.,test_score*100.,best_iter*batch_size,(end_time-start_time)/60) |
110 | 555 |
556 | |
557 if __name__ == '__main__': | |
558 mlp_full_mnist() | |
559 | |
560 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
|
561 (train_error,validation_error,test_error,nb_exemples,time)=mlp_full_nist(learning_rate=state.learning_rate,\ |
304
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
562 nb_max_exemples=state.nb_max_exemples,\ |
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
563 nb_hidden=state.nb_hidden,\ |
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
564 adaptive_lr=state.adaptive_lr,\ |
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
565 tau=state.tau,\ |
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
566 verbose = state.verbose,\ |
324 | 567 lr_t2_factor=state.lr_t2_factor, |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
568 data_set=state.data_set, |
378 | 569 init_model=state.init_model, |
338
fca22114bb23
added async save, restart from old model and independant error calculation based on Arnaud's iterator
xaviermuller
parents:
324
diff
changeset
|
570 channel=channel) |
143
f341a4efb44a
added adaptive lr, weight file save, traine error and error curves
XavierMuller
parents:
110
diff
changeset
|
571 state.train_error=train_error |
110 | 572 state.validation_error=validation_error |
573 state.test_error=test_error | |
574 state.nb_exemples=nb_exemples | |
575 state.time=time | |
304
1e4bf5a5b46d
added type 2 adaptive learning configurable learning weight + versionning
xaviermuller
parents:
237
diff
changeset
|
576 pylearn.version.record_versions(state,[theano,ift6266,pylearn]) |
110 | 577 return channel.COMPLETE |
578 | |
579 |