Mercurial > pylearn
comparison mlp_factory_approach.py @ 267:4dad41215967
mergec
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 04 Jun 2008 17:49:28 -0400 |
parents | ae0a8345869b |
children | eded3cb54930 |
comparison
equal
deleted
inserted
replaced
266:6e69fb91f3c0 | 267:4dad41215967 |
---|---|
1 import copy, sys | 1 import copy, sys, os |
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 | 7 from pylearn import dataset, nnet_ops, stopper, LookupList, filetensor |
8 | |
8 | 9 |
9 class AbstractFunction (Exception): pass | 10 class AbstractFunction (Exception): pass |
10 | 11 |
11 class AutoName(object): | 12 class AutoName(object): |
12 """ | 13 """ |
33 | 34 |
34 def __copy__(self): | 35 def __copy__(self): |
35 raise Exception('why not called?') | 36 raise Exception('why not called?') |
36 return GraphLearner.Model(self.algo, [copy.copy(p) for p in params]) | 37 return GraphLearner.Model(self.algo, [copy.copy(p) for p in params]) |
37 | 38 |
39 def __eq__(self,other,tolerance=0.) : | |
40 """ Only compares weights of matrices and bias vector. """ | |
41 if not isinstance(other,GraphLearner.Model) : | |
42 return False | |
43 for p in range(4) : | |
44 if self.params[p].shape != other.params[p].shape : | |
45 return False | |
46 if not numpy.all( numpy.abs(self.params[p] - other.params[p]) <= tolerance ) : | |
47 return False | |
48 return True | |
49 | |
38 def _cache(self, key, valfn): | 50 def _cache(self, key, valfn): |
39 d = self._fn_cache | 51 d = self._fn_cache |
40 if key not in d: | 52 if key not in d: |
41 d[key] = valfn() | 53 d[key] = valfn() |
42 return d[key] | 54 return d[key] |
43 | 55 |
44 def update_minibatch(self, minibatch): | 56 def update_minibatch(self, minibatch): |
45 assert isinstance(minibatch, LookupList) | 57 #assert isinstance(minibatch, LookupList) # why false??? |
46 self.update_fn(minibatch['input'], minibatch['target'], *self.params) | 58 self.update_fn(minibatch['input'], minibatch['target'], *self.params) |
47 | 59 |
48 def update(self, dataset, | 60 def update(self, dataset, |
49 default_minibatch_size=32): | 61 default_minibatch_size=32): |
50 """Update this model from more training data.""" | 62 """Update this model from more training data.""" |
51 params = self.params | 63 params = self.params |
52 minibatch_size = min(default_minibatch_size, len(dataset)) | 64 minibatch_size = min(default_minibatch_size, len(dataset)) |
53 for mb in dataset.minibatches(['input', 'target'], minibatch_size=minibatch_size): | 65 for mb in dataset.minibatches(['input', 'target'], minibatch_size=minibatch_size): |
54 self.update_minibatch(mb) | 66 self.update_minibatch(mb) |
67 | |
68 def save(self, f): | |
69 self.algo.graph.save(f, self) | |
55 | 70 |
56 def __call__(self, testset, fieldnames=['output_class']): | 71 def __call__(self, testset, fieldnames=['output_class']): |
57 """Apply this model (as a function) to new data. | 72 """Apply this model (as a function) to new data. |
58 | 73 |
59 @param testset: DataSet, whose fields feed Result terms in self.algo.g | 74 @param testset: DataSet, whose fields feed Result terms in self.algo.g |
109 | 124 |
110 def train_iter(self, trainset): | 125 def train_iter(self, trainset): |
111 raise AbstractFunction | 126 raise AbstractFunction |
112 optimizer = Opt() | 127 optimizer = Opt() |
113 | 128 |
129 def load(self,f) : | |
130 raise AbstractFunction | |
131 | |
132 def save(self,f,model) : | |
133 raise AbstractFunction | |
134 | |
135 | |
114 def __init__(self, graph): | 136 def __init__(self, graph): |
115 self.graph = graph | 137 self.graph = graph |
116 | 138 |
117 def _fn(self, inputs, outputs): | 139 def _fn(self, inputs, outputs): |
118 # Caching here would hamper multi-threaded apps | 140 # Caching here would hamper multi-threaded apps |
143 | 165 |
144 @return: model | 166 @return: model |
145 @rtype: GraphLearner.Model instance | 167 @rtype: GraphLearner.Model instance |
146 | 168 |
147 """ | 169 """ |
170 | |
148 iparams = self.graph.iparams() if iparams is None else iparams | 171 iparams = self.graph.iparams() if iparams is None else iparams |
172 | |
173 # if we load, type(trainset) == 'str' | |
174 if isinstance(trainset,str) or isinstance(trainset,file): | |
175 #loadmodel = GraphLearner.Model(self, iparams) | |
176 loadmodel = self.graph.load(self,trainset) | |
177 return loadmodel | |
178 | |
149 curmodel = GraphLearner.Model(self, iparams) | 179 curmodel = GraphLearner.Model(self, iparams) |
150 best = curmodel | 180 best = curmodel |
151 | 181 |
152 if trainset is not None: | 182 if trainset is not None: |
153 #do some training by calling Model.update_minibatch() | 183 #do some training by calling Model.update_minibatch() |
164 stp.next() | 194 stp.next() |
165 if validset: | 195 if validset: |
166 curmodel = best | 196 curmodel = best |
167 return curmodel | 197 return curmodel |
168 | 198 |
199 | |
169 def graphMLP(ninputs, nhid, nclass, lr_val, l2coef_val=0.0): | 200 def graphMLP(ninputs, nhid, nclass, lr_val, l2coef_val=0.0): |
201 | |
202 | |
170 def wrapper(i, node, thunk): | 203 def wrapper(i, node, thunk): |
171 if 0: | 204 if 0: |
172 print i, node | 205 print i, node |
173 print thunk.inputs | 206 print thunk.inputs |
174 print thunk.outputs | 207 print thunk.outputs |
197 loss_01 = T.neq(output_class, target) | 230 loss_01 = T.neq(output_class, target) |
198 #g_params = T.grad(nll + regularization, params) | 231 #g_params = T.grad(nll + regularization, params) |
199 g_params = T.grad(nll, params) | 232 g_params = T.grad(nll, params) |
200 new_params = [T.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] | 233 new_params = [T.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] |
201 | 234 |
235 | |
236 def __eq__(self,other) : | |
237 print 'G.__eq__ from graphMLP(), not implemented yet' | |
238 return NotImplemented | |
239 | |
240 | |
241 def load(self, algo, f): | |
242 """ Load from file the 2 matrices and bias vectors """ | |
243 cloase_at_end = False | |
244 if isinstance(f,str) : | |
245 f = open(f,'r') | |
246 close_at_end = True | |
247 params = [] | |
248 for i in xrange(4): | |
249 params.append(filetensor.read(f)) | |
250 if close_at_end : | |
251 f.close() | |
252 return GraphLearner.Model(algo, params) | |
253 | |
254 def save(self, f, model): | |
255 """ Save params to file, so 2 matrices and 2 bias vectors. Same order as iparams. """ | |
256 cloase_at_end = False | |
257 if isinstance(f,str) : | |
258 f = open(f,'w') | |
259 close_at_end = True | |
260 for p in model.params: | |
261 filetensor.write(f,p) | |
262 if close_at_end : | |
263 f.close() | |
264 | |
265 | |
202 def iparams(self): | 266 def iparams(self): |
267 """ init params. """ | |
203 def randsmall(*shape): | 268 def randsmall(*shape): |
204 return (numpy.random.rand(*shape) -0.5) * 0.001 | 269 return (numpy.random.rand(*shape) -0.5) * 0.001 |
205 return [randsmall(ninputs, nhid) | 270 return [randsmall(ninputs, nhid) |
206 , randsmall(nhid) | 271 , randsmall(nhid) |
207 , randsmall(nhid, nclass) | 272 , randsmall(nhid, nclass) |
248 n_match = sum(omatch) | 313 n_match = sum(omatch) |
249 | 314 |
250 self.failUnless(n_match == (numpy.sum(training_set1.fields()['target'] == | 315 self.failUnless(n_match == (numpy.sum(training_set1.fields()['target'] == |
251 training_set2.fields()['target'])), omatch) | 316 training_set2.fields()['target'])), omatch) |
252 | 317 |
318 model1.save('/tmp/model1') | |
319 | |
320 #denoising_aa = GraphLearner(denoising_g) | |
321 #model1 = denoising_aa(trainset) | |
322 #hidset = model(trainset, fieldnames=['hidden']) | |
323 #model2 = denoising_aa(hidset) | |
324 | |
325 #f = open('blah', 'w') | |
326 #for m in model: | |
327 # m.save(f) | |
328 #filetensor.write(f, initial_classification_weights) | |
329 #f.flush() | |
330 | |
331 #deep_sigmoid_net = GraphLearner(deepnetwork_g) | |
332 #deep_model = deep_sigmoid_net.load('blah') | |
333 #deep_model.update(trainset) #do some fine tuning | |
334 | |
335 model1_dup = learn_algo('/tmp/model1') | |
336 | |
337 | |
253 def equiv(self, g0, g1): | 338 def equiv(self, g0, g1): |
254 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], | 339 training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], |
255 [0, 1, 1], | 340 [0, 1, 1], |
256 [1, 0, 1], | 341 [1, 0, 1], |
257 [1, 1, 1]]), | 342 [1, 1, 1]]), |