diff mlp.py @ 134:3f4e5c9bdc5e

Fixes to ApplyFunctionDataSet and other things to make learner and mlp work
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Fri, 09 May 2008 17:38:57 -0400
parents b4657441dd65
children ae5651a3696b
line wrap: on
line diff
--- a/mlp.py	Fri May 09 13:38:54 2008 -0400
+++ b/mlp.py	Fri May 09 17:38:57 2008 -0400
@@ -68,7 +68,7 @@
 
     """
 
-    def __init__(self,n_hidden,n_classes,learning_rate,max_n_epochs,init_range=1.,n_inputs=None,minibatch_size=None):
+    def __init__(self,n_hidden,n_classes,learning_rate,max_n_epochs,L2_regularizer=0,init_range=1.,n_inputs=None,minibatch_size=None):
         self._n_inputs = n_inputs
         self._n_outputs = n_classes
         self._n_hidden = n_hidden
@@ -76,9 +76,11 @@
         self._max_n_epochs = max_n_epochs
         self._minibatch_size = minibatch_size
         self.learning_rate = learning_rate # this is the float
+        self.L2_regularizer = L2_regularizer
         self._learning_rate = t.scalar('learning_rate') # this is the symbol
         self._input = t.matrix('input') # n_examples x n_inputs
-        self._target = t.ivector('target') # n_examples x n_outputs
+        self._target = t.imatrix('target') # n_examples x 1
+        self._target_vector = self._target[:,0]
         self._L2_regularizer = t.scalar('L2_regularizer')
         self._W1 = t.matrix('W1')
         self._W2 = t.matrix('W2')
@@ -86,9 +88,9 @@
         self._b2 = t.row('b2')
         self._regularization_term = self._L2_regularizer * (t.sum(self._W1*self._W1) + t.sum(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._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target_vector)
+        self._output_class, self._max_output = t.argmax(self._output,1)
+        self._class_error = t.neq(self._output_class,self._target_vector)
         self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0]
         OnlineGradientTLearner.__init__(self)
             
@@ -98,15 +100,6 @@
     def parameterAttributes(self):
         return ["b1","W1", "b2", "W2"]
     
-    def useInputAttributes(self):
-        return self.parameterAttributes()
-
-    def useOutputAttributes(self):
-        return []
-
-    def updateInputAttributes(self):
-        return self.parameterAttributes() + ["L2_regularizer"]
-
     def updateMinibatchInputFields(self):
         return ["input","target"]
     
@@ -126,8 +119,8 @@
         minibatch_n_inputs  = minibatch["input"].shape[1]
         if not self._n_inputs:
             self._n_inputs = minibatch_n_inputs
-            self.b1 = numpy.zeros(self._n_hidden)
-            self.b2 = numpy.zeros(self._n_outputs)
+            self.b1 = numpy.zeros((1,self._n_hidden))
+            self.b2 = numpy.zeros((1,self._n_outputs))
             self.forget()
         elif self._n_inputs!=minibatch_n_inputs:
             # if the input changes dimension on the fly, we resize and forget everything