diff mlp.py @ 121:2ca8dccba270

debugging mlp.py
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Wed, 07 May 2008 16:08:18 -0400
parents d0a1bd0378c6
children 4efe6d36c061
line wrap: on
line diff
--- a/mlp.py	Wed May 07 15:46:24 2008 -0400
+++ b/mlp.py	Wed May 07 16:08:18 2008 -0400
@@ -1,7 +1,6 @@
 
 from learner import *
 from theano import tensor as t
-from theano.scalar import as_scalar
 from nnet_ops import *
 
 # this is one of the simplest example of learner, and illustrates
@@ -65,6 +64,27 @@
 
     """
 
+    def __init__(self,n_hidden,n_classes,learning_rate,init_range=1.):
+        self._n_outputs = n_classes
+        self._n_hidden = n_hidden
+        self._init_range = init_range
+        self.learning_rate = learning_rate # this is the float
+        self._learning_rate = t.scalar('learning_rate') # this is the symbol
+        self._input = t.matrix('input') # n_examples x n_inputs
+        self._target = t.matrix('target','int32') # n_examples x n_outputs
+        self._L2_regularizer = t.scalar('L2_regularizer')
+        self._W1 = t.matrix('W1')
+        self._W2 = t.matrix('W2')
+        self._b1 = t.row('b1')
+        self._b2 = t.row('b2')
+        self._regularization_term = self._L2_regularizer * (t.dot(self._W1,self._W1) + t.dot(self._W2,self._W2))
+        self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T)
+        self._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target)
+        self._output_class = t.argmax(self._output,1)
+        self._class_error = self._output_class != self._target
+        self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0]
+        MinibatchUpdatesTLearner.__init__(self)
+            
     def attributeNames(self):
         return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"]
 
@@ -95,28 +115,6 @@
             output_fields += ["class_error", "nll"]
         return output_fields
         
-    def __init__(self,n_hidden,n_classes,learning_rate,init_range=1.):
-        self._n_outputs = n_classes
-        self._n_hidden = n_hidden
-        self._init_range = init_range
-        self.learning_rate = learning_rate # this is the float
-        self._learning_rate = t.scalar('learning_rate') # this is the symbol
-        self._input = t.matrix('input') # n_examples x n_inputs
-        self._target = t.matrix('target') # n_examples x n_outputs
-        self._L2_regularizer = as_scalar(0.,'L2_regularizer')
-        self._W1 = t.matrix('W1')
-        self._W2 = t.matrix('W2')
-        self._b1 = t.row('b1')
-        self._b2 = t.row('b2')
-        self._regularizer = self._L2_regularizer * (t.dot(self._W1,self._W1) + t.dot(self._W2,self._W2))
-        self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T)
-        self._output = t.softmax(self._output_activations)
-        self._output_class = t.argmax(self._output,1)
-        self._class_error = self._output_class != self._target
-        self._nll,self._output = crossentropy_softmax_1hot(self._output_activation,self._target)
-        self._minibatch_criterion = self._nll + self._regularizer / t.shape(self._input)[0]
-        MinibatchUpdatesTLearner.__init__(self)
-            
     def allocate(self,minibatch):
         minibatch_n_inputs  = minibatch["input"].shape[1]
         if not self._n_inputs:
@@ -234,7 +232,7 @@
     def __init__(self):
         self._input = t.matrix('input') # n_examples x n_inputs
         self._target = t.matrix('target') # n_examples x n_outputs
-        self._L2_regularizer = as_scalar(0.,'L2_regularizer')
+        self._L2_regularizer = t.scalar('L2_regularizer')
         self._theta = t.matrix('theta')
         self._W = self._theta[:,1:] 
         self._b = self._theta[:,0]