changeset 703:9078561a7c21

added a parameter to SatckedDAAig
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Thu, 21 May 2009 12:18:36 -0400
parents f76079ba8d9a
children 521c04f8d2b3
files pylearn/algorithms/sandbox/DAA_inputs_groups.py
diffstat 1 files changed, 36 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pylearn/algorithms/sandbox/DAA_inputs_groups.py	Wed May 20 13:40:10 2009 -0400
+++ b/pylearn/algorithms/sandbox/DAA_inputs_groups.py	Thu May 21 12:18:36 2009 -0400
@@ -306,11 +306,11 @@
 #-----------------------------------------------------------------------------------------------------------------------
 
 class StackedDAAig(module.Module):
-	def __init__(self, depth = 1, input = None, auxinput = [None],
-				in_size = None, auxin_size = [[None]], n_hid = [1],
+	def __init__(self, depth = 1, input = T.dmatrix('input'), auxinput = [None],
+				in_size = None, auxin_size = [None], n_hid = [1],
 				regularize = False, tie_weights = False, hid_fn = 'sigmoid_act',
 				reconstruction_cost_function=cost.cross_entropy,
-				n_out = 2, target = None, **init):
+				n_out = 2, target = None, totalupdatebool=True, **init):
 		
 		super(StackedDAAig, self).__init__()
 		print '\t**** StackedDAAig.__init__ ****'
@@ -331,7 +331,8 @@
 		self.hid_fn = hid_fn
 		self.reconstruction_cost_function = reconstruction_cost_function
 		self.n_out = n_out
-		self.target = target
+		self.target = target if not(target is None) else T.lvector('target')
+		self.totalupdatebool = totalupdatebool
 		
 		# init for model construction
 		inputprec = input
@@ -345,19 +346,22 @@
 		# methods
 		self.localupdate = [None] * (self.depth+1) #update only on the layer parameters
 		self.globalupdate = [None] * (self.depth+1)#update wrt the layer cost backproped untill the input layer
-		self.totalupdate = [None] * (self.depth+1) #update wrt all the layers cost backproped untill the input layer
+		if self.totalupdatebool:
+			self.totalupdate = [None] * (self.depth+1) #update wrt all the layers cost backproped untill the input layer
 		#
 		self.representation = [None] * (self.depth+1)
 		self.reconstruction = [None] * (self.depth)
 		self.compute_localcost = [None] * (self.depth+1)
 		self.compute_globalcost = [None] * (self.depth+1)
-		self.compute_totalcost = [None] * (self.depth+1)
+		if self.totalupdatebool:
+			self.compute_totalcost = [None] * (self.depth+1)
 		self.validate = [None] * (self.depth)
 		self.noisyinputs = [None] * (self.depth)
 		#
 		self.localcost = [None] * (self.depth+1)
 		self.globalcost = [None] * (self.depth+1)
-		self.totalcost = [None] * (self.depth+1)
+		if self.totalupdatebool:
+			self.totalcost = [None] * (self.depth+1)
 		
 		paramstot = []
 		paramsenc = []
@@ -390,34 +394,40 @@
 			if self.regularize:
 				self.localcost[i] = self.daaig[i].noise.cost+self.daaig[i].regularization
 				self.globalcost[i] = self.daaig[i].noise.cost+self.daaig[i].regularization
-				self.totalcost[i] = self.daaig[i].noise.cost+self.daaig[i].regularization
+				if self.totalupdatebool:
+					self.totalcost[i] = self.daaig[i].noise.cost+self.daaig[i].regularization
 				for j in range(i):
 					self.globalcost[i] += self.daaig[j].regularizationenc
-					self.totalcost[i] += self.daaig[j].noise.cost+self.daaig[j].regularization
+					if self.totalupdatebool:
+						self.totalcost[i] += self.daaig[j].noise.cost+self.daaig[j].regularization
 				
 			else:
 				self.localcost[i] = self.daaig[i].noise.cost
 				self.globalcost[i] = self.daaig[i].noise.cost
