annotate 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
rev   line source
1200
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
1 """plugin_JB - draft of potential library architecture using iterators
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
2
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
3 This strategy makes use of a simple imperative language whose statements are python function
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
4 calls to create learning algorithms that can be manipulated and executed in several desirable
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
5 ways.
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
6
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
7 The training procedure for a PCA module is easy to express:
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
8
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
9 # allocate the relevant modules
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
10 dataset = Dataset(numpy.random.RandomState(123).randn(13,1))
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
11 pca = PCA_Analysis()
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
12 pca_batchsize=1000
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
13
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
14 # define the control-flow of the algorithm
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
15 train_pca = SEQ([
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
16 BUFFER_REPEAT(pca_batchsize, CALL(dataset.next)),
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
17 FILT(pca.analyze)])
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
18
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
19 # run the program
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
20 train_pca.run()
1200
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
21
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
22 The CALL, SEQ, FILT, and BUFFER_REPEAT are control-flow elements. The control-flow elements I
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
23 defined so far are:
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
24
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
25 - CALL - a basic statement, just calls a python function
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
26 - FILT - like call, but passes the return value of the last CALL or FILT to the python function
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
27 - SEQ - a sequence of elements to run in order
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
28 - REPEAT - do something N times (and return None or maybe the last CALL?)
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
29 - BUFFER_REPEAT - do something N times and accumulate the return value from each iter
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
30 - LOOP - do something an infinite number of times
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
31 - CHOOSE - like a switch statement (should rename to SWITCH)
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
32 - WEAVE - interleave execution of multiple control-flow elements
1211
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
33 - POPEN - launch a process and return its status when it's complete
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
34 - PRINT - a shortcut for CALL(print_obj)
1200
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
35
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
36
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
37 We don't have many requirements per-se for the architecture, but I think this design respects
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
38 and realizes all of them.
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
39 The advantages of this approach are:
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
40
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
41 - algorithms (including partially run ones) are COPYABLE, and SERIALIZABLE
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
42
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
43 - algorithms can be executed without seizing control of the python process (the run()
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
44 method does this, but if you look inside it you'll see it's a simple for loop)
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
45
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
46 - it is easy to execute an algorithm step by step in a main loop that also checks for
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
47 network or filesystem events related to e.g. job management.
1200
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
48
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
49 - the library can provide learning algorithms via control-flow templates, and the user can
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
50 edit them (with search/replace calls) to include HOOKS, and DIAGNOSTIC plug-in
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
51 functionality
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
52
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
53 e.g. prog.find(CALL(cd1_update, layer=layer1)).replace_with(
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
54 SEQ([CALL(cd1_update, layer=layer1), CALL(my_debugfn)]))
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
55
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
56 - user can print the 'program code' of an algorithm built from library pieces
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
57
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
58 - program can be optimized automatically.
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
59
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
60 - e.g. BUFFER(N, CALL(dataset.next)) could be replaced if dataset.next implements the
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
61 right attribute/protocol for 'bufferable' or something.
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
62
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
63 - e.g. SEQ([a,b,c,d]) could be compiled to a single CALL to a Theano-compiled function
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
64 if a, b, c, and d are calls to callable objects that export something like a
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
65 'theano_SEQ' interface
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
66
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
67
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
68 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
69
1200
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
70 __license__ = 'TODO'
acfd5e747a75 v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1199
diff changeset
71 __copyright__ = 'TODO'
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
72
1211
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
73 import cPickle, copy, subprocess, sys, time
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
74 import numpy
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
75
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
76 ####################################################
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
77 # CONTROL-FLOW CONSTRUCTS
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
78
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
79 class INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
80 """Return value for Element.step"""
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
81
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
82 class ELEMENT(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
83 """
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
84 Base class for control flow elements (e.g. CALL, REPEAT, etc.)
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
85
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
86 The design is that every element has a driver, that is another element, or the iterator
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
87 implementation in the ELEMENT class.
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
88
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
89 the driver calls start when entering a new control element
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
90 - this would be called once per e.g. outer loop iteration
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
91
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
92 the driver calls step to advance the control element
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
93 - which returns INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
94 - which returns any other object to indicate completion
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
95 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
96
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
97 # subclasses should override these methods:
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
98 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
99 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
100 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
101 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
102
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
103 # subclasses should typically not override these:
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
104 def run(self, arg=None, n_steps=float('inf')):
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
105 self.start(arg)
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
106 i = 0
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
107 r = self.step()
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
108 while r is INCOMPLETE:
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
109 i += 1
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
110 #TODO make sure there is not an off-by-one error
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
111 if i > n_steps:
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
112 break
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
113 r = self.step()
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
114 return r
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
115
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
116 class BUFFER_REPEAT(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
117 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
118 Accumulate a number of return values into one list / array.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
119
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
120 The source of return values `src` is a control element that will be restarted repeatedly in
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
121 order to fulfil the requiement of gathering N samples.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
122
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
123 TODO: support accumulating of tuples of arrays
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
124 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
125 def __init__(self, N, src, storage=None):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
126 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
127 TODO: use preallocated `storage`
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
128 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
129 self.N = N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
130 self.n = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
131 self.src = src
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
132 self.storage = storage
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
133 self.src.start(None)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
134 if self.storage != None:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
135 raise NotImplementedError()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
136 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
137 self.buf = [None] * self.N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
138 self.n = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
139 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
140 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
141 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
142 r = self.src.step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
143 if r is INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
144 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
145 self.src.start(None) # restart our stream
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
146 self.buf[self.n] = r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
147 self.n += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
148 if self.n == self.N:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
149 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
150 return self.buf
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
151 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
152 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
153 assert 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
154
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
155 class CALL(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
156 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
157 Control flow terminal - call a python function or method.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
158
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
159 Returns the return value of the call.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
160 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
161 def __init__(self, fn, *args, **kwargs):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
162 self.fn = fn
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
163 self.args = args
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
164 self.kwargs=kwargs
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
165 self.use_start_arg = kwargs.pop('use_start_arg', False)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
166 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
167 self.start_arg = arg
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
168 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
169 return self
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
170 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
171 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
172 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
173 if self.use_start_arg:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
174 if self.args:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
175 raise TypeError('cant get positional args both ways')
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
176 return self.fn(self.start_arg, **self.kwargs)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
177 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
178 return self.fn(*self.args, **self.kwargs)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
179 def __getstate__(self):
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
180 rval = dict(self.__dict__)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
181 if type(self.fn) is type(self.step): #instancemethod
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
182 fn = rval.pop('fn')
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
183 rval['i fn'] = fn.im_func, fn.im_self, fn.im_class
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
184 return rval
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
185 def __setstate__(self, dct):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
186 if 'i fn' in dct:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
187 dct['fn'] = type(self.step)(*dct.pop('i fn'))
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
188 self.__dict__.update(dct)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
189
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
190 def FILT(fn, **kwargs):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
191 """
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
192 Return a CALL object that uses the return value from the previous CALL as the first and
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
193 only positional argument.
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
194 """
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
195 return CALL(fn, use_start_arg=True, **kwargs)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
196
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
197 def CHOOSE(which, options):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
198 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
199 Execute one out of a number of optional control flow paths
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
200 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
201 raise NotImplementedError()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
202
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
203 def LOOP(elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
204 #TODO: implement a true infinite loop
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
205 try:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
206 iter(elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
207 return REPEAT(sys.maxint, elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
208 except TypeError:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
209 return REPEAT(sys.maxint, [elements])
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
210
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
211 class REPEAT(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
212 def __init__(self, N, elements, pass_rvals=False):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
213 self.N = N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
214 self.elements = elements
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
215 self.pass_rvals = pass_rvals
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
216
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
217 #TODO: check for N being callable
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
218 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
219 self.n = 0 #loop iteration
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
220 self.idx = 0 #element idx
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
221 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
222 self.elements[0].start(arg)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
223 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
224 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
225 r = self.elements[self.idx].step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
226 if r is INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
227 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
228 self.idx += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
229 if self.idx < len(self.elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
230 self.elements[self.idx].start(r)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
231 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
232 self.n += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
233 if self.n < self.N:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
234 self.idx = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
235 self.elements[self.idx].start(r)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
236 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
237 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
238 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
239 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
240
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
241 def SEQ(elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
242 return REPEAT(1, elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
243
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
244 class WEAVE(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
245 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
246 Interleave execution of a number of elements.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
247
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
248 TODO: allow a schedule (at least relative frequency) of elements from each program
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
249 """
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
250 def __init__(self, n_required, elements):
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
251 self.elements = elements
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
252 if n_required == -1:
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
253 self.n_required = len(elements)
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
254 else:
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
255 self.n_required = n_required
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
256 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
257 for el in self.elements:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
258 el.start(arg)
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
259 self.elem_finished = [0] * len(self.elements)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
260 self.idx = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
261 self.finished= False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
262 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
263 assert not self.finished # if this is triggered, we have a broken driver
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
264
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
265 #start with this check in case there were no elements
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
266 # it's possible for the number of finished elements to exceed the threshold
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
267 if sum(self.elem_finished) >= self.n_required:
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
268 self.finished = True
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
269 return None
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
270
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
271 # step the active element
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
272 r = self.elements[self.idx].step()
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
273
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
274 if r is not INCOMPLETE:
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
275 self.elem_finished[self.idx] = True
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
276
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
277 # check for completion
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
278 if sum(self.elem_finished) >= self.n_required:
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
279 self.finished = True
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
280 return None
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
281
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
282 # advance to the next un-finished element
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
283 self.idx = (self.idx+1) % len(self.elements)
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
284 while self.elem_finished[self.idx]:
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
285 self.idx = (self.idx+1) % len(self.elements)
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
286
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
287 return INCOMPLETE
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
288
1211
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
289 class POPEN(ELEMENT):
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
290 def __init__(self, args):
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
291 self.args = args
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
292 def start(self, arg):
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
293 self.p = subprocess.Popen(self.args)
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
294 def step(self):
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
295 r = self.p.poll()
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
296 if r is None:
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
297 return INCOMPLETE
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
298 return r
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
299
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
300 def PRINT(obj):
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
301 return CALL(print_obj, obj)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
302
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
303 ####################################################
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
304 # [Dummy] Components involved in learning algorithms
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
305
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
306 class Dataset(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
307 def __init__(self, data):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
308 self.pos = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
309 self.data = data
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
310 def next(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
311 rval = self.data[self.pos]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
312 self.pos += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
313 if self.pos == len(self.data):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
314 self.pos = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
315 return rval
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
316 def seek(self, pos):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
317 self.pos = pos
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
318
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
319 class KFold(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
320 def __init__(self, data, K):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
321 self.data = data
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
322 self.k = -1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
323 self.scores = [None]*K
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
324 self.K = K
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
325 def next_fold(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
326 self.k += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
327 self.data.seek(0) # restart the stream
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
328 def next(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
329 #TODO: skip the examples that are ommitted in this split
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
330 return self.data.next()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
331 def init_test(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
332 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
333 def next_test(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
334 return self.data.next()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
335 def test_size(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
336 return 5
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
337 def store_scores(self, scores):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
338 self.scores[self.k] = scores
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
339
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
340 def prog(self, clear, train, test):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
341 return REPEAT(self.K, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
342 CALL(self.next_fold),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
343 clear,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
344 train,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
345 CALL(self.init_test),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
346 BUFFER_REPEAT(self.test_size(),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
347 SEQ([ CALL(self.next_test), test])),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
348 FILT(self.store_scores) ])
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
349
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
350 class PCA_Analysis(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
351 def __init__(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
352 self.clear()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
353
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
354 def clear(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
355 self.mean = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
356 self.eigvecs=0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
357 self.eigvals=0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
358 def analyze(self, X):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
359 self.mean = numpy.mean(X, axis=0)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
360 self.eigvecs=1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
361 self.eigvals=1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
362 def filt(self, X):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
363 return (X - self.mean) * self.eigvecs #TODO: divide by root eigvals?
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
364 def pseudo_inverse(self, Y):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
365 return Y
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
366
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
367 class Layer(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
368 def __init__(self, w):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
369 self.w = w
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
370 def filt(self, x):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
371 return self.w*x
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
372 def clear(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
373 self.w =0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
374
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
375 def print_obj(obj):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
376 print obj
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
377 def print_obj_attr(obj, attr):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
378 print getattr(obj, attr)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
379 def no_op(*args, **kwargs):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
380 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
381
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
382 def cd1_update(X, layer, lr):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
383 # update self.layer from observation X
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
384 layer.w += X.mean() * lr #TODO: not exactly correct math!
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
385
1211
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
386
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
387 ###############################################################
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
388 # Example algorithms written in this control flow mini-language
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
389
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
390 def main_weave():
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
391 # Uses weave to demonstrate the interleaving of two bufferings of a single stream
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
392
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
393 l = [0]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
394 def f(a):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
395 print l
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
396 l[0] += a
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
397 return l[0]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
398
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
399 print WEAVE(1, [
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
400 BUFFER_REPEAT(3,CALL(f,1)),
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
401 BUFFER_REPEAT(5,CALL(f,1)),
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
402 ]).run()
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
403
1211
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
404 def main_weave_popen():
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
405 # Uses weave and Popen to demonstrate the control of a program with some asynchronous
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
406 # parallelism
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
407
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
408 p = WEAVE(2,[
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
409 SEQ([POPEN(['sleep', '5']), PRINT('done 1')]),
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
410 SEQ([POPEN(['sleep', '10']), PRINT('done 2')]),
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
411 LOOP([
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
412 CALL(print_obj, 'polling...'),
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
413 CALL(time.sleep, 1)])])
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
414 # The LOOP would forever if the WEAVE were not configured to stop after 2 of its elements
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
415 # complete.
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
416
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
417 p.run()
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
418 # Note that the program can be run multiple times...
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
419 p.run()
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
420
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
421 main = main_weave_popen
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
422 def main_kfold_dbn():
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
423 # Uses many of the control-flow elements to define the k-fold evaluation of a dbn
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
424 # The algorithm is not quite right, but the example shows off all of the required
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
425 # control-flow elements I think.
e7ac87720fee v2planning plugin_JB - added PRINT and POPEN to demonstrate parallel async. control flows
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1210
diff changeset
426
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
427 # create components
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
428 dataset = Dataset(numpy.random.RandomState(123).randn(13,1))
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
429 pca = PCA_Analysis()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
430 layer1 = Layer(w=4)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
431 layer2 = Layer(w=3)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
432 kf = KFold(dataset, K=10)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
433
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
434 pca_batchsize=1000
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
435 cd_batchsize = 5
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
436 n_cd_updates_layer1 = 10
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
437 n_cd_updates_layer2 = 10
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
438
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
439 # create algorithm
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
440
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
441 train_pca = SEQ([
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
442 BUFFER_REPEAT(pca_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
443 FILT(pca.analyze)])
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
444
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
445 train_layer1 = REPEAT(n_cd_updates_layer1, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
446 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
447 FILT(pca.filt),
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
448 FILT(cd1_update, layer=layer1, lr=.01)])
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
449
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
450 train_layer2 = REPEAT(n_cd_updates_layer2, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
451 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
452 FILT(pca.filt),
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
453 FILT(layer1.filt),
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
454 FILT(cd1_update, layer=layer2, lr=.01)])
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
455
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
456 kfold_prog = kf.prog(
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
457 clear = SEQ([ # FRAGMENT 1: this bit is the reset/clear stage
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
458 CALL(pca.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
459 CALL(layer1.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
460 CALL(layer2.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
461 ]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
462 train = SEQ([
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
463 train_pca,
1210
cbe1fb32686c v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1203
diff changeset
464 WEAVE(1, [ # Silly example of how to do debugging / loggin with WEAVE
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
465 train_layer1,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
466 LOOP(CALL(print_obj_attr, layer1, 'w'))]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
467 train_layer2,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
468 ]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
469 test=SEQ([
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
470 FILT(pca.filt), # may want to allow this SEQ to be
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
471 FILT(layer1.filt), # optimized into a shorter one that
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
472 FILT(layer2.filt), # compiles these calls together with
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
473 FILT(numpy.mean)])) # Theano
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
474
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
475 pkg1 = dict(prog=kfold_prog, kf=kf)
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
476 pkg2 = copy.deepcopy(pkg1) # programs can be copied
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
477
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
478 try:
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
479 pkg3 = cPickle.loads(cPickle.dumps(pkg1))
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
480 except:
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
481 print >> sys.stderr, "pickling doesnt work, but it can be fixed I think"
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
482
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
483 pkg = pkg2
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
484
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
485 # running a program updates the variables in its package, but not the other package
1203
865936d8221b v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1200
diff changeset
486 pkg['prog'].run()
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
487 print pkg['kf'].scores
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
488
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
489
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
490 if __name__ == '__main__':
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
491 sys.exit(main())
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
492