Mercurial > pylearn
annotate doc/v2_planning/plugin_JB.py @ 1203:865936d8221b
v2planning plugin_JB - removed VM for clarity
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Mon, 20 Sep 2010 21:33:45 -0400 |
parents | acfd5e747a75 |
children | cbe1fb32686c |
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 """ |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
249 def __init__(self, elements): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
250 self.elements = elements |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
251 def start(self, arg): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
252 for el in self.elements: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
253 el.start(arg) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
254 self.idx = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
255 self.any_is_finished = False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
256 self.finished= False |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
257 def step(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
258 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
|
259 self.idx = self.idx % len(self.elements) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
260 r = self.elements[self.idx].step() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
261 if r is not INCOMPLETE: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
262 self.any_is_finished = True |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
263 self.idx += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
264 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
|
265 self.finished = True |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
266 return None # dummy completion value |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
267 else: |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
268 return INCOMPLETE |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
269 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
270 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
271 #################################################### |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
272 # [Dummy] Components involved in learning algorithms |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
273 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
274 class Dataset(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
275 def __init__(self, data): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
276 self.pos = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
277 self.data = data |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
278 def next(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
279 rval = self.data[self.pos] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
280 self.pos += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
281 if self.pos == len(self.data): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
282 self.pos = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
283 return rval |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
284 def seek(self, pos): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
285 self.pos = pos |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
286 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
287 class KFold(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
288 def __init__(self, data, K): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
289 self.data = data |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
290 self.k = -1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
291 self.scores = [None]*K |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
292 self.K = K |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
293 def next_fold(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
294 self.k += 1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
295 self.data.seek(0) # restart the stream |
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 #TODO: skip the examples that are ommitted in this split |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
298 return self.data.next() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
299 def init_test(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
300 pass |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
301 def next_test(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
302 return self.data.next() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
303 def test_size(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
304 return 5 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
305 def store_scores(self, scores): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
306 self.scores[self.k] = scores |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
307 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
308 def prog(self, clear, train, test): |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
309 return REPEAT(self.K, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
310 CALL(self.next_fold), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
311 clear, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
312 train, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
313 CALL(self.init_test), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
314 BUFFER_REPEAT(self.test_size(), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
315 SEQ([ CALL(self.next_test), test])), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
316 FILT(self.store_scores) ]) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
317 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
318 class PCA_Analysis(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
319 def __init__(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
320 self.clear() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
321 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
322 def clear(self): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
323 self.mean = 0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
324 self.eigvecs=0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
325 self.eigvals=0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
326 def analyze(self, X): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
327 self.mean = numpy.mean(X, axis=0) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
328 self.eigvecs=1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
329 self.eigvals=1 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
330 def filt(self, X): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
331 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
|
332 def pseudo_inverse(self, Y): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
333 return Y |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
334 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
335 class Layer(object): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
336 def __init__(self, w): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
337 self.w = w |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
338 def filt(self, x): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
339 return self.w*x |
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.w =0 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
342 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
343 def print_obj(obj): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
344 print obj |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
345 def print_obj_attr(obj, attr): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
346 print getattr(obj, attr) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
347 def no_op(*args, **kwargs): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
348 pass |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
349 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
350 def cd1_update(X, layer, lr): |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
351 # update self.layer from observation X |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
352 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
|
353 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
354 def simple_main(): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
355 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
356 l = [0] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
357 def f(a): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
358 print l |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
359 l[0] += a |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
360 return l[0] |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
361 |
1203
865936d8221b
v2planning plugin_JB - removed VM for clarity
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1200
diff
changeset
|
362 print WEAVE([ |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
363 BUFFER_REPEAT(3,CALL(f,1)), |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
364 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
|
365 ]).run() |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
366 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
367 def main(): |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
368 # create components |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
369 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
370 pca = PCA_Analysis() |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
371 layer1 = Layer(w=4) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
372 layer2 = Layer(w=3) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
373 kf = KFold(dataset, K=10) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
374 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
375 pca_batchsize=1000 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
376 cd_batchsize = 5 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
377 n_cd_updates_layer1 = 10 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
378 n_cd_updates_layer2 = 10 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
379 |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
380 # create algorithm |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
381 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
382 train_pca = SEQ([ |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
383 BUFFER_REPEAT(pca_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
384 FILT(pca.analyze)]) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
385 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
386 train_layer1 = REPEAT(n_cd_updates_layer1, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
387 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
388 FILT(pca.filt), |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
389 FILT(cd1_update, layer=layer1, lr=.01)]) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
390 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
391 train_layer2 = REPEAT(n_cd_updates_layer2, [ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
392 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)), |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
393 FILT(pca.filt), |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
394 FILT(layer1.filt), |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
395 FILT(cd1_update, layer=layer2, lr=.01)]) |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
396 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
397 kfold_prog = kf.prog( |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
398 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
|
399 CALL(pca.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
400 CALL(layer1.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
401 CALL(layer2.clear), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
402 ]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
403 train = SEQ([ |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
404 train_pca, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
405 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
|
406 train_layer1, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
407 LOOP(CALL(print_obj_attr, layer1, 'w'))]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
408 train_layer2, |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
409 ]), |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
410 test=SEQ([ |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
411 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
|
412 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
|
413 FILT(layer2.filt), # compiles these calls together with |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
414 FILT(numpy.mean)])) # Theano |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
415 |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
416 pkg1 = dict(prog=kfold_prog, kf=kf) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
417 pkg2 = copy.deepcopy(pkg1) # programs can be copied |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
418 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
419 try: |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
420 pkg3 = cPickle.loads(cPickle.dumps(pkg1)) |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
421 except: |
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
422 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
|
423 |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
424 pkg = pkg2 |
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 # 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
|
427 pkg['prog'].run() |
1199
98954d8cb92d
v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1198
diff
changeset
|
428 print pkg['kf'].scores |
1198
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
429 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
430 |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
431 if __name__ == '__main__': |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
432 sys.exit(main()) |
1387771296a8
v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
433 |