comparison mlp_factory_approach.py @ 307:29c5ad01e9ce

merge
author James Bergstra <bergstrj@iro.umontreal.ca>
date Tue, 10 Jun 2008 18:33:15 -0400
parents 93280a0c151a
children
comparison
equal deleted inserted replaced
306:e2e5157ff044 307:29c5ad01e9ce
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 import dataset, nnet_ops, stopper, filetensor 7 import dataset, nnet_ops, stopper, filetensor
8 from lookup_list import LookupList 8 from pylearn.lookup_list import LookupList
9 9
10 10
11 class AbstractFunction (Exception): pass 11 class AbstractFunction (Exception): pass
12 12
13 class AutoName(object): 13 class AutoName(object):
60 assert isinstance(minibatch, LookupList) 60 assert isinstance(minibatch, LookupList)
61 self.update_fn(minibatch['input'], minibatch['target'], *self.params) 61 self.update_fn(minibatch['input'], minibatch['target'], *self.params)
62 62
63 def update(self, dataset, 63 def update(self, dataset,
64 default_minibatch_size=32): 64 default_minibatch_size=32):
65 """Update this model from more training data.""" 65 """
66 Update this model from more training data.Uses all the data once, cut
67 into minibatches. No early stopper here.
68 """
66 params = self.params 69 params = self.params
67 minibatch_size = min(default_minibatch_size, len(dataset)) 70 minibatch_size = min(default_minibatch_size, len(dataset))
68 for mb in dataset.minibatches(['input', 'target'], minibatch_size=minibatch_size): 71 for mb in dataset.minibatches(['input', 'target'], minibatch_size=minibatch_size):
69 self.update_minibatch(mb) 72 self.update_minibatch(mb)
70 73
121 124
122 def linker(self): 125 def linker(self):
123 return theano.gof.PerformLinker() 126 return theano.gof.PerformLinker()
124 127
125 def early_stopper(self): 128 def early_stopper(self):
126 stopper.NStages(10,1) 129 stopper.NStages(300,1)
127 130
128 def train_iter(self, trainset): 131 def train_iter(self, trainset):
129 raise AbstractFunction 132 raise AbstractFunction
130 optimizer = Opt() 133 optimizer = Opt()
131 134
144 # prefer caching in Model.__call__ 147 # prefer caching in Model.__call__
145 return theano.function(inputs, outputs, 148 return theano.function(inputs, outputs,
146 unpack_single=False, 149 unpack_single=False,
147 optimizer=self.graph.optimizer, 150 optimizer=self.graph.optimizer,
148 linker=self.graph.linker() if hasattr(self.graph, 'linker') 151 linker=self.graph.linker() if hasattr(self.graph, 'linker')
149 else 'c&py') 152 else 'c|py')
150 153
151 def __call__(self, 154 def __call__(self,
152 trainset=None, 155 trainset=None,
153 validset=None, 156 validset=None,
154 iparams=None): 157 iparams=None,
158 stp=None):
155 """Allocate and optionally train a model 159 """Allocate and optionally train a model
156 160
157 @param trainset: Data for minimizing the cost function 161 @param trainset: Data for minimizing the cost function
158 @type trainset: None or Dataset 162 @type trainset: None or Dataset
159 163
163 @param input: name of field to use as input 167 @param input: name of field to use as input
164 @type input: string 168 @type input: string
165 169
166 @param target: name of field to use as target 170 @param target: name of field to use as target
167 @type target: string 171 @type target: string
172
173 @param stp: early stopper, if None use default in graphMLP.G
174 @type stp: None or early stopper
168 175
169 @return: model 176 @return: model
170 @rtype: GraphLearner.Model instance 177 @rtype: GraphLearner.Model instance
171 178
172 """ 179 """
182 curmodel = GraphLearner.Model(self, iparams) 189 curmodel = GraphLearner.Model(self, iparams)
183 best = curmodel 190 best = curmodel
184 191
185 if trainset is not None: 192 if trainset is not None:
186 #do some training by calling Model.update_minibatch() 193 #do some training by calling Model.update_minibatch()
187 stp = self.graph.early_stopper() 194 if stp == None :
188 for mb in self.graph.train_iter(trainset): 195 stp = self.graph.early_stopper()
189 curmodel.update_minibatch(mb) 196 try :
190 if stp.set_score: 197 countiter = 0
191 if validset: 198 for mb in self.graph.train_iter(trainset):
192 stp.score = curmodel(validset, ['validset_score']) 199 curmodel.update_minibatch(mb)
193 if (stp.score < stp.best_score): 200 if stp.set_score:
194 best = copy.copy(curmodel) 201 if validset:
195 else: 202 stp.score = curmodel(validset, ['validset_score'])
196 stp.score = 0.0 203 if (stp.score < stp.best_score):
197 stp.next() 204 best = copy.copy(curmodel)
205 else:
206 stp.score = 0.0
207 countiter +=1
208 stp.next()
209 except StopIteration :
210 print 'Iterations stopped after ', countiter,' iterations'
198 if validset: 211 if validset:
199 curmodel = best 212 curmodel = best
200 return curmodel 213 return curmodel
201 214
202 215
276 , randsmall(nhid, nclass) 289 , randsmall(nhid, nclass)
277 , randsmall(nclass)] 290 , randsmall(nclass)]
278 291
279 def train_iter(self, trainset): 292 def train_iter(self, trainset):
280 return trainset.minibatches(['input', 'target'], 293 return trainset.minibatches(['input', 'target'],
281 minibatch_size=min(len(trainset), 32), n_batches=300) 294 minibatch_size=min(len(trainset), 32), n_batches=2000)
282 def early_stopper(self): 295 def early_stopper(self):
296 """ overwrites GraphLearner.graph function """
283 return stopper.NStages(300,1) 297 return stopper.NStages(300,1)
284 298
285 return G() 299 return G()
286 300
287 301