diff deep/stacked_dae/v_sylvain/sgd_optimization.py @ 305:fe5d428c2acc

Ajout de commentaires ainsi que correction de bug pour sauvegarder les parametres
author SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
date Wed, 31 Mar 2010 21:00:59 -0400
parents 1cc535f3e254
children a5abd5c8b4b0
line wrap: on
line diff
--- a/deep/stacked_dae/v_sylvain/sgd_optimization.py	Wed Mar 31 16:06:55 2010 -0400
+++ b/deep/stacked_dae/v_sylvain/sgd_optimization.py	Wed Mar 31 21:00:59 2010 -0400
@@ -88,7 +88,7 @@
         print "STARTING PRETRAINING, time = ", datetime.datetime.now()
         sys.stdout.flush()
         
-        un_fichier=int(819200.0/self.hp.minibatch_size) #Number of batches in a P07 batch
+        un_fichier=int(819200.0/self.hp.minibatch_size) #Number of batches in a P07 file
 
         start_time = time.clock()  
         ## Pre-train layer-wise 
@@ -198,6 +198,7 @@
 
         total_mb_index = 0
         minibatch_index = -1
+        parameters_finetune=[]
 
         while (epoch < num_finetune) and (not done_looping):
             epoch = epoch + 1
@@ -214,8 +215,8 @@
 
                 if (total_mb_index+1) % validation_frequency == 0: 
                     #minibatch_index += 1
-                    #The validation set is always NIST
-                    if ind_test == 0:
+                    #The validation set is always NIST (we want the model to be good on NIST)
+                    if ind_test == 0 | ind_test == 20:
                         iter=dataset_test.valid(minibatch_size)
                     else:
                         iter = dataset.valid(minibatch_size)
@@ -227,8 +228,8 @@
                     self.series["validation_error"].\
                         append((epoch, minibatch_index), this_validation_loss*100.)
 
-                    print('epoch %i, minibatch %i, validation error on %s : %f %%' % \
-                           (epoch, minibatch_index+1,nom_test, \
+                    print('epoch %i, minibatch %i, validation error on NIST : %f %%' % \
+                           (epoch, minibatch_index+1, \
                             this_validation_loss*100.))
 
 
@@ -240,9 +241,10 @@
                                improvement_threshold :
                             patience = max(patience, total_mb_index * patience_increase)
 
-                        # save best validation score and iteration number
+                        # save best validation score, iteration number and parameters
                         best_validation_loss = this_validation_loss
                         best_iter = total_mb_index
+                        parameters_finetune=[copy(x.value) for x in self.classifier.params]
 
                         # test it on the test set
                         iter = dataset.test(minibatch_size)
@@ -273,7 +275,7 @@
                     
                     if patience <= total_mb_index:
                         done_looping = True
-                        break
+                        break   #to exit the FOR loop
                     
                     sys.stdout.flush()
 
@@ -284,7 +286,7 @@
             self.series['params'].append((epoch,), self.classifier.all_params)
 
             if done_looping == True:    #To exit completly the fine-tuning
-                break
+                break   #to exit the WHILE loop
 
         end_time = time.clock()
         self.hp.update({'finetuning_time':end_time-start_time,\
@@ -299,34 +301,32 @@
         
         print ('The finetuning ran for %f minutes' % ((end_time-start_time)/60.))
         
+        sys.stdout.flush()
+        
         #Save a copy of the parameters in a file to be able to get them in the future
         
         if special == 1:    #To keep a track of the value of the parameters
-            parameters_finetune=[copy(x.value) for x in self.classifier.params]
             f = open('params_finetune_stanford.txt', 'w')
             pickle.dump(parameters_finetune,f)
             f.close()
         
         elif ind_test== 0:    #To keep a track of the value of the parameters
-            parameters_finetune=[copy(x.value) for x in self.classifier.params]
             f = open('params_finetune_P07.txt', 'w')
             pickle.dump(parameters_finetune,f)
             f.close()
 
         elif ind_test== 1:    #For the run with 2 finetunes. It will be faster.
-            parameters_finetune=[copy(x.value) for x in self.classifier.params]
             f = open('params_finetune_NIST.txt', 'w')
             pickle.dump(parameters_finetune,f)
             f.close()
         
         elif ind_test== 20:    #To keep a track of the value of the parameters
-            parameters_finetune=[copy(x.value) for x in self.classifier.params]
             f = open('params_finetune_NIST_then_P07.txt', 'w')
             pickle.dump(parameters_finetune,f)
             f.close()
         
 
-    #Set parameters like they where right after pre-train
+    #Set parameters like they where right after pre-train or finetune
     def reload_parameters(self,which):
         
         #self.parameters_pre=pickle.load('params_pretrain.txt')