changeset 802:a18b87ca33e6

added a supupdate method for DAA inputs groups
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Mon, 27 Jul 2009 19:07:22 -0400
parents 0772b76c806d
children 86b007959ceb
files pylearn/algorithms/sandbox/DAA_inputs_groups.py
diffstat 1 files changed, 58 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/algorithms/sandbox/DAA_inputs_groups.py	Thu Jul 23 12:25:47 2009 -0400
+++ b/pylearn/algorithms/sandbox/DAA_inputs_groups.py	Mon Jul 27 19:07:22 2009 -0400
@@ -29,7 +29,8 @@
     return rval
 
 # Weight initialisation utils--------------------------------------
-# time consuming but just a test
+
+# time consuming but just a test (not conclusive)
 def orthogonalinit(W,axis=1):
     nb = W.shape[axis]
     bn = W.shape[0] if axis is 1 else W.shape[1]
@@ -610,13 +611,13 @@
             self.localgradients_aux[i] = T.grad(self.daaig[i].noise.reconstruction_cost_aux,self.daaig[i].params) \
                 if auxin_size[i] is not None else T.constant(0)
             self.localgradients[i] = T.grad(self.localcost[i], self.daaig[i].params)
-            self.globalgradients[i] = T.grad(self.globalcost[i], self.daaig[i].params+paramsenc)
+            self.globalgradients[i] = T.grad(self.globalcost[i], paramsenc + self.daaig[i].params)
             if self.totalupdatebool:
                 self.totalgradients[i] = T.grad(self.totalcost[i], paramstot)
             
             #create the updates dictionnaries
             local_grads = dict((j,j-self.unsup_lr*g) for j,g in zip(self.daaig[i].params,self.localgradients[i]))
-            global_grads = dict((j,j-self.unsup_lr*g) for j,g in zip(self.daaig[i].params+paramsenc,self.globalgradients[i]))
+            global_grads = dict((j,j-self.unsup_lr*g) for j,g in zip(paramsenc+self.daaig[i].params,self.globalgradients[i]))
             if self.totalupdatebool:
                 total_grads = dict((j, j - self.unsup_lr * g) for j,g in zip(paramstot,self.totalgradients[i]))
             
@@ -661,12 +662,12 @@
             self.totalcost[-1] = [self.totalcost[-2], self.globalcost[-1]]
         
         self.localgradients[-1] = T.grad(self.localcost[-1], self.daaig[-1].params)
-        self.globalgradients[-1] = T.grad(self.globalcost[-1], self.daaig[-1].params+paramsenc)
+        self.globalgradients[-1] = T.grad(self.globalcost[-1], paramsenc + self.daaig[-1].params)
         if self.totalupdatebool:
             self.totalgradients[-1] = [T.grad(self.totalcost[-2], paramstot) , T.grad(self.globalcost[-1],paramstot) ]
         
         local_grads = dict((j,j-self.sup_lr*g) for j,g in zip(self.daaig[-1].params,self.localgradients[-1]))
-        global_grads = dict((j,j-self.sup_lr*g) for j,g in zip(self.daaig[-1].params+paramsenc,self.globalgradients[-1]))
+        global_grads = dict((j,j-self.sup_lr*g) for j,g in zip(paramsenc + self.daaig[-1].params,self.globalgradients[-1]))
         if self.totalupdatebool:
             total_grads = dict((j, j - self.unsup_lr * g1 - self.sup_lr * g2)\
                     for j,g1,g2 in zip(paramstot,self.totalgradients[-1][0],self.totalgradients[-1][1]))
@@ -753,30 +754,32 @@
         save_mat('wenc%s.ft'%(i) ,inst.daaig[i].w, save_dir)
         save_mat('benc%s.ft'%(i) ,inst.daaig[i].b, save_dir)
     
-    def _instance_load(self,inst,save_dir='',coef = None, Sup_layer = None):
+    def _instance_load(self,inst,save_dir='',coefenc = None, coefdec = None, Sup_layer = None):
         
-        if coef is None:
-            coef = [1.]*self.depth
+        if coefenc is None:
+            coefenc = [1.]*self.depth
+        if coefdec is None:
+            coefdec = [1.]*self.depth
             
         for i in range(self.depth):
-            inst.daaig[i].benc = load_mat('benc%s.ft'%(i), save_dir)/coef[i]
+            inst.daaig[i].benc = load_mat('benc%s.ft'%(i), save_dir)/coefenc[i]
             
             if self.daaig[i].auxinput is not None:
                 for j in range(len(inst.daaig[i].wauxenc)):
