Mercurial > pylearn
annotate 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 |
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 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
33 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
34 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
35 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
|
36 and realizes all of them. |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
37 The advantages of this approach are: |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
38 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
39 - 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
|
40 |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
41 - 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
|
42 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
|
43 |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
44 - 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
|
45 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
|
46 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
47 - 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
|
48 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
|
49 functionality |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
50 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
51 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
|
52 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
|
53 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
54 - 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
|
55 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
56 - program can be optimized automatically. |
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 - 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
|
59 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
|
60 |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
61 - 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
|
62 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
|
63 'theano_SEQ' interface |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 |
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 |
1200
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
68 __license__ = 'TODO' |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1199
diff
changeset
|
69 __copyright__ = 'TODO' |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
70 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
71 import copy, sys, cPickle |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 import numpy |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
73 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 #################################################### |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 # CONTROL-FLOW CONSTRUCTS |
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 class INCOMPLETE: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
78 """Return value for Element.step""" |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
79 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 class ELEMENT(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
81 """ |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
82 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
|
83 |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
84 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
|
85 implementation in the ELEMENT class. |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
87 the driver calls start when entering a new control element |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
88 - 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
|
89 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
90 the driver calls step to advance the control element |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
91 - which returns INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
92 - which returns any other object to indicate completion |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
93 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
94 |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
95 # subclasses should override these methods: |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
96 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
97 pass |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
98 def step(self): |
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 |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
101 # subclasses should typically not override these: |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
102 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
|
103 self.start(arg) |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
104 i = 0 |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
105 r = self.step() |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
106 while r is INCOMPLETE: |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
107 i += 1 |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
108 #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
|
109 if i > n_steps: |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
110 break |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
111 r = self.step() |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
112 return r |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
113 |
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
114 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
115 class BUFFER_REPEAT(ELEMENT): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
116 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
117 Accumulate a number of return values into one list / array. |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
118 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
119 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
|
120 order to fulfil the requiement of gathering N samples. |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
121 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
122 TODO: support accumulating of tuples of arrays |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
123 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
124 def __init__(self, N, src, storage=None): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
125 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
126 TODO: use preallocated `storage` |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
127 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
128 self.N = N |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
129 self.n = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
130 self.src = src |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
131 self.storage = storage |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
132 self.src.start(None) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
133 if self.storage != None: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
134 raise NotImplementedError() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
135 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
136 self.buf = [None] * self.N |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
137 self.n = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
138 self.finished = False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
139 def step(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
140 assert not self.finished |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
141 r = self.src.step() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
142 if r is INCOMPLETE: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
143 return r |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
144 self.src.start(None) # restart our stream |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
145 self.buf[self.n] = r |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
146 self.n += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
147 if self.n == self.N: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
148 self.finished = True |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
149 return self.buf |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
150 else: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
151 return INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
152 assert 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
153 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
154 class CALL(ELEMENT): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
155 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
156 Control flow terminal - call a python function or method. |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
157 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
158 Returns the return value of the call. |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
159 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
160 def __init__(self, fn, *args, **kwargs): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
161 self.fn = fn |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
162 self.args = args |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
163 self.kwargs=kwargs |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
164 self.use_start_arg = kwargs.pop('use_start_arg', False) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
165 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
166 self.start_arg = arg |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
167 self.finished = False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
168 return self |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
169 def step(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
170 assert not self.finished |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
171 self.finished = True |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
172 if self.use_start_arg: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
173 if self.args: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
174 raise TypeError('cant get positional args both ways') |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
175 return self.fn(self.start_arg, **self.kwargs) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
176 else: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
177 return self.fn(*self.args, **self.kwargs) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
178 def __getstate__(self): |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
179 rval = dict(self.__dict__) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
180 if type(self.fn) is type(self.step): #instancemethod |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
181 fn = rval.pop('fn') |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
182 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
|
183 return rval |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
184 def __setstate__(self, dct): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
185 if 'i fn' in dct: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
186 dct['fn'] = type(self.step)(*dct.pop('i fn')) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
187 self.__dict__.update(dct) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
188 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
189 def FILT(fn, **kwargs): |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
190 """ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
191 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
|
192 only positional argument. |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
193 """ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
194 return CALL(fn, use_start_arg=True, **kwargs) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
195 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
196 def CHOOSE(which, options): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
197 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
198 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
|
199 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
200 raise NotImplementedError() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
201 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
202 def LOOP(elements): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
203 #TODO: implement a true infinite loop |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
204 try: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
205 iter(elements) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
206 return REPEAT(sys.maxint, elements) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
207 except TypeError: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
208 return REPEAT(sys.maxint, [elements]) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
209 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
210 class REPEAT(ELEMENT): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
211 def __init__(self, N, elements, pass_rvals=False): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
212 self.N = N |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
213 self.elements = elements |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
214 self.pass_rvals = pass_rvals |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
215 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
216 #TODO: check for N being callable |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
217 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
218 self.n = 0 #loop iteration |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
219 self.idx = 0 #element idx |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
220 self.finished = False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
221 self.elements[0].start(arg) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
222 def step(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
223 assert not self.finished |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
224 r = self.elements[self.idx].step() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
225 if r is INCOMPLETE: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
226 return INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
227 self.idx += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
228 if self.idx < len(self.elements): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
229 self.elements[self.idx].start(r) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
230 return INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
231 self.n += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
232 if self.n < self.N: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
233 self.idx = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
234 self.elements[self.idx].start(r) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
235 return INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
236 else: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
237 self.finished = True |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
238 return r |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
239 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
240 def SEQ(elements): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
241 return REPEAT(1, elements) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
242 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
243 class WEAVE(ELEMENT): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
244 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
245 Interleave execution of a number of elements. |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
246 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
247 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
|
248 """ |
1210
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
249 def __init__(self, n_required, elements): |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
250 self.elements = elements |
1210
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
251 if n_required == -1: |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
252 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
|
253 else: |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
254 self.n_required = n_required |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
255 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
256 for el in self.elements: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
257 el.start(arg) |
1210
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
258 self.elem_finished = [0] * len(self.elements) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
259 self.idx = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
260 self.finished= False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
261 def step(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
262 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
|
263 |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
264 #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
|
265 # 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
|
266 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
|
267 self.finished = True |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
268 return None |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
269 |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
270 # step the active element |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
271 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
|
272 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
273 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
|
274 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
|
275 |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
276 # check for completion |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
277 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
|
278 self.finished = True |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
279 return None |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
280 |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
281 # 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
|
282 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
|
283 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
|
284 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
|
285 |
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
286 return INCOMPLETE |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
287 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
288 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
289 #################################################### |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
290 # [Dummy] Components involved in learning algorithms |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
291 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
292 class Dataset(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
293 def __init__(self, data): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
294 self.pos = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
295 self.data = data |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
296 def next(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
297 rval = self.data[self.pos] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
298 self.pos += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
299 if self.pos == len(self.data): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
300 self.pos = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
301 return rval |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
302 def seek(self, pos): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
303 self.pos = pos |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
304 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
305 class KFold(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
306 def __init__(self, data, K): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
307 self.data = data |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
308 self.k = -1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
309 self.scores = [None]*K |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
310 self.K = K |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
311 def next_fold(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
312 self.k += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
313 self.data.seek(0) # restart the stream |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
314 def next(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
315 #TODO: skip the examples that are ommitted in this split |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
316 return self.data.next() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
317 def init_test(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
318 pass |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
319 def next_test(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
320 return self.data.next() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
321 def test_size(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
322 return 5 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
323 def store_scores(self, scores): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
324 self.scores[self.k] = scores |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
325 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
326 def prog(self, clear, train, test): |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
327 return REPEAT(self.K, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
328 CALL(self.next_fold), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
329 clear, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
330 train, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
331 CALL(self.init_test), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
332 BUFFER_REPEAT(self.test_size(), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
333 SEQ([ CALL(self.next_test), test])), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
334 FILT(self.store_scores) ]) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
335 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
336 class PCA_Analysis(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
337 def __init__(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
338 self.clear() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
339 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
340 def clear(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
341 self.mean = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
342 self.eigvecs=0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
343 self.eigvals=0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
344 def analyze(self, X): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
345 self.mean = numpy.mean(X, axis=0) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
346 self.eigvecs=1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
347 self.eigvals=1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
348 def filt(self, X): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
349 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
|
350 def pseudo_inverse(self, Y): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
351 return Y |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
352 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
353 class Layer(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
354 def __init__(self, w): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
355 self.w = w |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
356 def filt(self, x): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
357 return self.w*x |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
358 def clear(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
359 self.w =0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
360 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
361 def print_obj(obj): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
362 print obj |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
363 def print_obj_attr(obj, attr): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
364 print getattr(obj, attr) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
365 def no_op(*args, **kwargs): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
366 pass |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
367 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
368 def cd1_update(X, layer, lr): |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
369 # update self.layer from observation X |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
370 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
|
371 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
372 def simple_main(): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
373 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
374 l = [0] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
375 def f(a): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
376 print l |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
377 l[0] += a |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
378 return l[0] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
379 |
1210
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
380 print WEAVE(1, [ |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
381 BUFFER_REPEAT(3,CALL(f,1)), |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
382 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
|
383 ]).run() |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
384 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
385 def main(): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
386 # create components |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
387 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
388 pca = PCA_Analysis() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
389 layer1 = Layer(w=4) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
390 layer2 = Layer(w=3) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
391 kf = KFold(dataset, K=10) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
392 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
393 pca_batchsize=1000 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
394 cd_batchsize = 5 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
395 n_cd_updates_layer1 = 10 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
396 n_cd_updates_layer2 = 10 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
397 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
398 # create algorithm |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
399 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
400 train_pca = SEQ([ |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
401 BUFFER_REPEAT(pca_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
402 FILT(pca.analyze)]) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
403 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
404 train_layer1 = REPEAT(n_cd_updates_layer1, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
405 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
406 FILT(pca.filt), |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
407 FILT(cd1_update, layer=layer1, lr=.01)]) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
408 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
409 train_layer2 = REPEAT(n_cd_updates_layer2, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
410 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
411 FILT(pca.filt), |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
412 FILT(layer1.filt), |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
413 FILT(cd1_update, layer=layer2, lr=.01)]) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
414 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
415 kfold_prog = kf.prog( |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
416 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
|
417 CALL(pca.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
418 CALL(layer1.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
419 CALL(layer2.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
420 ]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
421 train = SEQ([ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
422 train_pca, |
1210
cbe1fb32686c
v2planning plugin_JB - added n_required keyword to WEAVE
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1203
diff
changeset
|
423 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
|
424 train_layer1, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
425 LOOP(CALL(print_obj_attr, layer1, 'w'))]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
426 train_layer2, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
427 ]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
428 test=SEQ([ |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
429 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
|
430 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
|
431 FILT(layer2.filt), # compiles these calls together with |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
432 FILT(numpy.mean)])) # Theano |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
433 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
434 pkg1 = dict(prog=kfold_prog, kf=kf) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
435 pkg2 = copy.deepcopy(pkg1) # programs can be copied |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
436 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
437 try: |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
438 pkg3 = cPickle.loads(cPickle.dumps(pkg1)) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
439 except: |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
440 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
|
441 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
442 pkg = pkg2 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
443 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
444 # 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
|
445 pkg['prog'].run() |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
446 print pkg['kf'].scores |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
447 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
448 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
449 if __name__ == '__main__': |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
450 sys.exit(main()) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
451 |