comparison mlp.py @ 187:ebbb0e749565

added mlp_factory_approach
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 14 May 2008 11:51:08 -0400
parents 562f308873f0
children d1359de1ea13
comparison
equal deleted inserted replaced
186:562f308873f0 187:ebbb0e749565
19 class ManualNNet(object): 19 class ManualNNet(object):
20 def __init__(self, ninputs, nhid, nclass, lr, nepochs, 20 def __init__(self, ninputs, nhid, nclass, lr, nepochs,
21 linker='c&yp', 21 linker='c&yp',
22 hidden_layer=None): 22 hidden_layer=None):
23 class Vars: 23 class Vars:
24 def __init__(self, lr): 24 def __init__(self, lr, l2coef=0.0):
25 lr = t.constant(lr) 25 lr = t.constant(lr)
26 l2coef = t.constant(l2coef)
26 input = t.matrix('input') # n_examples x n_inputs 27 input = t.matrix('input') # n_examples x n_inputs
27 target = t.ivector('target') # n_examples x 1 28 target = t.ivector('target') # n_examples x 1
28 W2 = t.matrix('W2') 29 W2 = t.matrix('W2')
29 b2 = t.vector('b2') 30 b2 = t.vector('b2')
30 31
31 if hidden_layer: 32 if hidden_layer:
32 hidden, hidden_params, hidden_ivals = hidden_layer(input) 33 hid, hid_params, hid_ivals, hid_regularization = hidden_layer(input)
33 else: 34 else:
34 W1 = t.matrix('W1') 35 W1 = t.matrix('W1')
35 b1 = t.vector('b1') 36 b1 = t.vector('b1')
36 hidden = t.tanh(b1 + t.dot(input, W1)) 37 hid = t.tanh(b1 + t.dot(input, W1))
37 hidden_params = [W1, b1] 38 hid_params = [W1, b1]
38 hidden_ivals = [randshape(ninputs, nhid), randshape(nhid)] 39 hid_regularization = l2coef * t.sum(W1*W1)
39 40 hid_ivals = [randshape(ninputs, nhid), randshape(nhid)]
40 params = [W2, b2] + hidden_params 41
42 params = [W2, b2] + hid_params
41 ivals = [randshape(nhid, nclass), randshape(nclass)]\ 43 ivals = [randshape(nhid, nclass), randshape(nclass)]\
42 + hidden_ivals 44 + hid_ivals
43 nll, predictions = crossentropy_softmax_1hot( b2 + t.dot(hidden, W2), target) 45 nll, predictions = crossentropy_softmax_1hot( b2 + t.dot(hid, W2), target)
46 regularization = l2coef * t.sum(W2*W2) + hid_regularization
44 output_class = t.argmax(predictions,1) 47 output_class = t.argmax(predictions,1)
45 loss_01 = t.neq(output_class, target) 48 loss_01 = t.neq(output_class, target)
46 g_params = t.grad(nll, params) 49 g_params = t.grad(nll + regularization, params)
47 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] 50 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)]
48 self.__dict__.update(locals()); del self.self 51 self.__dict__.update(locals()); del self.self
49 self.nhid = nhid 52 self.nhid = nhid
50 self.nclass = nclass 53 self.nclass = nclass
51 self.nepochs = nepochs 54 self.nepochs = nepochs