comparison 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
comparison
equal deleted inserted replaced
133:b4657441dd65 134:3f4e5c9bdc5e
66 - 'parameters' = [b1, W1, b2, W2] 66 - 'parameters' = [b1, W1, b2, W2]
67 - 'regularization_term' 67 - 'regularization_term'
68 68
69 """ 69 """
70 70
71 def __init__(self,n_hidden,n_classes,learning_rate,max_n_epochs,init_range=1.,n_inputs=None,minibatch_size=None): 71 def __init__(self,n_hidden,n_classes,learning_rate,max_n_epochs,L2_regularizer=0,init_range=1.,n_inputs=None,minibatch_size=None):
72 self._n_inputs = n_inputs 72 self._n_inputs = n_inputs
73 self._n_outputs = n_classes 73 self._n_outputs = n_classes
74 self._n_hidden = n_hidden 74 self._n_hidden = n_hidden
75 self._init_range = init_range 75 self._init_range = init_range
76 self._max_n_epochs = max_n_epochs 76 self._max_n_epochs = max_n_epochs
77 self._minibatch_size = minibatch_size 77 self._minibatch_size = minibatch_size
78 self.learning_rate = learning_rate # this is the float 78 self.learning_rate = learning_rate # this is the float
79 self.L2_regularizer = L2_regularizer
79 self._learning_rate = t.scalar('learning_rate') # this is the symbol 80 self._learning_rate = t.scalar('learning_rate') # this is the symbol
80 self._input = t.matrix('input') # n_examples x n_inputs 81 self._input = t.matrix('input') # n_examples x n_inputs
81 self._target = t.ivector('target') # n_examples x n_outputs 82 self._target = t.imatrix('target') # n_examples x 1
83 self._target_vector = self._target[:,0]
82 self._L2_regularizer = t.scalar('L2_regularizer') 84 self._L2_regularizer = t.scalar('L2_regularizer')
83 self._W1 = t.matrix('W1') 85 self._W1 = t.matrix('W1')
84 self._W2 = t.matrix('W2') 86 self._W2 = t.matrix('W2')
85 self._b1 = t.row('b1') 87 self._b1 = t.row('b1')
86 self._b2 = t.row('b2') 88 self._b2 = t.row('b2')
87 self._regularization_term = self._L2_regularizer * (t.sum(self._W1*self._W1) + t.sum(self._W2*self._W2)) 89 self._regularization_term = self._L2_regularizer * (t.sum(self._W1*self._W1) + t.sum(self._W2*self._W2))
88 self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T) 90 self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T)
89 self._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target) 91 self._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target_vector)
90 self._output_class = t.argmax(self._output,1) 92 self._output_class, self._max_output = t.argmax(self._output,1)
91 self._class_error = self._output_class != self._target 93 self._class_error = t.neq(self._output_class,self._target_vector)
92 self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0] 94 self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0]
93 OnlineGradientTLearner.__init__(self) 95 OnlineGradientTLearner.__init__(self)
94 96
95 def attributeNames(self): 97 def attributeNames(self):
96 return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"] 98 return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"]
97 99
98 def parameterAttributes(self): 100 def parameterAttributes(self):
99 return ["b1","W1", "b2", "W2"] 101 return ["b1","W1", "b2", "W2"]
100 102
101 def useInputAttributes(self):
102 return self.parameterAttributes()
103
104 def useOutputAttributes(self):
105 return []
106
107 def updateInputAttributes(self):
108 return self.parameterAttributes() + ["L2_regularizer"]
109
110 def updateMinibatchInputFields(self): 103 def updateMinibatchInputFields(self):
111 return ["input","target"] 104 return ["input","target"]
112 105
113 def updateEndOutputAttributes(self): 106 def updateEndOutputAttributes(self):
114 return ["regularization_term"] 107 return ["regularization_term"]
124 117
125 def allocate(self,minibatch): 118 def allocate(self,minibatch):
126 minibatch_n_inputs = minibatch["input"].shape[1] 119 minibatch_n_inputs = minibatch["input"].shape[1]
127 if not self._n_inputs: 120 if not self._n_inputs:
128 self._n_inputs = minibatch_n_inputs 121 self._n_inputs = minibatch_n_inputs
129 self.b1 = numpy.zeros(self._n_hidden) 122 self.b1 = numpy.zeros((1,self._n_hidden))
130 self.b2 = numpy.zeros(self._n_outputs) 123 self.b2 = numpy.zeros((1,self._n_outputs))
131 self.forget() 124 self.forget()
132 elif self._n_inputs!=minibatch_n_inputs: 125 elif self._n_inputs!=minibatch_n_inputs:
133 # if the input changes dimension on the fly, we resize and forget everything 126 # if the input changes dimension on the fly, we resize and forget everything
134 self.forget() 127 self.forget()
135 128