# HG changeset patch # User Thierry Bertin-Mahieux # Date 1212613244 14400 # Node ID a1793a5e9523c06c220c5734742e522b1a74cc2a # Parent 5614b186c5f4b82828f389212e9f8039703e7465 we can now load and save in a file, see test class in the file for an example, but basically it's model1.save(filename) or learn_algo(filename) to load diff -r 5614b186c5f4 -r a1793a5e9523 mlp_factory_approach.py --- a/mlp_factory_approach.py Tue Jun 03 21:34:40 2008 -0400 +++ b/mlp_factory_approach.py Wed Jun 04 17:00:44 2008 -0400 @@ -1,10 +1,11 @@ -import copy, sys +import copy, sys, os import numpy import theano from theano import tensor as T -from pylearn import dataset, nnet_ops, stopper, LookupList +from pylearn import dataset, nnet_ops, stopper, LookupList, filetensor + class AbstractFunction (Exception): pass @@ -35,6 +36,17 @@ raise Exception('why not called?') return GraphLearner.Model(self.algo, [copy.copy(p) for p in params]) + def __eq__(self,other,tolerance=0.) : + """ Only compares weights of matrices and bias vector. """ + if not isinstance(other,GraphLearner.Model) : + return False + for p in range(4) : + if self.params[p].shape != other.params[p].shape : + return False + if not numpy.all( numpy.abs(self.params[p] - other.params[p]) <= tolerance ) : + return False + return True + def _cache(self, key, valfn): d = self._fn_cache if key not in d: @@ -42,7 +54,7 @@ return d[key] def update_minibatch(self, minibatch): - assert isinstance(minibatch, LookupList) + #assert isinstance(minibatch, LookupList) # why false??? self.update_fn(minibatch['input'], minibatch['target'], *self.params) def update(self, dataset, @@ -53,6 +65,9 @@ for mb in dataset.minibatches(['input', 'target'], minibatch_size=minibatch_size): self.update_minibatch(mb) + def save(self, f): + self.algo.graph.save(f, self) + def __call__(self, testset, fieldnames=['output_class']): """Apply this model (as a function) to new data. @@ -111,6 +126,13 @@ raise AbstractFunction optimizer = Opt() + def load(self,f) : + raise AbstractFunction + + def save(self,f,model) : + raise AbstractFunction + + def __init__(self, graph): self.graph = graph @@ -145,7 +167,15 @@ @rtype: GraphLearner.Model instance """ + iparams = self.graph.iparams() if iparams is None else iparams + + # if we load, type(trainset) == 'str' + if isinstance(trainset,str) or isinstance(trainset,file): + #loadmodel = GraphLearner.Model(self, iparams) + loadmodel = self.graph.load(self,trainset) + return loadmodel + curmodel = GraphLearner.Model(self, iparams) best = curmodel @@ -166,7 +196,10 @@ curmodel = best return curmodel + def graphMLP(ninputs, nhid, nclass, lr_val, l2coef_val=0.0): + + def wrapper(i, node, thunk): if 0: print i, node @@ -199,7 +232,39 @@ g_params = T.grad(nll, params) new_params = [T.sub_inplace(p, lr * gp) for p,gp in zip(params, g_params)] + + def __eq__(self,other) : + print 'G.__eq__ from graphMLP(), not implemented yet' + return NotImplemented + + + def load(self, algo, f): + """ Load from file the 2 matrices and bias vectors """ + cloase_at_end = False + if isinstance(f,str) : + f = open(f,'r') + close_at_end = True + params = [] + for i in xrange(4): + params.append(filetensor.read(f)) + if close_at_end : + f.close() + return GraphLearner.Model(algo, params) + + def save(self, f, model): + """ Save params to file, so 2 matrices and 2 bias vectors. Same order as iparams. """ + cloase_at_end = False + if isinstance(f,str) : + f = open(f,'w') + close_at_end = True + for p in model.params: + filetensor.write(f,p) + if close_at_end : + f.close() + + def iparams(self): + """ init params. """ def randsmall(*shape): return (numpy.random.rand(*shape) -0.5) * 0.001 return [randsmall(ninputs, nhid) @@ -250,6 +315,26 @@ self.failUnless(n_match == (numpy.sum(training_set1.fields()['target'] == training_set2.fields()['target'])), omatch) + model1.save('/tmp/model1') + + denoising_aa = GraphLearner(denoising_g) + model1 = denoising_aa(trainset) + hidset = model(trainset, fieldnames=['hidden']) + model2 = denoising_aa(hidset) + + f = open('blah', 'w') + for m in model: + m.save(f) + filetensor.write(f, initial_classification_weights) + f.flush() + + deep_sigmoid_net = GraphLearner(deepnetwork_g) + deep_model = deep_sigmoid_net.load('blah') + deep_model.update(trainset) #do some fine tuning + + model1_dup = learn_algo('/tmp/model1') + + def equiv(self, g0, g1): training_set1 = dataset.ArrayDataSet(numpy.array([[0, 0, 0], [0, 1, 1],