comparison baseline/mlp/mlp_nist.py @ 212:e390b0454515

added classic lr time decay and py code to calculate the error based on a saved model
author xaviermuller
date Wed, 10 Mar 2010 16:17:59 -0500
parents d37c944133c3
children 9b6e0af062af
comparison
equal deleted inserted replaced
211:476da2ba6a12 212:e390b0454515
29 import theano 29 import theano
30 import theano.tensor as T 30 import theano.tensor as T
31 import time 31 import time
32 import theano.tensor.nnet 32 import theano.tensor.nnet
33 import pylearn 33 import pylearn
34 import theano,pylearn.version
34 from pylearn.io import filetensor as ft 35 from pylearn.io import filetensor as ft
35 36
36 data_path = '/data/lisa/data/nist/by_class/' 37 data_path = '/data/lisa/data/nist/by_class/'
37 38
38 class MLP(object): 39 class MLP(object):
172 L1_reg = 0.00,\ 173 L1_reg = 0.00,\
173 L2_reg = 0.0001,\ 174 L2_reg = 0.0001,\
174 nb_max_exemples=1000000,\ 175 nb_max_exemples=1000000,\
175 batch_size=20,\ 176 batch_size=20,\
176 nb_hidden = 500,\ 177 nb_hidden = 500,\
177 nb_targets = 62): 178 nb_targets = 62,
179 tau=1e6):
178 180
179 181
180 configuration = [learning_rate,nb_max_exemples,nb_hidden,adaptive_lr] 182 configuration = [learning_rate,nb_max_exemples,nb_hidden,adaptive_lr]
183
184 #save initial learning rate if classical adaptive lr is used
185 initial_lr=learning_rate
181 186
182 total_validation_error_list = [] 187 total_validation_error_list = []
183 total_train_error_list = [] 188 total_train_error_list = []
184 learning_rate_list=[] 189 learning_rate_list=[]
185 best_training_error=float('inf'); 190 best_training_error=float('inf');
191
186 192
187 193
188 194
189 f = open(data_path+train_data) 195 f = open(data_path+train_data)
190 g= open(data_path+train_labels) 196 g= open(data_path+train_labels)
313 test_score = 0. 319 test_score = 0.
314 start_time = time.clock() 320 start_time = time.clock()
315 n_iter = nb_max_exemples/batch_size # nb of max times we are allowed to run through all exemples 321 n_iter = nb_max_exemples/batch_size # nb of max times we are allowed to run through all exemples
316 n_iter = n_iter/n_minibatches + 1 #round up 322 n_iter = n_iter/n_minibatches + 1 #round up
317 n_iter=max(1,n_iter) # run at least once on short debug call 323 n_iter=max(1,n_iter) # run at least once on short debug call
324 time_n=0 #in unit of exemples
325
318 326
319 327
320 if verbose == True: 328 if verbose == True:
321 print 'looping at most %d times through the data set' %n_iter 329 print 'looping at most %d times through the data set' %n_iter
322 for iter in xrange(n_iter* n_minibatches): 330 for iter in xrange(n_iter* n_minibatches):
323 331
324 # get epoch and minibatch index 332 # get epoch and minibatch index
325 epoch = iter / n_minibatches 333 epoch = iter / n_minibatches
326 minibatch_index = iter % n_minibatches 334 minibatch_index = iter % n_minibatches
327 335
336
337 if adaptive_lr==2:
338 classifier.lr.value = tau*initial_lr/(tau+time_n)
328 339
329 340
330 # get the minibatches corresponding to `iter` modulo 341 # get the minibatches corresponding to `iter` modulo
331 # `len(train_batches)` 342 # `len(train_batches)`
332 x,y = train_batches[ minibatch_index ] 343 x,y = train_batches[ minibatch_index ]
362 373
363 if verbose == True: 374 if verbose == True:
364 print('epoch %i, minibatch %i/%i, validation error %f, training error %f %%' % \ 375 print('epoch %i, minibatch %i/%i, validation error %f, training error %f %%' % \
365 (epoch, minibatch_index+1, n_minibatches, \ 376 (epoch, minibatch_index+1, n_minibatches, \
366 this_validation_loss*100.,this_train_loss*100)) 377 this_validation_loss*100.,this_train_loss*100))
378 print 'learning rate = %f' %classifier.lr.value
379 print 'time = %i' %time_n
367 380
368 381
369 #save the learning rate 382 #save the learning rate
370 learning_rate_list.append(classifier.lr.value) 383 learning_rate_list.append(classifier.lr.value)
371 384
423 if iter>patience: 436 if iter>patience:
424 print 'we have diverged' 437 print 'we have diverged'
425 break 438 break
426 439
427 440
441 time_n= time_n + batch_size
428 end_time = time.clock() 442 end_time = time.clock()
429 if verbose == True: 443 if verbose == True:
430 print(('Optimization complete. Best validation score of %f %% ' 444 print(('Optimization complete. Best validation score of %f %% '
431 'obtained at iteration %i, with test performance %f %%') % 445 'obtained at iteration %i, with test performance %f %%') %
432 (best_validation_loss * 100., best_iter, test_score*100.)) 446 (best_validation_loss * 100., best_iter, test_score*100.))
446 460
447 def jobman_mlp_full_nist(state,channel): 461 def jobman_mlp_full_nist(state,channel):
448 (train_error,validation_error,test_error,nb_exemples,time)=mlp_full_nist(learning_rate=state.learning_rate,\ 462 (train_error,validation_error,test_error,nb_exemples,time)=mlp_full_nist(learning_rate=state.learning_rate,\
449 nb_max_exemples=state.nb_max_exemples,\ 463 nb_max_exemples=state.nb_max_exemples,\
450 nb_hidden=state.nb_hidden,\ 464 nb_hidden=state.nb_hidden,\
451 adaptive_lr=state.adaptive_lr) 465 adaptive_lr=state.adaptive_lr,\
466 tau=tau)
452 state.train_error=train_error 467 state.train_error=train_error
453 state.validation_error=validation_error 468 state.validation_error=validation_error
454 state.test_error=test_error 469 state.test_error=test_error
455 state.nb_exemples=nb_exemples 470 state.nb_exemples=nb_exemples
456 state.time=time 471 state.time=time