-				self.totalcost[i] = self.daaig[i].noise.cost
-				for j in range(i):
-					self.totalcost[i] += self.daaig[j].noise.cost
+				if self.totalupdatebool:
+					self.totalcost[i] = self.daaig[i].noise.cost
+					for j in range(i):
+						self.totalcost[i] += self.daaig[j].noise.cost
 			
 			local_grads = dict((j, j - self.unsup_lr * T.grad(self.localcost[i], j))\
 					for j in self.daaig[i].params)
 			global_grads = dict((j, j - self.unsup_lr * T.grad(self.globalcost[i], j))\
 					for j in (self.daaig[i].params+paramsenc))
-			total_grads = dict((j, j - self.unsup_lr * T.grad(self.totalcost[i], j))\
-					for j in (paramstot))
+			if self.totalupdatebool:
+				total_grads = dict((j, j - self.unsup_lr * T.grad(self.totalcost[i], j))\
+						for j in (paramstot))
 			
 			self.localupdate[i] = theano.Method(self.inputs[i],self.localcost[i],local_grads)
 			self.globalupdate[i] = theano.Method(self.inputs[i],self.globalcost[i],global_grads)
-			self.totalupdate[i] = theano.Method(self.inputs[i],self.totalcost[i],total_grads)
+			if self.totalupdatebool:
+				self.totalupdate[i] = theano.Method(self.inputs[i],self.totalcost[i],total_grads)
 			#
 			self.representation[i] = theano.Method(self.inputs[i],self.daaig[i].clean.hidden)
 			self.reconstruction[i] = theano.Method(self.inputs[i],self.daaig[i].clean.rec)
 			self.compute_localcost[i] = theano.Method(self.inputs[i],self.localcost[i])
 			self.compute_globalcost[i] = theano.Method(self.inputs[i],self.globalcost[i])
-			self.compute_totalcost[i] = theano.Method(self.inputs[i],self.totalcost[i])
+			if self.totalupdatebool:
+				self.compute_totalcost[i] = theano.Method(self.inputs[i],self.totalcost[i])
 			self.validate[i] =theano.Method(self.inputs[i], [self.daaig[i].clean.cost, self.daaig[i].clean.rec])
 			noisyout = []
 			if not(inputprec is None):
@@ -443,22 +453,26 @@
 			self.localcost[-1] = self.daaig[-1].unregularized_cost
 			self.globalcost[-1] = self.daaig[-1].unregularized_cost
 		
-		self.totalcost[-1] = self.totalcost[-2] + self.localcost[-1]
+		if self.totalupdatebool:
+			self.totalcost[-1] = self.totalcost[-2] + self.localcost[-1]
 		
 		local_grads = dict((j, j - self.sup_lr * T.grad(self.localcost[-1], j))\
 					for j in self.daaig[-1].params)
 		global_grads = dict((j, j - self.sup_lr * T.grad(self.globalcost[-1], j))\
 					for j in (self.daaig[-1].params+paramsenc))
-		total_grads = dict((j, j - \
-				(self.unsup_lr * T.grad(self.totalcost[-2], j) + self.sup_lr *T.grad(self.globalcost[-1], j)))\
-				for j in paramstot)
+		if self.totalupdatebool:
+			total_grads = dict((j, j - \
+					(self.unsup_lr * T.grad(self.totalcost[-2], j) + self.sup_lr *T.grad(self.globalcost[-1], j)))\
+					for j in paramstot)
 		
 		self.localupdate[-1] = theano.Method(self.inputs[-1],self.localcost[-1],local_grads)
 		self.globalupdate[-1] = theano.Method(self.inputs[-1],self.globalcost[-1],global_grads)
-		self.totalupdate[-1] = theano.Method(self.inputs[-1],self.totalcost[-1],total_grads)
+		if self.totalupdatebool:
+			self.totalupdate[-1] = theano.Method(self.inputs[-1],self.totalcost[-1],total_grads)
 		self.compute_localcost[-1] = theano.Method(self.inputs[-1],self.localcost[-1])
 		self.compute_globalcost[-1] = theano.Method(self.inputs[-1],self.globalcost[-1])
-		self.compute_totalcost[-1] = theano.Method(self.inputs[-1],self.totalcost[-1])
+		if self.totalupdatebool:
+			self.compute_totalcost[-1] = theano.Method(self.inputs[-1],self.totalcost[-1])
 		self.representation[-1] = theano.Method(self.inputs[-2],self.daaig[-1].argmax_standalone)
 	
 	def _instance_initialize(self,inst,unsup_lr = 0.1, sup_lr = 0.01, reg_coef = 0,