Mercurial > ift6266
comparison deep/stacked_dae/v_sylvain/sgd_optimization.py @ 238:9fc641d7adda
Possibilite de restreindre la taille des ensemble d'entrainement, valid et test afin de pouvoir tester le code rapidement
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Mon, 15 Mar 2010 13:22:20 -0400 |
parents | ecb69e17950b |
children | 7dd43ef66d15 |
comparison
equal
deleted
inserted
replaced
237:9b6e0af062af | 238:9fc641d7adda |
---|---|
106 | 106 |
107 def train(self): | 107 def train(self): |
108 self.pretrain(self.dataset) | 108 self.pretrain(self.dataset) |
109 self.finetune(self.dataset) | 109 self.finetune(self.dataset) |
110 | 110 |
111 def pretrain(self,dataset): | 111 def pretrain(self,dataset,reduce): |
112 print "STARTING PRETRAINING, time = ", datetime.datetime.now() | 112 print "STARTING PRETRAINING, time = ", datetime.datetime.now() |
113 sys.stdout.flush() | 113 sys.stdout.flush() |
114 | 114 |
115 start_time = time.clock() | 115 start_time = time.clock() |
116 ## Pre-train layer-wise | 116 ## Pre-train layer-wise |
118 # go through pretraining epochs | 118 # go through pretraining epochs |
119 for epoch in xrange(self.hp.pretraining_epochs_per_layer): | 119 for epoch in xrange(self.hp.pretraining_epochs_per_layer): |
120 # go through the training set | 120 # go through the training set |
121 batch_index=int(0) | 121 batch_index=int(0) |
122 for x,y in dataset.train(self.hp.minibatch_size): | 122 for x,y in dataset.train(self.hp.minibatch_size): |
123 batch_index+=1 | |
124 if batch_index > reduce: #If maximum number of mini-batch is used | |
125 break | |
123 c = self.classifier.pretrain_functions[i](x) | 126 c = self.classifier.pretrain_functions[i](x) |
124 batch_index+=1 | 127 |
125 | 128 |
126 self.series["reconstruction_error"].append((epoch, batch_index), c) | 129 self.series["reconstruction_error"].append((epoch, batch_index), c) |
127 | 130 |
128 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c | 131 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c |
129 sys.stdout.flush() | 132 sys.stdout.flush() |
135 print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) | 138 print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) |
136 self.hp.update({'pretraining_time': end_time-start_time}) | 139 self.hp.update({'pretraining_time': end_time-start_time}) |
137 | 140 |
138 sys.stdout.flush() | 141 sys.stdout.flush() |
139 | 142 |
140 def finetune(self,dataset): | 143 def finetune(self,dataset,reduce): |
141 print "STARTING FINETUNING, time = ", datetime.datetime.now() | 144 print "STARTING FINETUNING, time = ", datetime.datetime.now() |
142 | 145 |
143 #index = T.lscalar() # index to a [mini]batch | 146 #index = T.lscalar() # index to a [mini]batch |
144 minibatch_size = self.hp.minibatch_size | 147 minibatch_size = self.hp.minibatch_size |
145 ensemble_x = T.matrix('ensemble_x') | 148 ensemble_x = T.matrix('ensemble_x') |
183 done_looping = False | 186 done_looping = False |
184 epoch = 0 | 187 epoch = 0 |
185 | 188 |
186 while (epoch < self.hp.max_finetuning_epochs) and (not done_looping): | 189 while (epoch < self.hp.max_finetuning_epochs) and (not done_looping): |
187 epoch = epoch + 1 | 190 epoch = epoch + 1 |
188 minibatch_index=int(-1) | 191 minibatch_index=int(0) |
189 for x,y in dataset.train(minibatch_size): | 192 for x,y in dataset.train(minibatch_size): |
193 minibatch_index +=1 | |
190 | 194 |
191 minibatch_index+=1 | 195 if minibatch_index > reduce: #If maximum number of mini-batchs is used |
196 break | |
197 | |
192 cost_ij = self.classifier.finetune(x,y) | 198 cost_ij = self.classifier.finetune(x,y) |
193 iter = epoch * self.n_train_batches + minibatch_index | 199 iter = epoch * self.n_train_batches + minibatch_index |
194 | 200 |
195 self.series["training_error"].append((epoch, minibatch_index), cost_ij) | 201 self.series["training_error"].append((epoch, minibatch_index), cost_ij) |
196 | 202 |
197 if (iter+1) % validation_frequency == 0: | 203 if (iter+1) % validation_frequency == 0: |
198 | 204 |
199 validation_losses = [validate_model(x,y) for x,y in dataset.valid(minibatch_size)] | 205 #validation_losses = [validate_model(x,y) for x,y in dataset.valid(minibatch_size)] |
206 test_index=int(0) | |
207 validation_losses=[] | |
208 for x,y in dataset.valid(minibatch_size): | |
209 test_index+=1 | |
210 if test_index > reduce: | |
211 break | |
212 validation_losses.append(validate_model(x,y)) | |
200 this_validation_loss = numpy.mean(validation_losses) | 213 this_validation_loss = numpy.mean(validation_losses) |
201 | 214 |
202 self.series["validation_error"].\ | 215 self.series["validation_error"].\ |
203 append((epoch, minibatch_index), this_validation_loss*100.) | 216 append((epoch, minibatch_index), this_validation_loss*100.) |
204 | 217 |
205 print('epoch %i, minibatch %i/%i, validation error %f %%' % \ | 218 print('epoch %i, minibatch %i, validation error %f %%' % \ |
206 (epoch, minibatch_index+1, self.n_train_batches, \ | 219 (epoch, minibatch_index, \ |
207 this_validation_loss*100.)) | 220 this_validation_loss*100.)) |
208 | 221 |
209 | 222 |
210 # if we got the best validation score until now | 223 # if we got the best validation score until now |
211 if this_validation_loss < best_validation_loss: | 224 if this_validation_loss < best_validation_loss: |
218 # save best validation score and iteration number | 231 # save best validation score and iteration number |
219 best_validation_loss = this_validation_loss | 232 best_validation_loss = this_validation_loss |
220 best_iter = iter | 233 best_iter = iter |
221 | 234 |
222 # test it on the test set | 235 # test it on the test set |
223 test_losses = [test_model(x,y) for x,y in dataset.test(minibatch_size)] | 236 #test_losses = [test_model(x,y) for x,y in dataset.test(minibatch_size)] |
237 test_losses=[] | |
238 i=0 | |
239 for x,y in dataset.test(minibatch_size): | |
240 i+=1 | |
241 if i > reduce: | |
242 break | |
243 test_losses.append(test_model(x,y)) | |
224 test_score = numpy.mean(test_losses) | 244 test_score = numpy.mean(test_losses) |
225 | 245 |
226 self.series["test_error"].\ | 246 self.series["test_error"].\ |
227 append((epoch, minibatch_index), test_score*100.) | 247 append((epoch, minibatch_index), test_score*100.) |
228 | 248 |
229 print((' epoch %i, minibatch %i/%i, test error of best ' | 249 print((' epoch %i, minibatch %i, test error of best ' |
230 'model %f %%') % | 250 'model %f %%') % |
231 (epoch, minibatch_index+1, self.n_train_batches, | 251 (epoch, minibatch_index, |
232 test_score*100.)) | 252 test_score*100.)) |
233 | 253 |
234 sys.stdout.flush() | 254 sys.stdout.flush() |
235 | 255 |
236 self.series['params'].append((epoch,), self.classifier.all_params) | 256 self.series['params'].append((epoch,), self.classifier.all_params) |