comparison deep/stacked_dae/v_sylvain/stacked_dae.py @ 280:c77ffb11f91d

rajout de methode reliant toutes les couches cachees a la logistic et changeant seulement les parametres de la logistic durant finetune
author SylvainPL <sylvain.pannetier.lebeuf@umontreal.ca>
date Wed, 24 Mar 2010 14:44:24 -0400
parents 02b141a466b4
children 60cacb9a70e4
comparison
equal deleted inserted replaced
279:206374eed2fb 280:c77ffb11f91d
34 # symbolic form 34 # symbolic form
35 self.y_pred=T.argmax(self.p_y_given_x, axis=1) 35 self.y_pred=T.argmax(self.p_y_given_x, axis=1)
36 36
37 # list of parameters for this layer 37 # list of parameters for this layer
38 self.params = [self.W, self.b] 38 self.params = [self.W, self.b]
39
39 40
40 def negative_log_likelihood(self, y): 41 def negative_log_likelihood(self, y):
41 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) 42 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
42 43
43 def errors(self, y): 44 def errors(self, y):
179 self.params = [] 180 self.params = []
180 # MODIF: added this so we also get the b_primes 181 # MODIF: added this so we also get the b_primes
181 # (not used for finetuning... still using ".params") 182 # (not used for finetuning... still using ".params")
182 self.all_params = [] 183 self.all_params = []
183 self.n_layers = len(hidden_layers_sizes) 184 self.n_layers = len(hidden_layers_sizes)
185 self.logistic_params = []
184 186
185 print "Creating SdA with params:" 187 print "Creating SdA with params:"
186 print "batch_size", batch_size 188 print "batch_size", batch_size
187 print "hidden_layers_sizes", hidden_layers_sizes 189 print "hidden_layers_sizes", hidden_layers_sizes
188 print "corruption_levels", corruption_levels 190 print "corruption_levels", corruption_levels
255 # self.x : train_set_x[index*batch_size:(index+1)*batch_size] / self.shared_divider}) 257 # self.x : train_set_x[index*batch_size:(index+1)*batch_size] / self.shared_divider})
256 # collect this function into a list 258 # collect this function into a list
257 self.pretrain_functions += [update_fn] 259 self.pretrain_functions += [update_fn]
258 260
259 261
260 # We now need to add a logistic layer on top of the MLP 262 # We now need to add a logistic layer on top of the SDA
261 self.logLayer = LogisticRegression(\ 263 self.logLayer = LogisticRegression(\
262 input = self.layers[-1].output,\ 264 input = self.layers[-1].output,\
263 n_in = hidden_layers_sizes[-1], n_out = n_outs) 265 n_in = hidden_layers_sizes[-1], n_out = n_outs)
264 266
265 self.params += self.logLayer.params 267 self.params += self.logLayer.params
275 for param,gparam in zip(self.params, gparams): 277 for param,gparam in zip(self.params, gparams):
276 updates[param] = param - gparam*finetune_lr 278 updates[param] = param - gparam*finetune_lr
277 279
278 self.finetune = theano.function([self.x,self.y], cost, 280 self.finetune = theano.function([self.x,self.y], cost,
279 updates = updates)#, 281 updates = updates)#,
280 # givens = {
281 # self.x : train_set_x[index*batch_size:(index+1)*batch_size]/self.shared_divider,
282 # self.y : train_set_y[index*batch_size:(index+1)*batch_size]} )
283 282
284 # symbolic variable that points to the number of errors made on the 283 # symbolic variable that points to the number of errors made on the
285 # minibatch given by self.x and self.y 284 # minibatch given by self.x and self.y
286 285
287 self.errors = self.logLayer.errors(self.y) 286 self.errors = self.logLayer.errors(self.y)
287
288
289 #STRUCTURE FOR THE FINETUNING OF THE LOGISTIC REGRESSION ON THE TOP WITH
290 #ALL HIDDEN LAYERS AS INPUT
291
292 all_h=[]
293 for i in xrange(self.n_layers):
294 all_h.append(self.layers[i].output)
295 self.all_hidden=T.concatenate(all_h,axis=1)
296
297
298 self.logLayer2 = LogisticRegression(\
299 input = self.all_hidden,\
300 n_in = sum(hidden_layers_sizes), n_out = n_outs)
301 #n_in=hidden_layers_sizes[0],n_out=n_outs)
302
303 #self.logistic_params+= self.logLayer2.params
304 # construct a function that implements one step of finetunining
305
306 # compute the cost, defined as the negative log likelihood
307 cost2 = self.logLayer2.negative_log_likelihood(self.y)
308 # compute the gradients with respect to the model parameters
309 gparams2 = T.grad(cost2, self.logLayer2.params)
310
311 # compute list of updates
312 updates2 = {}
313 for param,gparam in zip(self.logLayer2.params, gparams2):
314 updates2[param] = param - gparam*finetune_lr
315
316 self.finetune2 = theano.function([self.x,self.y], cost2,
317 updates = updates2)
318
319 # symbolic variable that points to the number of errors made on the
320 # minibatch given by self.x and self.y
321
322 self.errors2 = self.logLayer2.errors(self.y)
288 323
289 324
290 if __name__ == '__main__': 325 if __name__ == '__main__':
291 import sys 326 import sys
292 args = sys.argv[1:] 327 args = sys.argv[1:]