Mercurial > ift6266
comparison scripts/stacked_dae.py @ 114:0b4080394f2c
Added stacked DAE code for my experiments, based on tutorial code. Quite unfinished.
author | fsavard |
---|---|
date | Wed, 17 Feb 2010 09:29:19 -0500 |
parents | |
children | 4f37755d301b |
comparison
equal
deleted
inserted
replaced
113:291d749452df | 114:0b4080394f2c |
---|---|
1 #!/usr/bin/python | |
2 # coding: utf-8 | |
3 | |
4 # Code for stacked denoising autoencoder | |
5 # Tests with MNIST | |
6 # TODO: adapt for NIST | |
7 # Based almost entirely on deeplearning.net tutorial, modifications by | |
8 # François Savard | |
9 | |
10 # Base LogisticRegression, SigmoidalLayer, dA, SdA code taken | |
11 # from the deeplearning.net tutorial. Refactored a bit. | |
12 # Changes (mainly): | |
13 # - splitted initialization in smaller methods | |
14 # - removed the "givens" thing involving an index in the whole dataset | |
15 # (to allow flexibility in how data is inputted... not necessarily one big tensor) | |
16 # - changed the "driver" a lot, altough for the moment the same logic is used | |
17 | |
18 import time | |
19 import theano | |
20 import theano.tensor as T | |
21 import theano.tensor.nnet | |
22 from theano.tensor.shared_randomstreams import RandomStreams | |
23 import numpy, numpy.random | |
24 | |
25 from pylearn.datasets import MNIST | |
26 | |
27 | |
28 # from pylearn codebase | |
29 def update_locals(obj, dct): | |
30 if 'self' in dct: | |
31 del dct['self'] | |
32 obj.__dict__.update(dct) | |
33 | |
34 | |
35 class LogisticRegression(object): | |
36 def __init__(self, input, n_in, n_out): | |
37 # initialize with 0 the weights W as a matrix of shape (n_in, n_out) | |
38 self.W = theano.shared(value=numpy.zeros((n_in,n_out), dtype = theano.config.floatX), | |
39 name='W') | |
40 # initialize the baises b as a vector of n_out 0s | |
41 self.b = theano.shared(value=numpy.zeros((n_out,), dtype = theano.config.floatX), | |
42 name='b') | |
43 | |
44 # compute vector of class-membership probabilities in symbolic form | |
45 self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b) | |
46 | |
47 # compute prediction as class whose probability is maximal in | |
48 # symbolic form | |
49 self.y_pred=T.argmax(self.p_y_given_x, axis=1) | |
50 | |
51 self.params = [self.W, self.b] | |
52 | |
53 def negative_log_likelihood(self, y): | |
54 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) | |
55 | |
56 def errors(self, y): | |
57 # check if y has same dimension of y_pred | |
58 if y.ndim != self.y_pred.ndim: | |
59 raise TypeError('y should have the same shape as self.y_pred', | |
60 ('y', target.type, 'y_pred', self.y_pred.type)) | |
61 # check if y is of the correct datatype | |
62 if y.dtype.startswith('int'): | |
63 # the T.neq operator returns a vector of 0s and 1s, where 1 | |
64 # represents a mistake in prediction | |
65 return T.mean(T.neq(self.y_pred, y)) | |
66 else: | |
67 raise NotImplementedError() | |
68 | |
69 | |
70 class SigmoidalLayer(object): | |
71 def __init__(self, rng, input, n_in, n_out): | |
72 self.input = input | |
73 | |
74 W_values = numpy.asarray( rng.uniform( \ | |
75 low = -numpy.sqrt(6./(n_in+n_out)), \ | |
76 high = numpy.sqrt(6./(n_in+n_out)), \ | |
77 size = (n_in, n_out)), dtype = theano.config.floatX) | |
78 self.W = theano.shared(value = W_values) | |
79 | |
80 b_values = numpy.zeros((n_out,), dtype= theano.config.floatX) | |
81 self.b = theano.shared(value= b_values) | |
82 | |
83 self.output = T.nnet.sigmoid(T.dot(input, self.W) + self.b) | |
84 self.params = [self.W, self.b] | |
85 | |
86 | |
87 class dA(object): | |
88 def __init__(self, n_visible= 784, n_hidden= 500, \ | |
89 corruption_level = 0.1, input = None, \ | |
90 shared_W = None, shared_b = None): | |
91 update_locals(self, locals()) | |
92 | |
93 self.init_randomizer() | |
94 self.init_params() | |
95 self.init_functions() | |
96 | |
97 def init_randomizer(self): | |
98 # create a Theano random generator that gives symbolic random values | |
99 self.theano_rng = RandomStreams() | |
100 # create a numpy random generator | |
101 self.numpy_rng = numpy.random.RandomState() | |
102 | |
103 def init_params(self): | |
104 if self.shared_W != None and self.shared_b != None : | |
105 self.W = self.shared_W | |
106 self.b = self.shared_b | |
107 else: | |
108 # initial values for weights and biases | |
109 # note : W' was written as `W_prime` and b' as `b_prime` | |
110 | |
111 # W is initialized with `initial_W` which is uniformely sampled | |
112 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible) | |
113 # the output of uniform if converted using asarray to dtype | |
114 # theano.config.floatX so that the code is runable on GPU | |
115 initial_W = numpy.asarray( self.numpy_rng.uniform( \ | |
116 low = -numpy.sqrt(6./(n_hidden+n_visible)), \ | |
117 high = numpy.sqrt(6./(n_hidden+n_visible)), \ | |
118 size = (n_visible, n_hidden)), dtype = theano.config.floatX) | |
119 initial_b = numpy.zeros(n_hidden) | |
120 | |
121 # theano shared variables for weights and biases | |
122 self.W = theano.shared(value = initial_W, name = "W") | |
123 self.b = theano.shared(value = initial_b, name = "b") | |
124 | |
125 initial_b_prime= numpy.zeros(self.n_visible) | |
126 # tied weights, therefore W_prime is W transpose | |
127 self.W_prime = self.W.T | |
128 self.b_prime = theano.shared(value = initial_b_prime, name = "b'") | |
129 | |
130 def init_functions(self): | |
131 # if no input is given, generate a variable representing the input | |
132 if self.input == None : | |
133 # we use a matrix because we expect a minibatch of several examples, | |
134 # each example being a row | |
135 self.x = T.dmatrix(name = 'input') | |
136 else: | |
137 self.x = self.input | |
138 | |
139 # keep 90% of the inputs the same and zero-out randomly selected subset of | |
140 # 10% of the inputs | |
141 # note : first argument of theano.rng.binomial is the shape(size) of | |
142 # random numbers that it should produce | |
143 # second argument is the number of trials | |
144 # third argument is the probability of success of any trial | |
145 # | |
146 # this will produce an array of 0s and 1s where 1 has a | |
147 # probability of 1 - ``corruption_level`` and 0 with | |
148 # ``corruption_level`` | |
149 self.tilde_x = self.theano_rng.binomial(self.x.shape, 1, 1-self.corruption_level) * self.x | |
150 # using tied weights | |
151 self.y = T.nnet.sigmoid(T.dot(self.tilde_x, self.W) + self.b) | |
152 self.z = T.nnet.sigmoid(T.dot(self.y, self.W_prime) + self.b_prime) | |
153 self.L = - T.sum( self.x*T.log(self.z) + (1-self.x)*T.log(1-self.z), axis=1 ) | |
154 # note : L is now a vector, where each element is the cross-entropy cost | |
155 # of the reconstruction of the corresponding example of the | |
156 # minibatch. We need to compute the average of all these to get | |
157 # the cost of the minibatch | |
158 self.cost = T.mean(self.L) | |
159 | |
160 self.params = [ self.W, self.b, self.b_prime ] | |
161 | |
162 class SdA(): | |
163 def __init__(self, batch_size, n_ins, | |
164 hidden_layers_sizes, n_outs, | |
165 corruption_levels, rng, pretrain_lr, finetune_lr): | |
166 update_locals(self, locals()) | |
167 | |
168 self.layers = [] | |
169 self.pretrain_functions = [] | |
170 self.params = [] | |
171 self.n_layers = len(hidden_layers_sizes) | |
172 | |
173 if len(hidden_layers_sizes) < 1 : | |
174 raiseException (' You must have at least one hidden layer ') | |
175 | |
176 # allocate symbolic variables for the data | |
177 self.x = T.matrix('x') # the data is presented as rasterized images | |
178 self.y = T.ivector('y') # the labels are presented as 1D vector of | |
179 # [int] labels | |
180 | |
181 self.create_layers() | |
182 self.init_finetuning() | |
183 | |
184 def create_layers(self): | |
185 for i in xrange( self.n_layers ): | |
186 # construct the sigmoidal layer | |
187 | |
188 # the size of the input is either the number of hidden units of | |
189 # the layer below or the input size if we are on the first layer | |
190 if i == 0 : | |
191 input_size = self.n_ins | |
192 else: | |
193 input_size = self.hidden_layers_sizes[i-1] | |
194 | |
195 # the input to this layer is either the activation of the hidden | |
196 # layer below or the input of the SdA if you are on the first | |
197 # layer | |
198 if i == 0 : | |
199 layer_input = self.x | |
200 else: | |
201 layer_input = self.layers[-1].output | |
202 | |
203 layer = SigmoidalLayer(self.rng, layer_input, input_size, | |
204 self.hidden_layers_sizes[i] ) | |
205 # add the layer to the | |
206 self.layers += [layer] | |
207 self.params += layer.params | |
208 | |
209 # Construct a denoising autoencoder that shared weights with this | |
210 # layer | |
211 dA_layer = dA(input_size, self.hidden_layers_sizes[i], \ | |
212 corruption_level = self.corruption_levels[0],\ | |
213 input = layer_input, \ | |
214 shared_W = layer.W, shared_b = layer.b) | |
215 | |
216 self.init_updates_for_layer(dA_layer) | |
217 | |
218 def init_updates_for_layer(self, dA_layer): | |
219 # Construct a function that trains this dA | |
220 # compute gradients of layer parameters | |
221 gparams = T.grad(dA_layer.cost, dA_layer.params) | |
222 # compute the list of updates | |
223 updates = {} | |
224 for param, gparam in zip(dA_layer.params, gparams): | |
225 updates[param] = param - gparam * self.pretrain_lr | |
226 | |
227 # create a function that trains the dA | |
228 update_fn = theano.function([self.x], dA_layer.cost, \ | |
229 updates = updates) | |
230 | |
231 # collect this function into a list | |
232 self.pretrain_functions += [update_fn] | |
233 | |
234 def init_finetuning(self): | |
235 # We now need to add a logistic layer on top of the MLP | |
236 self.logLayer = LogisticRegression(\ | |
237 input = self.layers[-1].output,\ | |
238 n_in = self.hidden_layers_sizes[-1], n_out = self.n_outs) | |
239 | |
240 self.params += self.logLayer.params | |
241 # construct a function that implements one step of finetunining | |
242 | |
243 # compute the cost, defined as the negative log likelihood | |
244 cost = self.logLayer.negative_log_likelihood(self.y) | |
245 # compute the gradients with respect to the model parameters | |
246 gparams = T.grad(cost, self.params) | |
247 # compute list of updates | |
248 updates = {} | |
249 for param,gparam in zip(self.params, gparams): | |
250 updates[param] = param - gparam*self.finetune_lr | |
251 | |
252 self.finetune = theano.function([self.x, self.y], cost, | |
253 updates = updates) | |
254 | |
255 # symbolic variable that points to the number of errors made on the | |
256 # minibatch given by self.x and self.y | |
257 | |
258 self.errors = self.logLayer.errors(self.y) | |
259 | |
260 class MnistIterators: | |
261 def __init__(self, minibatch_size): | |
262 self.minibatch_size = minibatch_size | |
263 | |
264 self.mnist = MNIST.first_1k() | |
265 | |
266 self.len_train = len(self.mnist.train.x) | |
267 self.len_valid = len(self.mnist.valid.x) | |
268 self.len_test = len(self.mnist.test.x) | |
269 | |
270 def train_x_batches(self): | |
271 idx = 0 | |
272 while idx < len(self.mnist.train.x): | |
273 yield self.mnist.train.x[idx:idx+self.minibatch_size] | |
274 idx += self.minibatch_size | |
275 | |
276 def train_xy_batches(self): | |
277 idx = 0 | |
278 while idx < len(self.mnist.train.x): | |
279 mb_x = self.mnist.train.x[idx:idx+self.minibatch_size] | |
280 mb_y = self.mnist.train.y[idx:idx+self.minibatch_size] | |
281 yield mb_x, mb_y | |
282 idx += self.minibatch_size | |
283 | |
284 def valid_xy_batches(self): | |
285 idx = 0 | |
286 while idx < len(self.mnist.valid.x): | |
287 mb_x = self.mnist.valid.x[idx:idx+self.minibatch_size] | |
288 mb_y = self.mnist.valid.y[idx:idx+self.minibatch_size] | |
289 yield mb_x, mb_y | |
290 idx += self.minibatch_size | |
291 | |
292 | |
293 class MnistTrainingDriver: | |
294 def __init__(self, rng=numpy.random): | |
295 self.rng = rng | |
296 | |
297 self.init_SdA() | |
298 | |
299 def init_SdA(self): | |
300 # Hyperparam | |
301 hidden_layers_sizes = [1000, 1000, 1000] | |
302 n_outs = 10 | |
303 corruption_levels = [0.2, 0.2, 0.2] | |
304 minibatch_size = 10 | |
305 pretrain_lr = 0.001 | |
306 finetune_lr = 0.001 | |
307 | |
308 update_locals(self, locals()) | |
309 | |
310 self.mnist = MnistIterators(minibatch_size) | |
311 | |
312 # construct the stacked denoising autoencoder class | |
313 self.classifier = SdA( batch_size = minibatch_size, \ | |
314 n_ins=28*28, \ | |
315 hidden_layers_sizes = hidden_layers_sizes, \ | |
316 n_outs=n_outs, \ | |
317 corruption_levels = corruption_levels,\ | |
318 rng = self.rng,\ | |
319 pretrain_lr = pretrain_lr, \ | |
320 finetune_lr = finetune_lr) | |
321 | |
322 def compute_validation_error(self): | |
323 validation_error = 0.0 | |
324 | |
325 count = 0 | |
326 for mb_x, mb_y in self.mnist.valid_xy_batches(): | |
327 validation_error += self.classifier.errors(mb_x, mb_y) | |
328 count += 1 | |
329 | |
330 return float(validation_error) / count | |
331 | |
332 def pretrain(self): | |
333 pretraining_epochs = 20 | |
334 | |
335 for layer_idx, update_fn in enumerate(self.classifier.pretrain_functions): | |
336 for epoch in xrange(pretraining_epochs): | |
337 # go through the training set | |
338 cost_acc = 0.0 | |
339 for i, mb_x in enumerate(self.mnist.train_x_batches()): | |
340 cost_acc += update_fn(mb_x) | |
341 | |
342 if i % 100 == 0: | |
343 print i, "avg err = ", cost_acc / 100.0 | |
344 cost_acc = 0.0 | |
345 print 'Pre-training layer %d, epoch %d' % (layer_idx, epoch) | |
346 | |
347 def finetune(self): | |
348 max_training_epochs = 1000 | |
349 | |
350 n_train_batches = self.mnist.len_train / self.minibatch_size | |
351 | |
352 # early-stopping parameters | |
353 patience = 10000 # look as this many examples regardless | |
354 patience_increase = 2. # wait this much longer when a new best is | |
355 # found | |
356 improvement_threshold = 0.995 # a relative improvement of this much is | |
357 # considered significant | |
358 validation_frequency = min(n_train_batches, patience/2) | |
359 # go through this many | |
360 # minibatche before checking the network | |
361 # on the validation set; in this case we | |
362 # check every epoch | |
363 | |
364 | |
365 # TODO: use this | |
366 best_params = None | |
367 best_validation_loss = float('inf') | |
368 test_score = 0. | |
369 start_time = time.clock() | |
370 | |
371 done_looping = False | |
372 epoch = 0 | |
373 | |
374 while (epoch < max_training_epochs) and (not done_looping): | |
375 epoch = epoch + 1 | |
376 for minibatch_index, (mb_x, mb_y) in enumerate(self.mnist.train_xy_batches()): | |
377 cost_ij = classifier.finetune(mb_x, mb_y) | |
378 iter = epoch * n_train_batches + minibatch_index | |
379 | |
380 if (iter+1) % validation_frequency == 0: | |
381 this_validation_loss = self.compute_validation_error() | |
382 print('epoch %i, minibatch %i/%i, validation error %f %%' % \ | |
383 (epoch, minibatch_index+1, n_train_batches, \ | |
384 this_validation_loss*100.)) | |
385 | |
386 # if we got the best validation score until now | |
387 if this_validation_loss < best_validation_loss: | |
388 | |
389 #improve patience if loss improvement is good enough | |
390 if this_validation_loss < best_validation_loss * \ | |
391 improvement_threshold : | |
392 patience = max(patience, iter * patience_increase) | |
393 print "Improving patience" | |
394 | |
395 # save best validation score and iteration number | |
396 best_validation_loss = this_validation_loss | |
397 best_iter = iter | |
398 | |
399 # test it on the test set | |
400 #test_losses = [test_model(i) for i in xrange(n_test_batches)] | |
401 #test_score = numpy.mean(test_losses) | |
402 #print((' epoch %i, minibatch %i/%i, test error of best ' | |
403 # 'model %f %%') % | |
404 # (epoch, minibatch_index+1, n_train_batches, | |
405 # test_score*100.)) | |
406 | |
407 | |
408 if patience <= iter : | |
409 done_looping = True | |
410 break | |
411 | |
412 def train(): | |
413 driver = MnistTrainingDriver() | |
414 start_time = time.clock() | |
415 driver.pretrain() | |
416 print "PRETRAINING DONE. STARTING FINETUNING." | |
417 driver.finetune() | |
418 end_time = time.clock() | |
419 | |
420 if __name__ == '__main__': | |
421 train() | |
422 |