comparison scripts/stacked_dae/sgd_optimization.py @ 139:7d8366fb90bf

Ajouté des __init__.py dans l'arborescence pour que les scripts puissent être utilisés avec des paths pour jobman, et fait pas mal de modifs dans stacked_dae pour pouvoir réutiliser le travail fait pour des tests où le pretraining est le même.
author fsavard
date Mon, 22 Feb 2010 13:38:25 -0500
parents 5c79a2557f2f
children
comparison
equal deleted inserted replaced
138:128507ac4edf 139:7d8366fb90bf
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # Generic SdA optimization loop, adapted slightly from the deeplearning.net tutorial 4 # Generic SdA optimization loop, adapted from the deeplearning.net tutorial
5 5
6 import numpy 6 import numpy
7 import theano 7 import theano
8 import time 8 import time
9 import theano.tensor as T 9 import theano.tensor as T
10 import copy
11 import sys
10 12
11 from jobman import DD 13 from jobman import DD
14 import jobman, jobman.sql
12 15
13 from stacked_dae import SdA 16 from stacked_dae import SdA
14 17
15 def sgd_optimization(dataset, hyperparameters, n_ins, n_outs): 18 def shared_dataset(data_xy):
16 hp = hyperparameters 19 data_x, data_y = data_xy
17 20 #shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX))
18 printout_frequency = 1000 21 #shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX))
19 22 #shared_y = T.cast(shared_y, 'int32')
20 train_set, valid_set, test_set = dataset 23 shared_x = theano.shared(data_x)
21 24 shared_y = theano.shared(data_y)
22 def shared_dataset(data_xy): 25 return shared_x, shared_y
23 data_x, data_y = data_xy 26
24 shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX)) 27 class SdaSgdOptimizer:
25 shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX)) 28 def __init__(self, dataset, hyperparameters, n_ins, n_outs, input_divider=1.0,\
26 return shared_x, T.cast(shared_y, 'int32') 29 job_tree=False, results_db=None,\
27 30 experiment="",\
28 test_set_x, test_set_y = shared_dataset(test_set) 31 num_hidden_layers_to_try=[1,2,3], \
29 valid_set_x, valid_set_y = shared_dataset(valid_set) 32 finetuning_lr_to_try=[0.1, 0.01, 0.001, 0.0001, 0.00001]):
30 train_set_x, train_set_y = shared_dataset(train_set) 33
31 34 self.dataset = dataset
32 # compute number of minibatches for training, validation and testing 35 self.hp = copy.copy(hyperparameters)
33 n_train_batches = train_set_x.value.shape[0] / hp.minibatch_size 36 self.n_ins = n_ins
34 n_valid_batches = valid_set_x.value.shape[0] / hp.minibatch_size 37 self.n_outs = n_outs
35 n_test_batches = test_set_x.value.shape[0] / hp.minibatch_size 38 self.input_divider = numpy.asarray(input_divider, dtype=theano.config.floatX)
36 39
37 # allocate symbolic variables for the data 40 self.job_tree = job_tree
38 index = T.lscalar() # index to a [mini]batch 41 self.results_db = results_db
39 42 self.experiment = experiment
40 # construct the stacked denoising autoencoder class 43 if self.job_tree:
41 classifier = SdA( train_set_x=train_set_x, train_set_y = train_set_y,\ 44 assert(not results_db is None)
42 batch_size = hp.minibatch_size, n_ins= n_ins, \ 45 # these hp should not be there, so we insert default values
43 hidden_layers_sizes = hp.hidden_layers_sizes, n_outs=10, \ 46 # we use 3 hidden layers as we'll iterate through 1,2,3
44 corruption_levels = hp.corruption_levels,\ 47 self.hp.finetuning_lr = 0.1 # dummy value, will be replaced anyway
45 rng = numpy.random.RandomState(1234),\ 48 cl = self.hp.corruption_levels
46 pretrain_lr = hp.pretraining_lr, finetune_lr = hp.finetuning_lr ) 49 nh = self.hp.hidden_layers_sizes
47 50 self.hp.corruption_levels = [cl,cl,cl]
48 printout_acc = 0.0 51 self.hp.hidden_layers_sizes = [nh,nh,nh]
49 52
50 start_time = time.clock() 53 self.num_hidden_layers_to_try = num_hidden_layers_to_try
51 ## Pre-train layer-wise 54 self.finetuning_lr_to_try = finetuning_lr_to_try
52 for i in xrange(classifier.n_layers): 55
53 # go through pretraining epochs 56 self.printout_frequency = 1000
54 for epoch in xrange(hp.pretraining_epochs_per_layer): 57
55 # go through the training set 58 self.rng = numpy.random.RandomState(1234)
56 for batch_index in xrange(n_train_batches): 59
57 c = classifier.pretrain_functions[i](batch_index) 60 self.init_datasets()
58 61 self.init_classifier()
59 print c 62
60 63 def init_datasets(self):
61 printout_acc += c / printout_frequency 64 print "init_datasets"
62 if (batch_index+1) % printout_frequency == 0: 65 train_set, valid_set, test_set = self.dataset
63 print batch_index, "reconstruction cost avg=", printout_acc 66 self.test_set_x, self.test_set_y = shared_dataset(test_set)
67 self.valid_set_x, self.valid_set_y = shared_dataset(valid_set)
68 self.train_set_x, self.train_set_y = shared_dataset(train_set)
69
70 # compute number of minibatches for training, validation and testing
71 self.n_train_batches = self.train_set_x.value.shape[0] / self.hp.minibatch_size
72 self.n_valid_batches = self.valid_set_x.value.shape[0] / self.hp.minibatch_size
73 self.n_test_batches = self.test_set_x.value.shape[0] / self.hp.minibatch_size
74
75 def init_classifier(self):
76 print "Constructing classifier"
77 # construct the stacked denoising autoencoder class
78 self.classifier = SdA( \
79 train_set_x= self.train_set_x, \
80 train_set_y = self.train_set_y,\
81 batch_size = self.hp.minibatch_size, \
82 n_ins= self.n_ins, \
83 hidden_layers_sizes = self.hp.hidden_layers_sizes, \
84 n_outs = self.n_outs, \
85 corruption_levels = self.hp.corruption_levels,\
86 rng = self.rng,\
87 pretrain_lr = self.hp.pretraining_lr, \
88 finetune_lr = self.hp.finetuning_lr,\
89 input_divider = self.input_divider )
90
91 def train(self):
92 self.pretrain()
93 if not self.job_tree:
94 # if job_tree is True, finetuning was already performed
95 self.finetune()
96
97 def pretrain(self):
98 print "STARTING PRETRAINING"
99
100 printout_acc = 0.0
101 last_error = 0.0
102
103 start_time = time.clock()
104 ## Pre-train layer-wise
105 for i in xrange(self.classifier.n_layers):
106 # go through pretraining epochs
107 for epoch in xrange(self.hp.pretraining_epochs_per_layer):
108 # go through the training set
109 for batch_index in xrange(self.n_train_batches):
110 c = self.classifier.pretrain_functions[i](batch_index)
111
112 printout_acc += c / self.printout_frequency
113 if (batch_index+1) % self.printout_frequency == 0:
114 print batch_index, "reconstruction cost avg=", printout_acc
115 last_error = printout_acc
116 printout_acc = 0.0
117
118 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c
119
120 self.job_splitter(i+1, time.clock()-start_time, last_error)
121
122 end_time = time.clock()
123
124 print ('Pretraining took %f minutes' %((end_time-start_time)/60.))
125
126 # Save time by reusing intermediate results
127 def job_splitter(self, current_pretraining_layer, pretraining_time, last_error):
128
129 state_copy = None
130 original_classifier = None
131
132 if self.job_tree and current_pretraining_layer in self.num_hidden_layers_to_try:
133 for lr in self.finetuning_lr_to_try:
134 sys.stdout.flush()
135 sys.stderr.flush()
136
137 state_copy = copy.copy(self.hp)
138
139 self.hp.update({'num_hidden_layers':current_pretraining_layer, \
140 'finetuning_lr':lr,\
141 'pretraining_time':pretraining_time,\
142 'last_reconstruction_error':last_error})
143
144 original_classifier = self.classifier
145 print "ORIGINAL CLASSIFIER MEANS",original_classifier.get_params_means()
146 self.classifier = SdA.copy_reusing_lower_layers(original_classifier, current_pretraining_layer, new_finetuning_lr=lr)
147
148 self.finetune()
149
150 self.insert_finished_job()
151
152 print "NEW CLASSIFIER MEANS AFTERWARDS",self.classifier.get_params_means()
153 print "ORIGINAL CLASSIFIER MEANS AFTERWARDS",original_classifier.get_params_means()
154 self.classifier = original_classifier
155 self.hp = state_copy
156
157 def insert_finished_job(self):
158 job = copy.copy(self.hp)
159 job[jobman.sql.STATUS] = jobman.sql.DONE
160 job[jobman.sql.EXPERIMENT] = self.experiment
161
162 # don,t try to store arrays in db
163 job['hidden_layers_sizes'] = job.hidden_layers_sizes[0]
164 job['corruption_levels'] = job.corruption_levels[0]
165
166 print "Will insert finished job", job
167 jobman.sql.insert_dict(jobman.flatten(job), self.results_db)
168
169 def finetune(self):
170 print "STARTING FINETUNING"
171
172 index = T.lscalar() # index to a [mini]batch
173 minibatch_size = self.hp.minibatch_size
174
175 # create a function to compute the mistakes that are made by the model
176 # on the validation set, or testing set
177 test_model = theano.function([index], self.classifier.errors,
178 givens = {
179 self.classifier.x: self.test_set_x[index*minibatch_size:(index+1)*minibatch_size] / self.input_divider,
180 self.classifier.y: self.test_set_y[index*minibatch_size:(index+1)*minibatch_size]})
181
182 validate_model = theano.function([index], self.classifier.errors,
183 givens = {
184 self.classifier.x: self.valid_set_x[index*minibatch_size:(index+1)*minibatch_size] / self.input_divider,
185 self.classifier.y: self.valid_set_y[index*minibatch_size:(index+1)*minibatch_size]})
186
187
188 # early-stopping parameters
189 patience = 10000 # look as this many examples regardless
190 patience_increase = 2. # wait this much longer when a new best is
191 # found
192 improvement_threshold = 0.995 # a relative improvement of this much is
193 # considered significant
194 validation_frequency = min(self.n_train_batches, patience/2)
195 # go through this many
196 # minibatche before checking the network
197 # on the validation set; in this case we
198 # check every epoch
199
200 best_params = None
201 best_validation_loss = float('inf')
202 test_score = 0.
203 start_time = time.clock()
204
205 done_looping = False
206 epoch = 0
207
208 printout_acc = 0.0
209
210 if not self.hp.has_key('max_finetuning_epochs'):
211 self.hp.max_finetuning_epochs = 1000
212
213 while (epoch < self.hp.max_finetuning_epochs) and (not done_looping):
214 epoch = epoch + 1
215 for minibatch_index in xrange(self.n_train_batches):
216
217 cost_ij = self.classifier.finetune(minibatch_index)
218 iter = epoch * self.n_train_batches + minibatch_index
219
220 printout_acc += cost_ij / float(self.printout_frequency * minibatch_size)
221 if (iter+1) % self.printout_frequency == 0:
222 print iter, "cost avg=", printout_acc
64 printout_acc = 0.0 223 printout_acc = 0.0
224
225 if (iter+1) % validation_frequency == 0:
65 226
66 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c 227 validation_losses = [validate_model(i) for i in xrange(self.n_valid_batches)]
67 228 this_validation_loss = numpy.mean(validation_losses)
68 end_time = time.clock() 229 print('epoch %i, minibatch %i/%i, validation error %f %%' % \
69 230 (epoch, minibatch_index+1, self.n_train_batches, \
70 print ('Pretraining took %f minutes' %((end_time-start_time)/60.)) 231 this_validation_loss*100.))
71 # Fine-tune the entire model 232
72 233
73 minibatch_size = hp.minibatch_size 234 # if we got the best validation score until now
74 235 if this_validation_loss < best_validation_loss:
75 # create a function to compute the mistakes that are made by the model 236
76 # on the validation set, or testing set 237 #improve patience if loss improvement is good enough
77 test_model = theano.function([index], classifier.errors, 238 if this_validation_loss < best_validation_loss * \
78 givens = { 239 improvement_threshold :
79 classifier.x: test_set_x[index*minibatch_size:(index+1)*minibatch_size], 240 patience = max(patience, iter * patience_increase)
80 classifier.y: test_set_y[index*minibatch_size:(index+1)*minibatch_size]}) 241
81 242 # save best validation score and iteration number
82 validate_model = theano.function([index], classifier.errors, 243 best_validation_loss = this_validation_loss
83 givens = { 244 best_iter = iter
84 classifier.x: valid_set_x[index*minibatch_size:(index+1)*minibatch_size], 245
85 classifier.y: valid_set_y[index*minibatch_size:(index+1)*minibatch_size]}) 246 # test it on the test set
86 247 test_losses = [test_model(i) for i in xrange(self.n_test_batches)]
87 248 test_score = numpy.mean(test_losses)
88 # early-stopping parameters 249 print((' epoch %i, minibatch %i/%i, test error of best '
89 patience = 10000 # look as this many examples regardless 250 'model %f %%') %
90 patience_increase = 2. # wait this much longer when a new best is 251 (epoch, minibatch_index+1, self.n_train_batches,
91 # found 252 test_score*100.))
92 improvement_threshold = 0.995 # a relative improvement of this much is 253
93 # considered significant 254
94 validation_frequency = min(n_train_batches, patience/2) 255 if patience <= iter :
95 # go through this many
96 # minibatche before checking the network
97 # on the validation set; in this case we
98 # check every epoch
99
100 best_params = None
101 best_validation_loss = float('inf')
102 test_score = 0.
103 start_time = time.clock()
104
105 done_looping = False
106 epoch = 0
107
108 printout_acc = 0.0
109
110 print "----- START FINETUNING -----"
111
112 while (epoch < hp.max_finetuning_epochs) and (not done_looping):
113 epoch = epoch + 1
114 for minibatch_index in xrange(n_train_batches):
115
116 cost_ij = classifier.finetune(minibatch_index)
117 iter = epoch * n_train_batches + minibatch_index
118
119 printout_acc += cost_ij / float(printout_frequency * minibatch_size)
120 if (iter+1) % printout_frequency == 0:
121 print iter, "cost avg=", printout_acc
122 printout_acc = 0.0
123
124 if (iter+1) % validation_frequency == 0:
125
126 validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
127 this_validation_loss = numpy.mean(validation_losses)
128 print('epoch %i, minibatch %i/%i, validation error %f %%' % \
129 (epoch, minibatch_index+1, n_train_batches, \
130 this_validation_loss*100.))
131
132
133 # if we got the best validation score until now
134 if this_validation_loss < best_validation_loss:
135
136 #improve patience if loss improvement is good enough
137 if this_validation_loss < best_validation_loss * \
138 improvement_threshold :
139 patience = max(patience, iter * patience_increase)
140
141 # save best validation score and iteration number
142 best_validation_loss = this_validation_loss
143 best_iter = iter
144
145 # test it on the test set
146 test_losses = [test_model(i) for i in xrange(n_test_batches)]
147 test_score = numpy.mean(test_losses)
148 print((' epoch %i, minibatch %i/%i, test error of best '
149 'model %f %%') %
150 (epoch, minibatch_index+1, n_train_batches,
151 test_score*100.))
152
153
154 if patience <= iter :
155 done_looping = True 256 done_looping = True
156 break 257 break
157 258
158 end_time = time.clock() 259 end_time = time.clock()
159 print(('Optimization complete with best validation score of %f %%,' 260 self.hp.update({'finetuning_time':end_time-start_time,\
160 'with test performance %f %%') % 261 'best_validation_error':best_validation_loss,\
161 262 'test_score':test_score,
162 (best_validation_loss * 100., test_score*100.)) 263 'num_finetuning_epochs':epoch})
163 print ('The code ran for %f minutes' % ((end_time-start_time)/60.)) 264 print(('Optimization complete with best validation score of %f %%,'
164 265 'with test performance %f %%') %
165 266 (best_validation_loss * 100., test_score*100.))
267 print ('The finetuning ran for %f minutes' % ((end_time-start_time)/60.))
268
269
270