comparison 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
comparison
equal deleted inserted replaced
120:5fa46297191b 121:2ca8dccba270
1 1
2 from learner import * 2 from learner import *
3 from theano import tensor as t 3 from theano import tensor as t
4 from theano.scalar import as_scalar
5 from nnet_ops import * 4 from nnet_ops import *
6 5
7 # this is one of the simplest example of learner, and illustrates 6 # this is one of the simplest example of learner, and illustrates
8 # the use of theano 7 # the use of theano
9 8
63 - 'parameters' = [b1, W1, b2, W2] 62 - 'parameters' = [b1, W1, b2, W2]
64 - 'regularization_term' 63 - 'regularization_term'
65 64
66 """ 65 """
67 66
68 def attributeNames(self):
69 return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"]
70
71 def parameterAttributes(self):
72 return ["b1","W1", "b2", "W2"]
73
74 def useInputAttributes(self):
75 return self.parameterAttributes()
76
77 def useOutputAttributes(self):
78 return []
79
80 def updateInputAttributes(self):
81 return self.parameterAttributes() + ["L2_regularizer"]
82
83 def updateMinibatchInputFields(self):
84 return ["input","target"]
85
86 def updateEndOutputAttributes(self):
87 return ["regularization_term"]
88
89 def lossAttribute(self):
90 return "minibatch_criterion"
91
92 def defaultOutputFields(self, input_fields):
93 output_fields = ["output", "output_class",]
94 if "target" in input_fields:
95 output_fields += ["class_error", "nll"]
96 return output_fields
97
98 def __init__(self,n_hidden,n_classes,learning_rate,init_range=1.): 67 def __init__(self,n_hidden,n_classes,learning_rate,init_range=1.):
99 self._n_outputs = n_classes 68 self._n_outputs = n_classes
100 self._n_hidden = n_hidden 69 self._n_hidden = n_hidden
101 self._init_range = init_range 70 self._init_range = init_range
102 self.learning_rate = learning_rate # this is the float 71 self.learning_rate = learning_rate # this is the float
103 self._learning_rate = t.scalar('learning_rate') # this is the symbol 72 self._learning_rate = t.scalar('learning_rate') # this is the symbol
104 self._input = t.matrix('input') # n_examples x n_inputs 73 self._input = t.matrix('input') # n_examples x n_inputs
105 self._target = t.matrix('target') # n_examples x n_outputs 74 self._target = t.matrix('target','int32') # n_examples x n_outputs
106 self._L2_regularizer = as_scalar(0.,'L2_regularizer') 75 self._L2_regularizer = t.scalar('L2_regularizer')
107 self._W1 = t.matrix('W1') 76 self._W1 = t.matrix('W1')
108 self._W2 = t.matrix('W2') 77 self._W2 = t.matrix('W2')
109 self._b1 = t.row('b1') 78 self._b1 = t.row('b1')
110 self._b2 = t.row('b2') 79 self._b2 = t.row('b2')
111 self._regularizer = self._L2_regularizer * (t.dot(self._W1,self._W1) + t.dot(self._W2,self._W2)) 80 self._regularization_term = self._L2_regularizer * (t.dot(self._W1,self._W1) + t.dot(self._W2,self._W2))
112 self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T) 81 self._output_activations =self._b2+t.dot(t.tanh(self._b1+t.dot(self._input,self._W1.T)),self._W2.T)
113 self._output = t.softmax(self._output_activations) 82 self._nll,self._output = crossentropy_softmax_1hot(self._output_activations,self._target)
114 self._output_class = t.argmax(self._output,1) 83 self._output_class = t.argmax(self._output,1)
115 self._class_error = self._output_class != self._target 84 self._class_error = self._output_class != self._target
116 self._nll,self._output = crossentropy_softmax_1hot(self._output_activation,self._target) 85 self._minibatch_criterion = self._nll + self._regularization_term / t.shape(self._input)[0]
117 self._minibatch_criterion = self._nll + self._regularizer / t.shape(self._input)[0]
118 MinibatchUpdatesTLearner.__init__(self) 86 MinibatchUpdatesTLearner.__init__(self)
119 87
88 def attributeNames(self):
89 return ["parameters","b1","W2","b2","W2", "L2_regularizer","regularization_term"]
90
91 def parameterAttributes(self):
92 return ["b1","W1", "b2", "W2"]
93
94 def useInputAttributes(self):
95 return self.parameterAttributes()
96
97 def useOutputAttributes(self):
98 return []
99
100 def updateInputAttributes(self):
101 return self.parameterAttributes() + ["L2_regularizer"]
102
103 def updateMinibatchInputFields(self):
104 return ["input","target"]
105
106 def updateEndOutputAttributes(self):
107 return ["regularization_term"]
108
109 def lossAttribute(self):
110 return "minibatch_criterion"
111
112 def defaultOutputFields(self, input_fields):
113 output_fields = ["output", "output_class",]
114 if "target" in input_fields:
115 output_fields += ["class_error", "nll"]
116 return output_fields
117
120 def allocate(self,minibatch): 118 def allocate(self,minibatch):
121 minibatch_n_inputs = minibatch["input"].shape[1] 119 minibatch_n_inputs = minibatch["input"].shape[1]
122 if not self._n_inputs: 120 if not self._n_inputs:
123 self._n_inputs = minibatch_n_inputs 121 self._n_inputs = minibatch_n_inputs
124 self.b1 = numpy.zeros(self._n_hidden) 122 self.b1 = numpy.zeros(self._n_hidden)
232 return output_fields 230 return output_fields
233 231
234 def __init__(self): 232 def __init__(self):
235 self._input = t.matrix('input') # n_examples x n_inputs 233 self._input = t.matrix('input') # n_examples x n_inputs
236 self._target = t.matrix('target') # n_examples x n_outputs 234 self._target = t.matrix('target') # n_examples x n_outputs
237 self._L2_regularizer = as_scalar(0.,'L2_regularizer') 235 self._L2_regularizer = t.scalar('L2_regularizer')
238 self._theta = t.matrix('theta') 236 self._theta = t.matrix('theta')
239 self._W = self._theta[:,1:] 237 self._W = self._theta[:,1:]
240 self._b = self._theta[:,0] 238 self._b = self._theta[:,0]
241 self._XtX = t.matrix('XtX') 239 self._XtX = t.matrix('XtX')
242 self._XtY = t.matrix('XtY') 240 self._XtY = t.matrix('XtY')