Mercurial > pylearn
annotate doc/v2_planning/arch_src/plugin_JB_main.py @ 1274:9d5905d6d879
hmc - changed updates to member fn from lambda for pickling
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Wed, 08 Sep 2010 13:17:45 -0400 |
parents | 9fac28d80fb7 |
children |
rev | line source |
---|---|
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
1 """plugin_JB_main - main functions illustrating control flow library""" |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
2 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
3 from plugin_JB import * #TODO: don't do this |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
4 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
5 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
6 #################################################### |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
7 # [Dummy] Components involved in learning algorithms |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
8 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
9 class Dataset(object): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
10 def __init__(self, data): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
11 self.pos = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
12 self.data = data |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
13 def next(self, n=1): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
14 rval = self.data[self.pos:self.pos+n] |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
15 self.pos += n |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
16 if self.pos >= len(self.data): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 self.pos = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
18 return rval |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
19 def seek(self, pos): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
20 self.pos = pos |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
21 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
22 class KFold(object): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
23 def __init__(self, data, K): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
24 self.data = data |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
25 self.k = -1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
26 self.scores = [None]*K |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
27 self.K = K |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
28 def next_fold(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
29 self.k += 1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
30 self.data.seek(0) # restart the stream |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
31 def next(self, n=1): |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
32 #TODO: skip the examples that are ommitted in this split |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
33 return self.data.next(n) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
34 def init_test(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
35 pass |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
36 def next_test(self, n=1): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
37 return self.data.next(n) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 def test_size(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 return 5 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
40 def store_scores(self, scores): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
41 self.scores[self.k] = scores |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
42 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
43 def prog(self, clear, train, test, test_data_reg, test_counter_reg, test_scores_reg): |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
44 return REPEAT(self.K, SEQ([ |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
45 CALL(self.next_fold), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
46 clear, |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
47 train, |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
48 CALL(self.init_test), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
49 REPEAT(self.test_size(), SEQ([ |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
50 CALL(self.next_test, _set=test_data_reg), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
51 test]), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
52 counter=test_counter_reg), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
53 CALL(self.store_scores, test_scores_reg)])) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
54 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
55 class PCA_Analysis(object): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
56 def __init__(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
57 self.clear() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
58 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
59 def clear(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
60 self.mean = 0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
61 self.eigvecs=0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
62 self.eigvals=0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
63 def analyze(self, X): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 self.mean = numpy.mean(X, axis=0) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 self.eigvecs=1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
66 self.eigvals=1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
67 def filt(self, X): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
68 return (X - self.mean) * self.eigvecs #TODO: divide by root eigvals? |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
69 def pseudo_inverse(self, Y): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
70 return Y |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
71 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 class Layer(object): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
73 def __init__(self, w): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 self.w = w |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 def filt(self, x): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
76 return self.w*x |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
77 def clear(self): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
78 self.w =0 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
79 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 def cd1_update(X, layer, lr): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
81 # update self.layer from observation X |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
82 layer.w += X.mean() * lr #TODO: not exactly correct math! |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
83 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
84 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
85 ############################################################### |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 # Example algorithms written in this control flow mini-language |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
87 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
88 def main_weave(): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
89 # Uses weave to demonstrate the interleaving of two bufferings of a single stream |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
90 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
91 l = [0] |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
92 def f(a): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
93 print l |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
94 l[0] += a |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
95 return l[0] |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
96 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
97 print WEAVE(1, [ |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
98 REPEAT(3,CALL(f,1)), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
99 REPEAT(5,CALL(f,1)), |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
100 ]).run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
101 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
102 def main_weave_popen(): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
103 # Uses weave and Popen to demonstrate the control of a program with some asynchronous |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
104 # parallelism |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
105 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
106 p = WEAVE(2,[ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
107 SEQ([POPEN(['sleep', '5']), PRINT('done 1')]), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
108 SEQ([POPEN(['sleep', '10']), PRINT('done 2')]), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
109 LOOP(SEQ([ |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
110 CALL(print_obj, 'polling...'), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
111 CALL(time.sleep, 1)]))]) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
112 # The LOOP would forever if the WEAVE were not configured to stop after 2 of its elements |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
113 # complete. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
114 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
115 p.run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
116 # Note that the program can be run multiple times... |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
117 p.run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
118 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
119 def main_spawn(): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
120 # illustate the use of SPAWN to drive a set of control programs |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
121 # in other processes |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
122 data1 = {0:"blah data1"} |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
123 data2 = {1:"foo data2"} |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
124 p = WEAVE(2,[ |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
125 SPAWN(data1, REPEAT(3, SEQ([ |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
126 CALL(importable_fn, data1), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
127 PRINT("hello from 1")]))), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
128 SPAWN(data2, REPEAT(1, SEQ([ |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
129 CALL(importable_fn, data2), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
130 PRINT("hello from 2")]))), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
131 LOOP(SEQ([ |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
132 CALL(print_obj, 'polling...'), |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
133 CALL(time.sleep, 0.5)]))]) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
134 print 'BEFORE' |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
135 print data1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
136 print data2 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
137 p.run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
138 print 'AFTER' |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
139 print data1 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
140 print data2 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
141 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
142 def main_kfold_dbn(): |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
143 # Uses many of the control-flow elements to define the k-fold evaluation of a dbn |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
144 # The algorithm is not quite right, but the example shows off all of the required |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
145 # control-flow elements I think. |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
146 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
147 # create components |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
148 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
149 pca = PCA_Analysis() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
150 layer1 = Layer(w=4) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
151 layer2 = Layer(w=3) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
152 kf = KFold(dataset, K=10) |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
153 reg = Registers() |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
154 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
155 pca_batchsize=1000 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
156 cd_batchsize = 5 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
157 n_cd_updates_layer1 = 10 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
158 n_cd_updates_layer2 = 10 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
159 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
160 # create algorithm |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
161 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
162 train_pca = SEQ([ |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
163 CALL(kf.next, pca_batchsize, _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
164 CALL(pca.analyze, reg('x'))]) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
165 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
166 train_layer1 = REPEAT(n_cd_updates_layer1, SEQ([ |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
167 CALL(kf.next, cd_batchsize, _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
168 CALL(pca.filt, reg('x'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
169 CALL(cd1_update, reg('x'), layer=layer1, lr=.01)])) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
170 |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
171 train_layer2 = REPEAT(n_cd_updates_layer2, SEQ([ |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
172 CALL(kf.next, cd_batchsize, _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
173 CALL(pca.filt, reg('x'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
174 CALL(layer1.filt, reg('x'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
175 CALL(cd1_update, reg('x'), layer=layer2, lr=.01)])) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
176 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
177 kfold_prog = kf.prog( |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
178 clear = SEQ([ # FRAGMENT 1: this bit is the reset/clear stage |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
179 CALL(pca.clear), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
180 CALL(layer1.clear), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
181 CALL(layer2.clear), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
182 ]), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
183 train = SEQ([ |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
184 train_pca, |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
185 WEAVE(1, [ # Silly example of how to do debugging / loggin with WEAVE |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
186 train_layer1, |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
187 LOOP(PRINT(reg('x')))]), |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
188 train_layer2, |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
189 ]), |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
190 test=SEQ([ |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
191 CALL(pca.filt, reg('testx'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
192 CALL(layer1.filt, reg('x'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
193 CALL(layer2.filt, reg('x'), _set=reg('x')), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
194 CALL(numpy.mean, reg('x'), _set=reg('score'))]), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
195 test_data_reg=reg('testx'), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
196 test_counter_reg=reg('i'), |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
197 test_scores_reg=reg('score')) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
198 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
199 pkg1 = dict(prog=kfold_prog, kf=kf) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
200 pkg2 = copy.deepcopy(pkg1) # programs can be copied |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
201 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
202 try: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
203 pkg3 = cPickle.loads(cPickle.dumps(pkg1)) |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
204 except: |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
205 print >> sys.stderr, "pickling doesnt work, but it can be fixed I think" |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
206 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
207 pkg = pkg2 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
208 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
209 # running a program updates the variables in its package, but not the other package |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
210 pkg['prog'].run() |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
211 print pkg['kf'].scores |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
212 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
213 |
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
214 if __name__ == '__main__': |
1219
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
215 try: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
216 sys.argv[1] |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
217 except: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
218 print """You have to tell which main function to use, try: |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
219 - python plugin_JB_main.py 'main_kfold_dbn()' |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
220 - python plugin_JB_main.py 'main_weave()' |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
221 - python plugin_JB_main.py 'main_weave_popen()' |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
222 - python plugin_JB_main.py 'main_spawn()' |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
223 """ |
9fac28d80fb7
plugin_JB - removed FILT and BUFFER_REPEAT, added Registers
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1212
diff
changeset
|
224 sys.exit(1) |
1212
478bb1f8215c
plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
225 sys.exit(eval(sys.argv[1])) |