comparison mlp_factory_approach.py @ 299:eded3cb54930

small bug fixed
author Thierry Bertin-Mahieux <bertinmt@iro.umontreal.ca>
date Fri, 06 Jun 2008 17:58:45 -0400
parents ae0a8345869b
children 6ead65d30f1e
comparison
equal deleted inserted replaced
298:5987415496df 299:eded3cb54930
2 import numpy 2 import numpy
3 3
4 import theano 4 import theano
5 from theano import tensor as T 5 from theano import tensor as T
6 6
7 from pylearn import dataset, nnet_ops, stopper, LookupList, filetensor 7 import dataset, nnet_ops, stopper, filetensor
8 from lookup_list import LookupList
8 9
9 10
10 class AbstractFunction (Exception): pass 11 class AbstractFunction (Exception): pass
11 12
12 class AutoName(object): 13 class AutoName(object):
52 if key not in d: 53 if key not in d:
53 d[key] = valfn() 54 d[key] = valfn()
54 return d[key] 55 return d[key]
55 56
56 def update_minibatch(self, minibatch): 57 def update_minibatch(self, minibatch):
57 #assert isinstance(minibatch, LookupList) # why false??? 58 if not isinstance(minibatch, LookupList):
59 print type(minibatch)
60 assert isinstance(minibatch, LookupList)
58 self.update_fn(minibatch['input'], minibatch['target'], *self.params) 61 self.update_fn(minibatch['input'], minibatch['target'], *self.params)
59 62
60 def update(self, dataset, 63 def update(self, dataset,
61 default_minibatch_size=32): 64 default_minibatch_size=32):
62 """Update this model from more training data.""" 65 """Update this model from more training data."""
214 lr = T.constant(lr_val) 217 lr = T.constant(lr_val)
215 assert l2coef_val == 0.0 218 assert l2coef_val == 0.0
216 l2coef = T.constant(l2coef_val) 219 l2coef = T.constant(l2coef_val)
217 input = T.matrix() # n_examples x n_inputs 220 input = T.matrix() # n_examples x n_inputs
218 target = T.ivector() # len: n_examples 221 target = T.ivector() # len: n_examples
222 #target = T.matrix()
219 W2, b2 = T.matrix(), T.vector() 223 W2, b2 = T.matrix(), T.vector()
220 224
221 W1, b1 = T.matrix(), T.vector() 225 W1, b1 = T.matrix(), T.vector()
222 hid = T.tanh(b1 + T.dot(input, W1)) 226 hid = T.tanh(b1 + T.dot(input, W1))
223 hid_regularization = l2coef * T.sum(W1*W1) 227 hid_regularization = l2coef * T.sum(W1*W1)
224 228
225 params = [W1, b1, W2, b2] 229 params = [W1, b1, W2, b2]
226 activations = b2 + T.dot(hid, W2) 230 activations = b2 + T.dot(hid, W2)
227 nll, predictions = nnet_ops.crossentropy_softmax_1hot(activations, target) 231 nll, predictions = nnet_ops.crossentropy_softmax_1hot(activations, target )
228 regularization = l2coef * T.sum(W2*W2) + hid_regularization 232 regularization = l2coef * T.sum(W2*W2) + hid_regularization
229 output_class = T.argmax(activations,1) 233 output_class = T.argmax(activations,1)
230 loss_01 = T.neq(output_class, target) 234 loss_01 = T.neq(output_class, target)
231 #g_params = T.grad(nll + regularization, params) 235 #g_params = T.grad(nll + regularization, params)
232 g_params = T.grad(nll, params) 236 g_params = T.grad(nll, params)