Mercurial > pylearn
annotate mlp_factory_approach.py @ 207:c5a7105fa40b
trying to merge
author | Yoshua Bengio <bengioy@iro.umontreal.ca> |
---|---|
date | Fri, 16 May 2008 16:38:15 -0400 |
parents | f2ddc795ec49 e816821c1e50 |
children | bf320808919f |
rev | line source |
---|---|
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
1 import dataset |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
2 import theano |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
3 import theano.tensor as t |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
4 import numpy |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
5 import nnet_ops |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
6 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
7 def _randshape(*shape): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
8 return (numpy.random.rand(*shape) -0.5) * 0.001 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
9 def _function(inputs, outputs, linker='c&py'): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
10 return theano.function(inputs, outputs, unpack_single=False,linker=linker) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
11 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
12 class NeuralNet(object): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
13 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
14 class Model(object): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
15 def __init__(self, nnet, params): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
16 self.nnet = nnet |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 self.params = params |
190
aa7a3ecbcc90
progress toward early stopping
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
189
diff
changeset
|
18 |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
19 def update(self, trainset, stopper=None): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
20 """Update this model from more training data.""" |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
21 v = self.nnet.v |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
22 params = self.params |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
23 update_fn = _function([v.input, v.target] + v.params, [v.nll] + v.new_params) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
24 if stopper is not None: |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
25 raise NotImplementedError() |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
26 else: |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
27 for i in xrange(100): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
28 for input, target in trainset.minibatches(['input', 'target'], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
29 minibatch_size=min(32, len(trainset))): |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
30 results = update_fn(input, target[:,0], *params) |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
31 if 0: print results[0] |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
32 # print params['b'] |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
33 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
34 def __call__(self, testset, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
35 output_fieldnames=['output_class'], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
36 test_stats_collector=None, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
37 copy_inputs=False, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 put_stats_in_output_dataset=True, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 output_attributes=[]): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
40 """Apply this model (as a function) to new data""" |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
41 inputs = [self.nnet.v.input, self.nnet.v.target] + self.nnet.v.params |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
42 fn = _function(inputs, [getattr(self.nnet.v, name) for name in output_fieldnames]) |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
43 if 'target' in testset.fieldNames(): |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
44 return dataset.ApplyFunctionDataSet(testset, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
45 lambda input, target: fn(input, target[:,0], *self.params), |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
46 output_fieldnames) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
47 else: |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
48 return dataset.ApplyFunctionDataSet(testset, |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
49 lambda input: fn(input, numpy.zeros(1,dtype='int64'), *self.params), |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
50 output_fieldnames) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
51 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
52 def __init__(self, ninputs, nhid, nclass, lr, nepochs, |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
53 l2coef=0.0, |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
54 linker='c&yp', |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
55 hidden_layer=None): |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
56 if not hidden_layer: |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
57 hidden_layer = AffineSigmoidLayer("hidden",ninputs,nhid,l2coef) |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
58 class Vars: |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
59 def __init__(self, lr, l2coef): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
60 lr = t.constant(lr) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
61 l2coef = t.constant(l2coef) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
62 input = t.matrix('input') # n_examples x n_inputs |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
63 target = t.ivector('target') # n_examples x 1 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 W2 = t.matrix('W2') |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 b2 = t.vector('b2') |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
66 |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
67 hid = hidden_layer(input) |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
68 hid_params = hidden_layer.params() |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
69 hid_params_init_vals = hidden_layer.params_ivals() |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
70 hid_regularization = hidden_layer.regularization() |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
71 |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 params = [W2, b2] + hid_params |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
73 nll, predictions = nnet_ops.crossentropy_softmax_1hot( b2 + t.dot(hid, W2), target) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 regularization = l2coef * t.sum(W2*W2) + hid_regularization |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 output_class = t.argmax(predictions,1) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
76 loss_01 = t.neq(output_class, target) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
77 g_params = t.grad(nll + regularization, params) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
78 new_params = [t.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
79 setattr_and_name(self, locals()) |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 self.nhid = nhid |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
81 self.nclass = nclass |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
82 self.nepochs = nepochs |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
83 self.v = Vars(lr, l2coef) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
84 self.params = None |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
85 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 def __call__(self, trainset=None, iparams=None): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
87 if iparams is None: |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
88 iparams = LookupList(["W","b"],[_randshape(self.nhid, self.nclass), _randshape(self.nclass)]) |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
89 + self.v.hid_params_init_vals() |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
90 rval = NeuralNet.Model(self, iparams) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
91 if trainset: |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
92 rval.update(trainset) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
93 return rval |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
94 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
95 |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
96 def setattr_and_name(self, dict): |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
97 """This will do a self.__setattr__ for all elements in the dict |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
98 (except for element self). In addition it will make sure that |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
99 each element's .name (if it exists) is set to the element's key |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
100 in the dicitonary. |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
101 Typical usage: setattr_and_name(self, locals()) """ |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
102 for varname,var in locals.items(): |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
103 if var is not self: |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
104 if hasattr(var,"name") and not var.name: |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
105 var.name=varname |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
106 self.__setattr__(varname,var) |
191
e816821c1e50
added early stopping to mlp.__call__
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
190
diff
changeset
|
107 |
e816821c1e50
added early stopping to mlp.__call__
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
190
diff
changeset
|
108 |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
109 if __name__ == '__main__': |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
110 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
111 [0, 1, 1], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
112 [1, 0, 1], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
113 [1, 1, 1]]), |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
114 {'input':slice(2),'target':2}) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
115 training_set2 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
116 [0, 1, 1], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
117 [1, 0, 0], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
118 [1, 1, 1]]), |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
119 {'input':slice(2),'target':2}) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
120 test_data = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
121 [0, 1, 1], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
122 [1, 0, 0], |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
123 [1, 1, 1]]), |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
124 {'input':slice(2)}) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
125 |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
126 |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
127 learn_algo = NeuralNet(2, 10, 3, .1, 1000) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
128 |
206
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
129 model = learn_algo() |
f2ddc795ec49
changes made with Pascal but should probably be discarded
Yoshua Bengio <bengioy@iro.umontreal.ca>
parents:
187
diff
changeset
|
130 |
187
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
131 model1 = learn_algo(training_set1) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
132 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
133 model2 = learn_algo(training_set2) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
134 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
135 n_match = 0 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
136 for o1, o2 in zip(model1(test_data), model2(test_data)): |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
137 n_match += (o1 == o2) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
138 |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
139 print n_match, numpy.sum(training_set1.fields()['target'] == |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
140 training_set2.fields()['target']) |
ebbb0e749565
added mlp_factory_approach
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
141 |