Mercurial > ift6266
comparison deep/stacked_dae/v_sylvain/sgd_optimization.py @ 233:02ed13244133
version pour utilisation du module dataset
author | SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca> |
---|---|
date | Sun, 14 Mar 2010 15:07:17 -0400 |
parents | 8a94a5c808cd |
children | ecb69e17950b |
comparison
equal
deleted
inserted
replaced
232:4ce1fc11f4b2 | 233:02ed13244133 |
---|---|
103 #theano.printing.pydotprint(self.classifier.pretrain_functions[0], "function.graph") | 103 #theano.printing.pydotprint(self.classifier.pretrain_functions[0], "function.graph") |
104 | 104 |
105 sys.stdout.flush() | 105 sys.stdout.flush() |
106 | 106 |
107 def train(self): | 107 def train(self): |
108 self.pretrain() | 108 self.pretrain(self.dataset) |
109 self.finetune() | 109 self.finetune(self.dataset) |
110 | 110 |
111 def pretrain(self): | 111 def pretrain(self,dataset): |
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 |
117 for i in xrange(self.classifier.n_layers): | 117 for i in xrange(self.classifier.n_layers): |
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 for batch_index in xrange(self.n_train_batches): | 121 for x,y in dataset.train(self.hp.minibatch_size): |
122 c = self.classifier.pretrain_functions[i](batch_index) | 122 c = self.classifier.pretrain_functions[i](x) |
123 | 123 |
124 self.series["reconstruction_error"].append((epoch, batch_index), c) | 124 self.series["reconstruction_error"].append((epoch, batch_index), c) |
125 | 125 |
126 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c | 126 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c |
127 sys.stdout.flush() | 127 sys.stdout.flush() |
133 print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) | 133 print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) |
134 self.hp.update({'pretraining_time': end_time-start_time}) | 134 self.hp.update({'pretraining_time': end_time-start_time}) |
135 | 135 |
136 sys.stdout.flush() | 136 sys.stdout.flush() |
137 | 137 |
138 def finetune(self): | 138 def finetune(self,dataset): |
139 print "STARTING FINETUNING, time = ", datetime.datetime.now() | 139 print "STARTING FINETUNING, time = ", datetime.datetime.now() |
140 | 140 |
141 index = T.lscalar() # index to a [mini]batch | 141 #index = T.lscalar() # index to a [mini]batch |
142 minibatch_size = self.hp.minibatch_size | 142 minibatch_size = self.hp.minibatch_size |
143 | 143 |
144 # create a function to compute the mistakes that are made by the model | 144 # create a function to compute the mistakes that are made by the model |
145 # on the validation set, or testing set | 145 # on the validation set, or testing set |
146 shared_divider = theano.shared(numpy.asarray(self.input_divider, dtype=theano.config.floatX)) | 146 shared_divider = theano.shared(numpy.asarray(self.input_divider, dtype=theano.config.floatX)) |
147 test_model = theano.function([index], self.classifier.errors, | 147 test_model = theano.function([ensemble_x,ensemble_y], self.classifier.errors, |
148 givens = { | 148 givens = { |
149 self.classifier.x: self.test_set_x[index*minibatch_size:(index+1)*minibatch_size] / shared_divider, | 149 #self.classifier.x: self.test_set_x[index*minibatch_size:(index+1)*minibatch_size] / shared_divider, |
150 self.classifier.y: self.test_set_y[index*minibatch_size:(index+1)*minibatch_size]}) | 150 #self.classifier.y: self.test_set_y[index*minibatch_size:(index+1)*minibatch_size]}) |
151 | 151 self.classifier.x: ensemble_x, |
152 validate_model = theano.function([index], self.classifier.errors, | 152 self.classifier.y: ensemble_y}) |
153 | |
154 validate_model = theano.function([ensemble_x,ensemble_y], self.classifier.errors, | |
153 givens = { | 155 givens = { |
154 self.classifier.x: self.valid_set_x[index*minibatch_size:(index+1)*minibatch_size] / shared_divider, | 156 #self.classifier.x: self.valid_set_x[index*minibatch_size:(index+1)*minibatch_size] / shared_divider, |
155 self.classifier.y: self.valid_set_y[index*minibatch_size:(index+1)*minibatch_size]}) | 157 #self.classifier.y: self.valid_set_y[index*minibatch_size:(index+1)*minibatch_size]}) |
158 self.classifier.x: ensemble_x, | |
159 self.classifier.y: ensemble_y}) | |
156 | 160 |
157 | 161 |
158 # early-stopping parameters | 162 # early-stopping parameters |
159 patience = 10000 # look as this many examples regardless | 163 patience = 10000 # look as this many examples regardless |
160 patience_increase = 2. # wait this much longer when a new best is | 164 patience_increase = 2. # wait this much longer when a new best is |
175 done_looping = False | 179 done_looping = False |
176 epoch = 0 | 180 epoch = 0 |
177 | 181 |
178 while (epoch < self.hp.max_finetuning_epochs) and (not done_looping): | 182 while (epoch < self.hp.max_finetuning_epochs) and (not done_looping): |
179 epoch = epoch + 1 | 183 epoch = epoch + 1 |
180 for minibatch_index in xrange(self.n_train_batches): | 184 minibatch_index=int(-1) |
181 | 185 for x,y in dataset.train(minibatch_size): |
182 cost_ij = self.classifier.finetune(minibatch_index) | 186 |
187 minibatch_index+=1 | |
188 cost_ij = self.classifier.finetune(x,y) | |
183 iter = epoch * self.n_train_batches + minibatch_index | 189 iter = epoch * self.n_train_batches + minibatch_index |
184 | 190 |
185 self.series["training_error"].append((epoch, minibatch_index), cost_ij) | 191 self.series["training_error"].append((epoch, minibatch_index), cost_ij) |
186 | 192 |
187 if (iter+1) % validation_frequency == 0: | 193 if (iter+1) % validation_frequency == 0: |
188 | 194 |
189 validation_losses = [validate_model(i) for i in xrange(self.n_valid_batches)] | 195 validation_losses = [validate_model(x,y) for x,y in dataset.valid(minibatch_size)] |
190 this_validation_loss = numpy.mean(validation_losses) | 196 this_validation_loss = numpy.mean(validation_losses) |
191 | 197 |
192 self.series["validation_error"].\ | 198 self.series["validation_error"].\ |
193 append((epoch, minibatch_index), this_validation_loss*100.) | 199 append((epoch, minibatch_index), this_validation_loss*100.) |
194 | 200 |
208 # save best validation score and iteration number | 214 # save best validation score and iteration number |
209 best_validation_loss = this_validation_loss | 215 best_validation_loss = this_validation_loss |
210 best_iter = iter | 216 best_iter = iter |
211 | 217 |
212 # test it on the test set | 218 # test it on the test set |
213 test_losses = [test_model(i) for i in xrange(self.n_test_batches)] | 219 test_losses = [test_model(x,y) for x,y in dataset.test(minibatch_size)] |
214 test_score = numpy.mean(test_losses) | 220 test_score = numpy.mean(test_losses) |
215 | 221 |
216 self.series["test_error"].\ | 222 self.series["test_error"].\ |
217 append((epoch, minibatch_index), test_score*100.) | 223 append((epoch, minibatch_index), test_score*100.) |
218 | 224 |