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