changeset 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 206374eed2fb
children a8b92a4a708d
files deep/stacked_dae/v_sylvain/stacked_dae.py
diffstat 1 files changed, 39 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/deep/stacked_dae/v_sylvain/stacked_dae.py	Wed Mar 24 14:36:55 2010 -0400
+++ b/deep/stacked_dae/v_sylvain/stacked_dae.py	Wed Mar 24 14:44:24 2010 -0400
@@ -36,6 +36,7 @@
 
         # list of parameters for this layer
         self.params = [self.W, self.b]
+        
 
     def negative_log_likelihood(self, y):
        return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
@@ -181,6 +182,7 @@
         # (not used for finetuning... still using ".params")
         self.all_params         = []
         self.n_layers           = len(hidden_layers_sizes)
+        self.logistic_params    = []
 
         print "Creating SdA with params:"
         print "batch_size", batch_size
@@ -257,7 +259,7 @@
             self.pretrain_functions += [update_fn]
 
         
-        # We now need to add a logistic layer on top of the MLP
+        # We now need to add a logistic layer on top of the SDA
         self.logLayer = LogisticRegression(\
                          input = self.layers[-1].output,\
                          n_in = hidden_layers_sizes[-1], n_out = n_outs)
@@ -277,15 +279,48 @@
             
         self.finetune = theano.function([self.x,self.y], cost, 
                 updates = updates)#,
-        #        givens = {
-        #          self.x : train_set_x[index*batch_size:(index+1)*batch_size]/self.shared_divider,
-        #          self.y : train_set_y[index*batch_size:(index+1)*batch_size]} )
 
         # symbolic variable that points to the number of errors made on the
         # minibatch given by self.x and self.y
 
         self.errors = self.logLayer.errors(self.y)
         
+        
+        #STRUCTURE FOR THE FINETUNING OF THE LOGISTIC REGRESSION ON THE TOP WITH
+        #ALL HIDDEN LAYERS AS INPUT
+        
+        all_h=[]
+        for i in xrange(self.n_layers):
+            all_h.append(self.layers[i].output)
+        self.all_hidden=T.concatenate(all_h,axis=1)
+
+
+        self.logLayer2 = LogisticRegression(\
+                         input = self.all_hidden,\
+                         n_in = sum(hidden_layers_sizes), n_out = n_outs)
+                         #n_in=hidden_layers_sizes[0],n_out=n_outs)
+
+        #self.logistic_params+= self.logLayer2.params
+        # construct a function that implements one step of finetunining
+
+        # compute the cost, defined as the negative log likelihood 
+        cost2 = self.logLayer2.negative_log_likelihood(self.y)
+        # compute the gradients with respect to the model parameters
+        gparams2 = T.grad(cost2, self.logLayer2.params)
+
+        # compute list of updates
+        updates2 = {}
+        for param,gparam in zip(self.logLayer2.params, gparams2):
+            updates2[param] = param - gparam*finetune_lr
+   
+        self.finetune2 = theano.function([self.x,self.y], cost2, 
+                updates = updates2)
+
+        # symbolic variable that points to the number of errors made on the
+        # minibatch given by self.x and self.y
+
+        self.errors2 = self.logLayer2.errors(self.y)
+        
 
 if __name__ == '__main__':
     import sys