Mercurial > pylearn
comparison doc/v2_planning/plugin_JB.py @ 1210:cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Tue, 21 Sep 2010 23:38:53 -0400 |
parents | 865936d8221b |
children | e7ac87720fee |
comparison
equal
deleted
inserted
replaced
1209:5ff1d375fc33 | 1210:cbe1fb32686c |
---|---|
244 """ | 244 """ |
245 Interleave execution of a number of elements. | 245 Interleave execution of a number of elements. |
246 | 246 |
247 TODO: allow a schedule (at least relative frequency) of elements from each program | 247 TODO: allow a schedule (at least relative frequency) of elements from each program |
248 """ | 248 """ |
249 def __init__(self, elements): | 249 def __init__(self, n_required, elements): |
250 self.elements = elements | 250 self.elements = elements |
251 if n_required == -1: | |
252 self.n_required = len(elements) | |
253 else: | |
254 self.n_required = n_required | |
251 def start(self, arg): | 255 def start(self, arg): |
252 for el in self.elements: | 256 for el in self.elements: |
253 el.start(arg) | 257 el.start(arg) |
258 self.elem_finished = [0] * len(self.elements) | |
254 self.idx = 0 | 259 self.idx = 0 |
255 self.any_is_finished = False | |
256 self.finished= False | 260 self.finished= False |
257 def step(self): | 261 def step(self): |
258 assert not self.finished # if this is triggered, we have a broken driver | 262 assert not self.finished # if this is triggered, we have a broken driver |
259 self.idx = self.idx % len(self.elements) | 263 |
264 #start with this check in case there were no elements | |
265 # it's possible for the number of finished elements to exceed the threshold | |
266 if sum(self.elem_finished) >= self.n_required: | |
267 self.finished = True | |
268 return None | |
269 | |
270 # step the active element | |
260 r = self.elements[self.idx].step() | 271 r = self.elements[self.idx].step() |
272 | |
261 if r is not INCOMPLETE: | 273 if r is not INCOMPLETE: |
262 self.any_is_finished = True | 274 self.elem_finished[self.idx] = True |
263 self.idx += 1 | 275 |
264 if self.idx == len(self.elements) and self.any_is_finished: | 276 # check for completion |
265 self.finished = True | 277 if sum(self.elem_finished) >= self.n_required: |
266 return None # dummy completion value | 278 self.finished = True |
267 else: | 279 return None |
268 return INCOMPLETE | 280 |
281 # advance to the next un-finished element | |
282 self.idx = (self.idx+1) % len(self.elements) | |
283 while self.elem_finished[self.idx]: | |
284 self.idx = (self.idx+1) % len(self.elements) | |
285 | |
286 return INCOMPLETE | |
269 | 287 |
270 | 288 |
271 #################################################### | 289 #################################################### |
272 # [Dummy] Components involved in learning algorithms | 290 # [Dummy] Components involved in learning algorithms |
273 | 291 |
357 def f(a): | 375 def f(a): |
358 print l | 376 print l |
359 l[0] += a | 377 l[0] += a |
360 return l[0] | 378 return l[0] |
361 | 379 |
362 print WEAVE([ | 380 print WEAVE(1, [ |
363 BUFFER_REPEAT(3,CALL(f,1)), | 381 BUFFER_REPEAT(3,CALL(f,1)), |
364 BUFFER_REPEAT(5,CALL(f,1)), | 382 BUFFER_REPEAT(5,CALL(f,1)), |
365 ]).run() | 383 ]).run() |
366 | 384 |
367 def main(): | 385 def main(): |
400 CALL(layer1.clear), | 418 CALL(layer1.clear), |
401 CALL(layer2.clear), | 419 CALL(layer2.clear), |
402 ]), | 420 ]), |
403 train = SEQ([ | 421 train = SEQ([ |
404 train_pca, | 422 train_pca, |
405 WEAVE([ # Silly example of how to do debugging / loggin with WEAVE | 423 WEAVE(1, [ # Silly example of how to do debugging / loggin with WEAVE |
406 train_layer1, | 424 train_layer1, |
407 LOOP(CALL(print_obj_attr, layer1, 'w'))]), | 425 LOOP(CALL(print_obj_attr, layer1, 'w'))]), |
408 train_layer2, | 426 train_layer2, |
409 ]), | 427 ]), |
410 test=SEQ([ | 428 test=SEQ([ |