comparison baseline/conv_mlp/convolutional_mlp.py @ 253:a491d3600a77

Derniere version du reseau a convolution
author Jeremy Eustache <jeremy.eustache@voila.fr>
date Wed, 17 Mar 2010 10:21:57 -0400
parents 168aae8a6419
children d41fe003fade
comparison
equal deleted inserted replaced
252:7dd43ef66d15 253:a491d3600a77
24 import numpy, theano, cPickle, gzip, time 24 import numpy, theano, cPickle, gzip, time
25 import theano.tensor as T 25 import theano.tensor as T
26 import theano.sandbox.softsign 26 import theano.sandbox.softsign
27 import pylearn.datasets.MNIST 27 import pylearn.datasets.MNIST
28 from pylearn.io import filetensor as ft 28 from pylearn.io import filetensor as ft
29 from theano.tensor.signal import downsample 29 from theano.sandbox import conv, downsample
30 from theano.tensor.nnet import conv 30 import theano,pylearn.version,ift6266
31 31
32 class LeNetConvPoolLayer(object): 32 class LeNetConvPoolLayer(object):
33 33
34 def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2,2)): 34 def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2,2)):
35 """ 35 """
212 # make minibatches of size 20 212 # make minibatches of size 20
213 batch_size = batch # sized of the minibatch 213 batch_size = batch # sized of the minibatch
214 214
215 # Dealing with the training set 215 # Dealing with the training set
216 # get the list of training images (x) and their labels (y) 216 # get the list of training images (x) and their labels (y)
217 (train_set_x, train_set_y) = (d[:4000,:],labels[:4000]) 217 (train_set_x, train_set_y) = (d[:200000,:],labels[:200000])
218 # initialize the list of training minibatches with empty list 218 # initialize the list of training minibatches with empty list
219 train_batches = [] 219 train_batches = []
220 for i in xrange(0, len(train_set_x), batch_size): 220 for i in xrange(0, len(train_set_x), batch_size):
221 # add to the list of minibatches the minibatch starting at 221 # add to the list of minibatches the minibatch starting at
222 # position i, ending at position i+batch_size 222 # position i, ending at position i+batch_size
227 [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])] 227 [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
228 228
229 #print train_batches[500] 229 #print train_batches[500]
230 230
231 # Dealing with the validation set 231 # Dealing with the validation set
232 (valid_set_x, valid_set_y) = (d[4000:5000,:],labels[4000:5000]) 232 (valid_set_x, valid_set_y) = (d[200000:270000,:],labels[200000:270000])
233 # initialize the list of validation minibatches 233 # initialize the list of validation minibatches
234 valid_batches = [] 234 valid_batches = []
235 for i in xrange(0, len(valid_set_x), batch_size): 235 for i in xrange(0, len(valid_set_x), batch_size):
236 valid_batches = valid_batches + \ 236 valid_batches = valid_batches + \
237 [(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])] 237 [(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
238 238
239 # Dealing with the testing set 239 # Dealing with the testing set
240 (test_set_x, test_set_y) = (d[5000:6000,:],labels[5000:6000]) 240 (test_set_x, test_set_y) = (d[270000:340000,:],labels[270000:340000])
241 # initialize the list of testing minibatches 241 # initialize the list of testing minibatches
242 test_batches = [] 242 test_batches = []
243 for i in xrange(0, len(test_set_x), batch_size): 243 for i in xrange(0, len(test_set_x), batch_size):
244 test_batches = test_batches + \ 244 test_batches = test_batches + \
245 [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])] 245 [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
246 246
247
247 return train_batches, valid_batches, test_batches 248 return train_batches, valid_batches, test_batches
248 249
249 250
250 def evaluate_lenet5(learning_rate=0.1, n_iter=1, batch_size=20, n_kern0=20,n_kern1=50,filter_shape=5,n_layer=3, dataset='mnist.pkl.gz'): 251 def evaluate_lenet5(learning_rate=0.1, n_iter=200, batch_size=20, n_kern0=20, n_kern1=50, n_layer=3, filter_shape0=5, filter_shape1=5, dataset='mnist.pkl.gz'):
251 rng = numpy.random.RandomState(23455) 252 rng = numpy.random.RandomState(23455)
252 253
253 print 'Before load dataset' 254 print 'Before load dataset'
254 train_batches, valid_batches, test_batches = load_dataset(dataset,batch_size) 255 train_batches, valid_batches, test_batches = load_dataset(dataset,batch_size)
255 print 'After load dataset' 256 print 'After load dataset'
256 257
257 ishape = (32,32) # this is the size of NIST images 258 ishape = (32,32) # this is the size of NIST images
258 n_kern2=80 259 n_kern2=80
260 n_kern3=100
261 if n_layer==4:
262 filter_shape1=3
263 filter_shape2=3
264 if n_layer==5:
265 filter_shape0=4
266 filter_shape1=2
267 filter_shape2=2
268 filter_shape3=2
269
259 270
260 # allocate symbolic variables for the data 271 # allocate symbolic variables for the data
261 x = T.matrix('x') # rasterized images 272 x = T.matrix('x') # rasterized images
262 y = T.lvector() # the labels are presented as 1D vector of [long int] labels 273 y = T.lvector() # the labels are presented as 1D vector of [long int] labels
263 274
274 # filtering reduces the image size to (32-5+1,32-5+1)=(28,28) 285 # filtering reduces the image size to (32-5+1,32-5+1)=(28,28)
275 # maxpooling reduces this further to (28/2,28/2) = (14,14) 286 # maxpooling reduces this further to (28/2,28/2) = (14,14)
276 # 4D output tensor is thus of shape (20,20,14,14) 287 # 4D output tensor is thus of shape (20,20,14,14)
277 layer0 = LeNetConvPoolLayer(rng, input=layer0_input, 288 layer0 = LeNetConvPoolLayer(rng, input=layer0_input,
278 image_shape=(batch_size,1,32,32), 289 image_shape=(batch_size,1,32,32),
279 filter_shape=(n_kern0,1,filter_shape,filter_shape), poolsize=(2,2)) 290 filter_shape=(n_kern0,1,filter_shape0,filter_shape0), poolsize=(2,2))
280 291
281 if(n_layer>2): 292 if(n_layer>2):
282 293
283 # Construct the second convolutional pooling layer 294 # Construct the second convolutional pooling layer
284 # filtering reduces the image size to (14-5+1,14-5+1)=(10,10) 295 # filtering reduces the image size to (14-5+1,14-5+1)=(10,10)
285 # maxpooling reduces this further to (10/2,10/2) = (5,5) 296 # maxpooling reduces this further to (10/2,10/2) = (5,5)
286 # 4D output tensor is thus of shape (20,50,5,5) 297 # 4D output tensor is thus of shape (20,50,5,5)
287 fshape=(32-filter_shape+1)/2 298 fshape0=(32-filter_shape0+1)/2
288 layer1 = LeNetConvPoolLayer(rng, input=layer0.output, 299 layer1 = LeNetConvPoolLayer(rng, input=layer0.output,
289 image_shape=(batch_size,n_kern0,fshape,fshape), 300 image_shape=(batch_size,n_kern0,fshape0,fshape0),
290 filter_shape=(n_kern1,n_kern0,filter_shape,filter_shape), poolsize=(2,2)) 301 filter_shape=(n_kern1,n_kern0,filter_shape1,filter_shape1), poolsize=(2,2))
291 302
292 else: 303 else:
293 304
294 fshape=(32-filter_shape+1)/2 305 fshape0=(32-filter_shape0+1)/2
295 layer1_input = layer0.output.flatten(2) 306 layer1_input = layer0.output.flatten(2)
296 # construct a fully-connected sigmoidal layer 307 # construct a fully-connected sigmoidal layer
297 layer1 = SigmoidalLayer(rng, input=layer1_input,n_in=n_kern0*fshape*fshape, n_out=500) 308 layer1 = SigmoidalLayer(rng, input=layer1_input,n_in=n_kern0*fshape0*fshape0, n_out=500)
298 309
299 layer2 = LogisticRegression(input=layer1.output, n_in=500, n_out=10) 310 layer2 = LogisticRegression(input=layer1.output, n_in=500, n_out=10)
300 cost = layer2.negative_log_likelihood(y) 311 cost = layer2.negative_log_likelihood(y)
301 test_model = theano.function([x,y], layer2.errors(y)) 312 test_model = theano.function([x,y], layer2.errors(y))
302 params = layer2.params+ layer1.params + layer0.params 313 params = layer2.params+ layer1.params + layer0.params
303 314
304 315
305 if(n_layer>3): 316 if(n_layer>3):
306 317
307 fshape=(32-filter_shape+1)/2 318 fshape0=(32-filter_shape0+1)/2
308 fshape2=(fshape-filter_shape+1)/2 319 fshape1=(fshape0-filter_shape1+1)/2
309 fshape3=(fshape2-filter_shape+1)/2
310 layer2 = LeNetConvPoolLayer(rng, input=layer1.output, 320 layer2 = LeNetConvPoolLayer(rng, input=layer1.output,
311 image_shape=(batch_size,n_kern1,fshape2,fshape2), 321 image_shape=(batch_size,n_kern1,fshape1,fshape1),
312 filter_shape=(n_kern2,n_kern1,filter_shape,filter_shape), poolsize=(2,2)) 322 filter_shape=(n_kern2,n_kern1,filter_shape2,filter_shape2), poolsize=(2,2))
313 323
324 if(n_layer>4):
325
326
327 fshape0=(32-filter_shape0+1)/2
328 fshape1=(fshape0-filter_shape1+1)/2
329 fshape2=(fshape1-filter_shape2+1)/2
330 fshape3=(fshape2-filter_shape3+1)/2
331 layer3 = LeNetConvPoolLayer(rng, input=layer2.output,
332 image_shape=(batch_size,n_kern2,fshape2,fshape2),
333 filter_shape=(n_kern3,n_kern2,filter_shape3,filter_shape3), poolsize=(2,2))
334
335 layer4_input = layer3.output.flatten(2)
336
337 layer4 = SigmoidalLayer(rng, input=layer4_input,
338 n_in=n_kern3*fshape3*fshape3, n_out=500)
339
340
341 layer5 = LogisticRegression(input=layer4.output, n_in=500, n_out=10)
342
343 cost = layer5.negative_log_likelihood(y)
344
345 test_model = theano.function([x,y], layer5.errors(y))
346
347 params = layer5.params+ layer4.params+ layer3.params+ layer2.params+ layer1.params + layer0.params
348
349 elif(n_layer>3):
350
351 fshape0=(32-filter_shape0+1)/2
352 fshape1=(fshape0-filter_shape1+1)/2
353 fshape2=(fshape1-filter_shape2+1)/2
314 layer3_input = layer2.output.flatten(2) 354 layer3_input = layer2.output.flatten(2)
315 355
316 layer3 = SigmoidalLayer(rng, input=layer3_input, 356 layer3 = SigmoidalLayer(rng, input=layer3_input,
317 n_in=n_kern2*fshape3*fshape3, n_out=500) 357 n_in=n_kern2*fshape2*fshape2, n_out=500)
318 358
319 359
320 layer4 = LogisticRegression(input=layer3.output, n_in=500, n_out=10) 360 layer4 = LogisticRegression(input=layer3.output, n_in=500, n_out=10)
321 361
322 cost = layer4.negative_log_likelihood(y) 362 cost = layer4.negative_log_likelihood(y)
326 params = layer4.params+ layer3.params+ layer2.params+ layer1.params + layer0.params 366 params = layer4.params+ layer3.params+ layer2.params+ layer1.params + layer0.params
327 367
328 368
329 elif(n_layer>2): 369 elif(n_layer>2):
330 370
331 fshape=(32-filter_shape+1)/2 371 fshape0=(32-filter_shape0+1)/2
332 fshape2=(fshape-filter_shape+1)/2 372 fshape1=(fshape0-filter_shape1+1)/2
333 373
334 # the SigmoidalLayer being fully-connected, it operates on 2D matrices of 374 # the SigmoidalLayer being fully-connected, it operates on 2D matrices of
335 # shape (batch_size,num_pixels) (i.e matrix of rasterized images). 375 # shape (batch_size,num_pixels) (i.e matrix of rasterized images).
336 # This will generate a matrix of shape (20,32*4*4) = (20,512) 376 # This will generate a matrix of shape (20,32*4*4) = (20,512)
337 layer2_input = layer1.output.flatten(2) 377 layer2_input = layer1.output.flatten(2)
338 378
339 # construct a fully-connected sigmoidal layer 379 # construct a fully-connected sigmoidal layer
340 layer2 = SigmoidalLayer(rng, input=layer2_input, 380 layer2 = SigmoidalLayer(rng, input=layer2_input,
341 n_in=n_kern1*fshape2*fshape2, n_out=500) 381 n_in=n_kern1*fshape1*fshape1, n_out=500)
342 382
343 383
344 # classify the values of the fully-connected sigmoidal layer 384 # classify the values of the fully-connected sigmoidal layer
345 layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10) 385 layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)
346 386
460 if __name__ == '__main__': 500 if __name__ == '__main__':
461 evaluate_lenet5() 501 evaluate_lenet5()
462 502
463 def experiment(state, channel): 503 def experiment(state, channel):
464 print 'start experiment' 504 print 'start experiment'
465 (best_validation_loss, test_score, minutes_trained, iter) = evaluate_lenet5(state.learning_rate, state.n_iter, state.batch_size, state.n_kern0, state.n_kern1, state.filter_shape, state.n_layer) 505 (best_validation_loss, test_score, minutes_trained, iter) = evaluate_lenet5(state.learning_rate, state.n_iter, state.batch_size, state.n_kern0, state.n_kern1, state.n_layer, state.filter_shape0, state.filter_shape1)
466 print 'end experiment' 506 print 'end experiment'
467 507
468 state.best_validation_loss = best_validation_loss 508 state.best_validation_loss = best_validation_loss
469 state.test_score = test_score 509 state.test_score = test_score
470 state.minutes_trained = minutes_trained 510 state.minutes_trained = minutes_trained