-                    inst.daaig[i].wauxenc[j] = load_mat('wauxenc%s_%s.ft'%(i,j),save_dir)/coef[i]
-                    inst.daaig[i].bauxdec[j] = load_mat('bauxdec%s_%s.ft'%(i,j),save_dir)
+                    inst.daaig[i].wauxenc[j] = load_mat('wauxenc%s_%s.ft'%(i,j),save_dir)/coefenc[i]
+                    inst.daaig[i].bauxdec[j] = load_mat('bauxdec%s_%s.ft'%(i,j),save_dir)/coefdec[i]
             
             if self.daaig[i].input is not None:
-                inst.daaig[i].wenc = load_mat('wenc%s.ft'%(i),save_dir)/coef[i]
-                inst.daaig[i].bdec = load_mat('bdec%s.ft'%(i),save_dir)
+                inst.daaig[i].wenc = load_mat('wenc%s.ft'%(i),save_dir)/coefenc[i]
+                inst.daaig[i].bdec = load_mat('bdec%s.ft'%(i),save_dir)/coefdec[i]
             
             if not self.daaig[i].tie_weights:
                 if self.daaig[i].auxinput is not None:
                     for j in range(len(inst.daaig[i].wauxdec)):
-                        inst.daaig[i].wauxdec[j] = load_mat('wauxdec%s_%s.ft'%(i,j),save_dir)
+                        inst.daaig[i].wauxdec[j] = load_mat('wauxdec%s_%s.ft'%(i,j),save_dir)/coefdec[i]
                 
                 if self.daaig[i].input is not None:
-                    inst.daaig[i].wdec = load_mat('wdec%s.ft'%(i),save_dir)
+                    inst.daaig[i].wdec = load_mat('wdec%s.ft'%(i),save_dir)/coefdec[i]
         i=i+1
         if Sup_layer is None:
             inst.daaig[i].w = load_mat('wenc%s.ft'%(i),save_dir)
@@ -791,6 +794,36 @@
     def _instance_recsaturation(self,inst,layer,inputs):
         return numpy.mean(numpy.median(abs(inst.recactivation[layer](*inputs)),1))
     
+    def _instance_error(self,inst,inputs,target):
+        return numpy.sum(inst.classify(*inputs) != target) / float(len(target)) *100.0
+    
+    def _instance_nll(self,inst,inputs,target):
+        return numpy.sum(inst.NLL(*(inputs+[target]))) / float(len(target))
+    
+    def _instance_unsupgrad(self,inst,inputs,layer,param_name):
+        inst.noiseseed(0)
+        gradin = inst.compute_localgradients_in[layer](*inputs)
+        inst.noiseseed(0)
+        gradaux = inst.compute_localgradients_aux[layer](*inputs)
+        inst.noiseseed(0)
+        gradtot = inst.compute_localgradients[layer](*inputs)
+        
+        for j in range(len(gradtot)):
+            if str(self.daaig[layer].params[j]) is param_name:
+                tmpin = numpy.sqrt((pow(inst.daaig[layer].scale_cost_in,2)*gradin[j]*gradin[j]).sum()) \
+                                if type(gradin) is list else 0
+                tmpaux= numpy.sqrt((pow(inst.daaig[layer].scale_cost_aux,2)*gradaux[j]*gradaux[j]).sum())\
+                                 if type(gradaux) is list else 0
+                tmptot = numpy.sqrt((gradtot[j]*gradtot[j]).sum()) if type(gradtot) is list else 0
+                
+                if type(gradin) is list and type(gradaux) is list and (gradin[j]*gradin[j]).sum() != 0:
+                    projauxin =(inst.daaig[layer].scale_cost_aux*gradaux[j] * \
+                                inst.daaig[layer].scale_cost_in*gradin[j]).sum()/ \
+                                (numpy.sqrt((pow(inst.daaig[layer].scale_cost_in,2)*gradin[j]*gradin[j]).sum()))
+                else:
+                    projauxin = 0
+                return tmpin, tmpaux, tmptot, tmpin/(tmpaux+tmpin)*100, projauxin/tmpaux*100 if tmpaux != 0 else 0
+    
     def _instance_noiseseed(self,inst,seed):
         scannoise.R.rand.seed(seed)
         for i in range(self.depth):
@@ -820,3 +853,13 @@
                         cost[layer] = inst.globalupdate[i](*data)
         if printcost:
             print cost
+        return cost
+    
+    def _instance_supupdate(self,inst,data,typeup = 'global',printcost = False):
+        if typeup is 'local':
+            cost[-1] = inst.localupdate[-1](*data)
+        if typeup is 'global':
+            cost[-1] = inst.globalupdate[-1](*data)
+        if printcost:
+            print cost
+        return cost