Mercurial > ift6266
comparison deep/stacked_dae/v_sylvain/sgd_optimization.py @ 384:8117c0e70db9
Ajout de la faculte de prendre PNIST07
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Tue, 27 Apr 2010 08:20:37 -0400 |
parents | f24b10e43a6f |
children | 88cb95007670 |
comparison
equal
deleted
inserted
replaced
383:5c3935aa3f8a | 384:8117c0e70db9 |
---|---|
154 f = open('params_pretrain.txt', 'w') | 154 f = open('params_pretrain.txt', 'w') |
155 cPickle.dump(self.parameters_pre,f,protocol=-1) | 155 cPickle.dump(self.parameters_pre,f,protocol=-1) |
156 f.close() | 156 f.close() |
157 | 157 |
158 | 158 |
159 def finetune(self,dataset,dataset_test,num_finetune,ind_test,special=0,decrease=0): | 159 def finetune(self,dataset,dataset_test,num_finetune,ind_test,special=0,decrease=0,dataset_test2=None): |
160 | 160 |
161 if special != 0 and special != 1: | 161 if special != 0 and special != 1: |
162 sys.exit('Bad value for variable special. Must be in {0,1}') | 162 sys.exit('Bad value for variable special. Must be in {0,1}') |
163 print "STARTING FINETUNING, time = ", datetime.datetime.now() | 163 print "STARTING FINETUNING, time = ", datetime.datetime.now() |
164 | 164 |
165 minibatch_size = self.hp.minibatch_size | 165 minibatch_size = self.hp.minibatch_size |
166 if ind_test == 0 or ind_test == 20: | 166 if ind_test == 0 or ind_test == 20: |
167 nom_test = "NIST" | 167 nom_test = "NIST" |
168 nom_train="P07" | 168 nom_train="P07" |
169 elif ind_test == 2: | |
170 nom_train = "PNIST07" | |
171 nom_test = "NIST" | |
172 nom_test2 = "P07" | |
169 else: | 173 else: |
170 nom_test = "P07" | 174 nom_test = "P07" |
171 nom_train = "NIST" | 175 nom_train = "NIST" |
172 | 176 |
173 | 177 |
236 self.series["training_error"].append((epoch, minibatch_index), cost_ij) | 240 self.series["training_error"].append((epoch, minibatch_index), cost_ij) |
237 | 241 |
238 if (total_mb_index+1) % validation_frequency == 0: | 242 if (total_mb_index+1) % validation_frequency == 0: |
239 #minibatch_index += 1 | 243 #minibatch_index += 1 |
240 #The validation set is always NIST (we want the model to be good on NIST) | 244 #The validation set is always NIST (we want the model to be good on NIST) |
241 if ind_test == 0 | ind_test == 20: | 245 if ind_test == 0 | ind_test == 20 | ind_test == 2: |
242 iter=dataset_test.valid(minibatch_size,bufsize=buffersize) | 246 iter=dataset_test.valid(minibatch_size,bufsize=buffersize) |
243 else: | 247 else: |
244 iter = dataset.valid(minibatch_size,bufsize=buffersize) | 248 iter = dataset.valid(minibatch_size,bufsize=buffersize) |
245 if self.max_minibatches: | 249 if self.max_minibatches: |
246 iter = itermax(iter, self.max_minibatches) | 250 iter = itermax(iter, self.max_minibatches) |
247 validation_losses = [validate_model(x,y) for x,y in iter] | 251 validation_losses = [validate_model(x,y) for x,y in iter] |
279 iter2 = dataset_test.test(minibatch_size,bufsize=buffersize) | 283 iter2 = dataset_test.test(minibatch_size,bufsize=buffersize) |
280 if self.max_minibatches: | 284 if self.max_minibatches: |
281 iter2 = itermax(iter2, self.max_minibatches) | 285 iter2 = itermax(iter2, self.max_minibatches) |
282 test_losses2 = [test_model(x,y) for x,y in iter2] | 286 test_losses2 = [test_model(x,y) for x,y in iter2] |
283 test_score2 = numpy.mean(test_losses2) | 287 test_score2 = numpy.mean(test_losses2) |
288 | |
289 #test it on the third test set if there is one | |
290 iter3 = dataset_test2.test(minibatch_size, bufsize=buffersize) | |
291 if self.max_minibatches: | |
292 iter3 = itermax(iter3, self.max_minibatches) | |
293 test_losses3 = [test_model(x,y) for x,y in iter3] | |
294 test_score3 = numpy.mean(test_losses3) | |
284 | 295 |
285 self.series["test_error"].\ | 296 self.series["test_error"].\ |
286 append((epoch, minibatch_index), test_score*100.) | 297 append((epoch, minibatch_index), test_score*100.) |
287 | 298 |
288 print((' epoch %i, minibatch %i, test error on dataset %s (train data) of best ' | 299 print((' epoch %i, minibatch %i, test error on dataset %s (train data) of best ' |
292 | 303 |
293 print((' epoch %i, minibatch %i, test error on dataset %s of best ' | 304 print((' epoch %i, minibatch %i, test error on dataset %s of best ' |
294 'model %f %%') % | 305 'model %f %%') % |
295 (epoch, minibatch_index+1,nom_test, | 306 (epoch, minibatch_index+1,nom_test, |
296 test_score2*100.)) | 307 test_score2*100.)) |
308 print((' epoch %i, minibatch %i, test error on dataset %s of best ' | |
309 'model %f %%') % | |
310 (epoch, minibatch_index+1,nom_test2, | |
311 test_score3*100.)) | |
297 | 312 |
298 if patience <= total_mb_index: | 313 if patience <= total_mb_index: |
299 done_looping = True | 314 done_looping = True |
300 break #to exit the FOR loop | 315 break #to exit the FOR loop |
301 | 316 |
304 # useful when doing tests | 319 # useful when doing tests |
305 if self.max_minibatches and minibatch_index >= self.max_minibatches: | 320 if self.max_minibatches and minibatch_index >= self.max_minibatches: |
306 break | 321 break |
307 | 322 |
308 if decrease == 1: | 323 if decrease == 1: |
309 if (ind_test == 21 & epoch % 100 == 0) | ind_test == 20: | 324 if (ind_test == 21 & epoch % 100 == 0) | ind_test == 20 | ind_test == 2: |
310 learning_rate /= 2 #divide the learning rate by 2 for each new epoch of P07 (or 100 of NIST) | 325 learning_rate /= 2 #divide the learning rate by 2 for each new epoch of P07 (or 100 of NIST) |
311 | 326 |
312 self.series['params'].append((epoch,), self.classifier.all_params) | 327 self.series['params'].append((epoch,), self.classifier.all_params) |
313 | 328 |
314 if done_looping == True: #To exit completly the fine-tuning | 329 if done_looping == True: #To exit completly the fine-tuning |
322 | 337 |
323 print(('\nOptimization complete with best validation score of %f %%,' | 338 print(('\nOptimization complete with best validation score of %f %%,' |
324 'with test performance %f %% on dataset %s ') % | 339 'with test performance %f %% on dataset %s ') % |
325 (best_validation_loss * 100., test_score*100.,nom_train)) | 340 (best_validation_loss * 100., test_score*100.,nom_train)) |
326 print(('The test score on the %s dataset is %f')%(nom_test,test_score2*100.)) | 341 print(('The test score on the %s dataset is %f')%(nom_test,test_score2*100.)) |
342 print(('The test score on the %s dataset is %f')%(nom_test2,test_score3*100.)) | |
327 | 343 |
328 print ('The finetuning ran for %f minutes' % ((end_time-start_time)/60.)) | 344 print ('The finetuning ran for %f minutes' % ((end_time-start_time)/60.)) |
329 | 345 |
330 sys.stdout.flush() | 346 sys.stdout.flush() |
331 | 347 |
347 cPickle.dump(parameters_finetune,f,protocol=-1) | 363 cPickle.dump(parameters_finetune,f,protocol=-1) |
348 f.close() | 364 f.close() |
349 | 365 |
350 elif ind_test== 21: #To keep a track of the value of the parameters | 366 elif ind_test== 21: #To keep a track of the value of the parameters |
351 f = open('params_finetune_P07_then_NIST.txt', 'w') | 367 f = open('params_finetune_P07_then_NIST.txt', 'w') |
368 cPickle.dump(parameters_finetune,f,protocol=-1) | |
369 f.close() | |
370 elif ind_test == 2: | |
371 f = open('params_finetune_PNIST07.txt', 'w') | |
352 cPickle.dump(parameters_finetune,f,protocol=-1) | 372 cPickle.dump(parameters_finetune,f,protocol=-1) |
353 f.close() | 373 f.close() |
354 | 374 |
355 | 375 |
356 #Set parameters like they where right after pre-train or finetune | 376 #Set parameters like they where right after pre-train or finetune |
374 [self.classifier.x,self.classifier.y], self.classifier.errors) | 394 [self.classifier.x,self.classifier.y], self.classifier.errors) |
375 | 395 |
376 iter2 = dataset.train(self.hp.minibatch_size,bufsize=buffersize) | 396 iter2 = dataset.train(self.hp.minibatch_size,bufsize=buffersize) |
377 train_losses2 = [test_model(x,y) for x,y in iter2] | 397 train_losses2 = [test_model(x,y) for x,y in iter2] |
378 train_score2 = numpy.mean(train_losses2) | 398 train_score2 = numpy.mean(train_losses2) |
379 print "Training error is: " + str(train_score2) | 399 print(('The training error is %f')%(train_score2*100.)) |
380 | 400 |
381 #To see the prediction of the model, the real answer and the image to judge | 401 #To see the prediction of the model, the real answer and the image to judge |
382 def see_error(self, dataset): | 402 def see_error(self, dataset): |
383 import pylab | 403 import pylab |
384 #The function to know the prediction | 404 #The function to know the prediction |