Mercurial > pylearn
diff doc/v2_planning/plugin_JB.py @ 1211:e7ac87720fee
v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 22 Sep 2010 00:23:07 -0400 |
parents | cbe1fb32686c |
children | 478bb1f8215c |
line wrap: on
line diff
--- a/doc/v2_planning/plugin_JB.py Tue Sep 21 23:38:53 2010 -0400 +++ b/doc/v2_planning/plugin_JB.py Wed Sep 22 00:23:07 2010 -0400 @@ -30,6 +30,8 @@ - LOOP - do something an infinite number of times - CHOOSE - like a switch statement (should rename to SWITCH) - WEAVE - interleave execution of multiple control-flow elements +- POPEN - launch a process and return its status when it's complete +- PRINT - a shortcut for CALL(print_obj) We don't have many requirements per-se for the architecture, but I think this design respects @@ -68,7 +70,7 @@ __license__ = 'TODO' __copyright__ = 'TODO' -import copy, sys, cPickle +import cPickle, copy, subprocess, sys, time import numpy #################################################### @@ -111,7 +113,6 @@ r = self.step() return r - class BUFFER_REPEAT(ELEMENT): """ Accumulate a number of return values into one list / array. @@ -285,6 +286,19 @@ return INCOMPLETE +class POPEN(ELEMENT): + def __init__(self, args): + self.args = args + def start(self, arg): + self.p = subprocess.Popen(self.args) + def step(self): + r = self.p.poll() + if r is None: + return INCOMPLETE + return r + +def PRINT(obj): + return CALL(print_obj, obj) #################################################### # [Dummy] Components involved in learning algorithms @@ -369,7 +383,12 @@ # update self.layer from observation X layer.w += X.mean() * lr #TODO: not exactly correct math! -def simple_main(): + +############################################################### +# Example algorithms written in this control flow mini-language + +def main_weave(): + # Uses weave to demonstrate the interleaving of two bufferings of a single stream l = [0] def f(a): @@ -382,7 +401,29 @@ BUFFER_REPEAT(5,CALL(f,1)), ]).run() -def main(): +def main_weave_popen(): + # Uses weave and Popen to demonstrate the control of a program with some asynchronous + # parallelism + + p = WEAVE(2,[ + SEQ([POPEN(['sleep', '5']), PRINT('done 1')]), + SEQ([POPEN(['sleep', '10']), PRINT('done 2')]), + LOOP([ + CALL(print_obj, 'polling...'), + CALL(time.sleep, 1)])]) + # The LOOP would forever if the WEAVE were not configured to stop after 2 of its elements + # complete. + + p.run() + # Note that the program can be run multiple times... + p.run() + +main = main_weave_popen +def main_kfold_dbn(): + # Uses many of the control-flow elements to define the k-fold evaluation of a dbn + # The algorithm is not quite right, but the example shows off all of the required + # control-flow elements I think. + # create components dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) pca = PCA_Analysis()