@@ -185,50 +185,66 @@ def sgd_optimization_mnist( learning_rate=0.01, n_iter=100):
185185 train_model = pfunc ([x , y ], cost , updates = updates )
186186
187187 # early-stopping parameters
188- patience = 2000 # look as this many examples regardless
189- patience_increase = 2 # wait this much longer when a new best is
190- # found
191- improvement_threshold = 0.99 # a relative improvement of this much is
192- # considered significant
193- validation_frequency = 1000 # make this many SGD updates between
194- # validations
188+ patience = 5000 # look as this many examples regardless
189+ patience_increase = 2 # wait this much longer when a new best is
190+ # found
191+ improvement_threshold = 0.995 # a relative improvement of this much is
192+ # considered significant
193+ validation_frequency = 1000 # make this many SGD updates between
194+ # validations
195195
196196 best_params = None
197197 best_validation_loss = float ('inf' )
198-
199-
200- for i in xrange (n_iter ):
201- # go through the training set and update the model parameters
202- for x ,y in train_batches :
203- cost_ij = train_model (x , y )
204-
205-
206- # test the model on the validation set ( measuring the average number
207- # of errors )
208- valid_score = 0.
209- for x ,y in valid_batches :
210- # sum up the errors for each minibatch
211- valid_score += test_model (x ,y )
212- # get the average by dividing with the number of minibatches
213- valid_score /= len (valid_batches )
214-
215- print ('epoch %i, validation error %f' % (i , valid_score ))
216-
217-
218- # if we got the best validation score until now
219- if valid_score < best_valid_score :
220- best_valid_score = valid_score
221- # test it on the test set
222-
223- test_score = 0.
224- for x ,y in test_batches :
225- test_score += test_model (x ,y )
226- test_score /= len (test_batches )
227- print ('epoch %i, test error of best model %f' % (i , test_score ))
198+ test_score = 0.
199+
200+ # have a maximum of `n_iter` iterations through the entire dataset
201+ for iter in xrange (n_iter * len (train_batches )):
202+
203+ # get epoch and minibatch index
204+ epoch = iter / len (train_batches )
205+ minibatch_index = iter % len (train_batches )
206+
207+ # get the minibatches corresponding to `iter` modulo
208+ # `len(train_batches)`
209+ x ,y = train_batches [ minibatch_index ]
210+ cost_ij = train_model (x ,y )
211+
212+ if (iter + 1 ) % validation_frequency == 0 :
213+ # compute zero-one loss on validation set
214+ this_validation_loss = 0.
215+ for x ,y in valid_batches :
216+ # sum up the errors for each minibatch
217+ this_validation_loss += test_model (x ,y )
218+ # get the average by dividing with the number of minibatches
219+ this_validation_loss /= len (valid_batches )
220+
221+ print ('epoch %i, validation error %f' %
222+ (epoch , this_validation_loss ))
223+
224+ #improve patience
225+ if this_validation_loss < best_validation_loss * \
226+ improvement_threshold :
227+ patience = max (patience , iter * patience_increase )
228+
229+
230+ # if we got the best validation score until now
231+ if this_validation_loss < best_validation_loss :
232+ best_validation_loss = this_validation_loss
233+ # test it on the test set
234+
235+ test_score = 0.
236+ for x ,y in test_batches :
237+ test_score += test_model (x ,y )
238+ test_score /= len (test_batches )
239+ print (' epoch %i, test error of best model %f' %
240+ (epoch , test_score ))
241+
242+ if patience <= iter :
243+ break
228244
229245
230246 print (('Optimization complete with best validation score of %f,'
231- 'with test performance %f' ) % (best_valid_score , test_score ))
247+ 'with test performance %f' ) % (best_validation_loss , test_score ))
232248
233249
234250
0 commit comments