Mercurial > pylearn
comparison mlp_factory_approach.py @ 187:ebbb0e749565
added mlp_factory_approach
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 14 May 2008 11:51:08 -0400 |
parents | |
children | 8f58abb943d4 f2ddc795ec49 |
comparison
equal
deleted
inserted
replaced
186:562f308873f0 | 187:ebbb0e749565 |
---|---|
1 import dataset | |
2 import theano | |
3 import theano.tensor as t | |
4 import numpy | |
5 import nnet_ops | |
6 | |
7 def _randshape(*shape): | |
8 return (numpy.random.rand(*shape) -0.5) * 0.001 | |
9 def _function(inputs, outputs, linker='c&py'): | |
10 return theano.function(inputs, outputs, unpack_single=False,linker=linker) | |
11 | |
12 class NeuralNet(object): | |
13 | |
14 class Model(object): | |
15 def __init__(self, nnet, params): | |
16 self.nnet = nnet | |
17 self.params = params | |
18 | |
19 def update(self, trainset, stopper=None): | |
20 """Update this model from more training data.""" | |
21 v = self.nnet.v | |
22 params = self.params | |
23 update_fn = _function([v.input, v.target] + v.params, [v.nll] + v.new_params) | |
24 if stopper is not None: | |
25 raise NotImplementedError() | |
26 else: | |
27 for i in xrange(100): | |
28 for input, target in trainset.minibatches(['input', 'target'], | |
29 minibatch_size=min(32, len(trainset))): | |
30 dummy = update_fn(input, target[:,0], *params) | |
31 if 0: print dummy[0] #the nll | |
32 | |
33 def __call__(self, testset, | |
34 output_fieldnames=['output_class'], | |
35 test_stats_collector=None, | |
36 copy_inputs=False, | |
37 put_stats_in_output_dataset=True, | |
38 output_attributes=[]): | |
39 """Apply this model (as a function) to new data""" | |
40 inputs = [self.nnet.v.input, self.nnet.v.target] + self.nnet.v.params | |
41 fn = _function(inputs, [getattr(self.nnet.v, name) for name in output_fieldnames]) | |
42 if 'target' in testset.fields(): | |
43 return dataset.ApplyFunctionDataSet(testset, | |
44 lambda input, target: fn(input, target[:,0], *self.params), | |
45 output_fieldnames) | |
46 else: | |
47 return dataset.ApplyFunctionDataSet(testset, | |
48 lambda input: fn(input, numpy.zeros(1,dtype='int64'), *self.params), | |
49 output_fieldnames) | |
50 | |
51 def __init__(self, ninputs, nhid, nclass, lr, nepochs, | |
52 l2coef=0.0, | |
53 linker='c&yp', | |
54 hidden_layer=None): | |
55 class Vars: | |
56 def __init__(self, lr, l2coef): | |
57 lr = t.constant(lr) | |
58 l2coef = t.constant(l2coef) | |
59 input = t.matrix('input') # n_examples x n_inputs | |
60 target = t.ivector('target') # n_examples x 1 | |
61 W2 = t.matrix('W2') | |
62 b2 = t.vector('b2') | |
63 | |
64 if hidden_layer: | |
65 hid, hid_params, hid_ivals, hid_regularization = hidden_layer(input) | |
66 else: | |
67 W1 = t.matrix('W1') | |
68 b1 = t.vector('b1') | |
69 hid = t.tanh(b1 + t.dot(input, W1)) | |
70 hid_params = [W1, b1] | |
71 hid_regularization = l2coef * t.sum(W1*W1) | |
72 hid_ivals = lambda : [_randshape(ninputs, nhid), _randshape(nhid)] | |
73 | |
74 params = [W2, b2] + hid_params | |
75 nll, predictions = nnet_ops.crossentropy_softmax_1hot( b2 + t.dot(hid, W2), target) | |
76 regularization = l2coef * t.sum(W2*W2) + hid_regularization | |
77 output_class = t.argmax(predictions,1) | |
78 loss_01 = t.neq(output_class, target) | |
79 g_params = t.grad(nll + regularization, params) | |
80 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] | |
81 self.__dict__.update(locals()); del self.self | |
82 self.nhid = nhid | |
83 self.nclass = nclass | |
84 self.nepochs = nepochs | |
85 self.v = Vars(lr, l2coef) | |
86 self.params = None | |
87 | |
88 def __call__(self, trainset=None, iparams=None): | |
89 if iparams is None: | |
90 iparams = [_randshape(self.nhid, self.nclass), _randshape(self.nclass)]\ | |
91 + self.v.hid_ivals() | |
92 rval = NeuralNet.Model(self, iparams) | |
93 if trainset: | |
94 rval.update(trainset) | |
95 return rval | |
96 | |
97 | |
98 if __name__ == '__main__': | |
99 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], | |
100 [0, 1, 1], | |
101 [1, 0, 1], | |
102 [1, 1, 1]]), | |
103 {'input':slice(2),'target':2}) | |
104 training_set2 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], | |
105 [0, 1, 1], | |
106 [1, 0, 0], | |
107 [1, 1, 1]]), | |
108 {'input':slice(2),'target':2}) | |
109 test_data = dataset.ArrayDataSet(numpy.array([[0, 0, 0], | |
110 [0, 1, 1], | |
111 [1, 0, 0], | |
112 [1, 1, 1]]), | |
113 {'input':slice(2)}) | |
114 | |
115 learn_algo = NeuralNet(2, 10, 3, .1, 1000) | |
116 | |
117 model1 = learn_algo(training_set1) | |
118 | |
119 model2 = learn_algo(training_set2) | |
120 | |
121 n_match = 0 | |
122 for o1, o2 in zip(model1(test_data), model2(test_data)): | |
123 n_match += (o1 == o2) | |
124 | |
125 print n_match, numpy.sum(training_set1.fields()['target'] == | |
126 training_set2.fields()['target']) | |
127 |