annotate doc/v2_planning/arch_src/plugin_JB_main.py @ 1214:681b5e7e3b81

a few comments on James version
author Razvan Pascanu <r.pascanu@gmail.com>
date Wed, 22 Sep 2010 10:39:39 -0400
parents 478bb1f8215c
children 9fac28d80fb7
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
13 def next(self):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
14 rval = self.data[self.pos]
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
15 self.pos += 1
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
16 if self.pos == len(self.data):
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
31 def next(self):
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
33 return self.data.next()
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
36 def next_test(self):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
37 return self.data.next()
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
43 def prog(self, clear, train, test):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
44 return REPEAT(self.K, [
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),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
49 BUFFER_REPEAT(self.test_size(),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
50 SEQ([ CALL(self.next_test), test])),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
51 FILT(self.store_scores) ])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
52
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
53 class PCA_Analysis(object):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
54 def __init__(self):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
55 self.clear()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
56
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
57 def clear(self):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
58 self.mean = 0
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
59 self.eigvecs=0
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
60 self.eigvals=0
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
61 def analyze(self, X):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
62 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
63 self.eigvecs=1
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
64 self.eigvals=1
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
65 def filt(self, X):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
66 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
67 def pseudo_inverse(self, Y):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
68 return Y
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
69
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
70 class Layer(object):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
71 def __init__(self, w):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
72 self.w = w
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
73 def filt(self, x):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
74 return self.w*x
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
75 def clear(self):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
76 self.w =0
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
77
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
78 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
79 # 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
80 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
81
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
82
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 # 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
85
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
86 def main_weave():
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
87 # 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
88
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
89 l = [0]
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
90 def f(a):
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
91 print l
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
92 l[0] += a
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
93 return l[0]
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
94
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
95 print WEAVE(1, [
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
96 BUFFER_REPEAT(3,CALL(f,1)),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
97 BUFFER_REPEAT(5,CALL(f,1)),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
98 ]).run()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
99
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
100 def main_weave_popen():
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
101 # 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
102 # parallelism
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
103
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
104 p = WEAVE(2,[
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
105 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
106 SEQ([POPEN(['sleep', '10']), PRINT('done 2')]),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
107 LOOP([
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
108 CALL(print_obj, 'polling...'),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
109 CALL(time.sleep, 1)])])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
110 # 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
111 # complete.
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
112
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
113 p.run()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
114 # 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
115 p.run()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
116
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
117 def main_spawn():
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
118 # 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
119 # in other processes
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
120 data1 = {0:"blah data1"}
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
121 data2 = {1:"foo data2"}
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
122 p = WEAVE(2,[
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
123 SPAWN(data1, REPEAT(3, [
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
124 CALL(importable_fn, data1),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
125 PRINT("hello from 1")])),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
126 SPAWN(data2, REPEAT(1, [
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
127 CALL(importable_fn, data2),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
128 PRINT("hello from 2")])),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
129 LOOP([
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
130 CALL(print_obj, 'polling...'),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
131 CALL(time.sleep, 0.5)])])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
132 print 'BEFORE'
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
133 print data1
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
134 print data2
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
135 p.run()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
136 print 'AFTER'
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
137 print data1
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
138 print data2
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
139
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
140 def main_kfold_dbn():
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
141 # 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
142 # 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
143 # control-flow elements I think.
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
144
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
145 # create components
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
146 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
147 pca = PCA_Analysis()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
148 layer1 = Layer(w=4)
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
149 layer2 = Layer(w=3)
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
150 kf = KFold(dataset, K=10)
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
151
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
152 pca_batchsize=1000
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
153 cd_batchsize = 5
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
154 n_cd_updates_layer1 = 10
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
155 n_cd_updates_layer2 = 10
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
156
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
157 # create algorithm
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
158
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
159 train_pca = SEQ([
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
160 BUFFER_REPEAT(pca_batchsize, CALL(kf.next)),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
161 FILT(pca.analyze)])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
162
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
163 train_layer1 = REPEAT(n_cd_updates_layer1, [
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
164 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
165 FILT(pca.filt),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
166 FILT(cd1_update, layer=layer1, lr=.01)])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
167
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
168 train_layer2 = REPEAT(n_cd_updates_layer2, [
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
169 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
170 FILT(pca.filt),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
171 FILT(layer1.filt),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
172 FILT(cd1_update, layer=layer2, lr=.01)])
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
173
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
174 kfold_prog = kf.prog(
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
175 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
176 CALL(pca.clear),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
177 CALL(layer1.clear),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
178 CALL(layer2.clear),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
179 ]),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
180 train = SEQ([
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
181 train_pca,
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
182 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
183 train_layer1,
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
184 LOOP(CALL(print_obj_attr, layer1, 'w'))]),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
185 train_layer2,
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
186 ]),
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
187 test=SEQ([
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
188 FILT(pca.filt), # may want to allow this SEQ to be
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
189 FILT(layer1.filt), # optimized into a shorter one that
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
190 FILT(layer2.filt), # compiles these calls together with
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
191 FILT(numpy.mean)])) # Theano
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
192
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
193 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
194 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
195
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
196 try:
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
197 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
198 except:
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
199 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
200
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
201 pkg = pkg2
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
202
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
203 # 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
204 pkg['prog'].run()
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
205 print pkg['kf'].scores
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
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
208 if __name__ == '__main__':
478bb1f8215c plugin_JB - added SPAWN control element and demo program
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
209 sys.exit(eval(sys.argv[1]))