comparison mlp.py @ 186:562f308873f0

added ManualNNet
author James Bergstra <bergstrj@iro.umontreal.ca>
date Tue, 13 May 2008 20:10:03 -0400
parents 25d0a0c713da
children ebbb0e749565
comparison
equal deleted inserted replaced
185:3d953844abd3 186:562f308873f0
8 from learner import * 8 from learner import *
9 from theano import tensor as t 9 from theano import tensor as t
10 from nnet_ops import * 10 from nnet_ops import *
11 import math 11 import math
12 from misc import * 12 from misc import *
13
14 def function(inputs, outputs, linker='c&py'):
15 return theano.function(inputs, outputs, unpack_single=False,linker=linker)
16
17 def randshape(*shape): return (numpy.random.rand(*shape) -0.5) * 0.001
18
19 class ManualNNet(object):
20 def __init__(self, ninputs, nhid, nclass, lr, nepochs,
21 linker='c&yp',
22 hidden_layer=None):
23 class Vars:
24 def __init__(self, lr):
25 lr = t.constant(lr)
26 input = t.matrix('input') # n_examples x n_inputs
27 target = t.ivector('target') # n_examples x 1
28 W2 = t.matrix('W2')
29 b2 = t.vector('b2')
30
31 if hidden_layer:
32 hidden, hidden_params, hidden_ivals = hidden_layer(input)
33 else:
34 W1 = t.matrix('W1')
35 b1 = t.vector('b1')
36 hidden = t.tanh(b1 + t.dot(input, W1))
37 hidden_params = [W1, b1]
38 hidden_ivals = [randshape(ninputs, nhid), randshape(nhid)]
39
40 params = [W2, b2] + hidden_params
41 ivals = [randshape(nhid, nclass), randshape(nclass)]\
42 + hidden_ivals
43 nll, predictions = crossentropy_softmax_1hot( b2 + t.dot(hidden, W2), target)
44 output_class = t.argmax(predictions,1)
45 loss_01 = t.neq(output_class, target)
46 g_params = t.grad(nll, params)
47 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)]
48 self.__dict__.update(locals()); del self.self
49 self.nhid = nhid
50 self.nclass = nclass
51 self.nepochs = nepochs
52 self.v = Vars(lr)
53 self.params = None
54
55 def update(self, trainset):
56 params = self.v.ivals
57 update_fn = function(
58 [self.v.input, self.v.target] + self.v.params,
59 [self.v.nll] + self.v.new_params)
60 for i in xrange(self.nepochs):
61 for input, target in trainset.minibatches(['input', 'target'],
62 minibatch_size=min(32, len(trainset))):
63 dummy = update_fn(input, target[:,0], *params)
64 if 0: print dummy[0] #the nll
65 return self.use
66 __call__ = update
67
68 def use(self, dset,
69 output_fieldnames=['output_class'],
70 test_stats_collector=None,
71 copy_inputs=False,
72 put_stats_in_output_dataset=True,
73 output_attributes=[]):
74 inputs = [self.v.input, self.v.target] + self.v.params
75 fn = function(inputs, [getattr(self.v, name) for name in output_fieldnames])
76 target = dset.fields()['target'] if ('target' in dset.fields()) else numpy.zeros((1,1),dtype='int64')
77 return ApplyFunctionDataSet(dset,
78 lambda input, target: fn(input, target[:,0], *self.v.ivals),
79 output_fieldnames)
80
13 81
14 class OneHiddenLayerNNetClassifier(OnlineGradientTLearner): 82 class OneHiddenLayerNNetClassifier(OnlineGradientTLearner):
15 """ 83 """
16 Implement a straightforward classicial feedforward 84 Implement a straightforward classicial feedforward
17 one-hidden-layer neural net, with L2 regularization. 85 one-hidden-layer neural net, with L2 regularization.