Mercurial > pylearn
annotate doc/v2_planning/plugin_greenlet.py @ 1371:98d4232df1d8
comment on OD idea
author | Razvan Pascanu <r.pascanu@gmail.com> |
---|---|
date | Mon, 15 Nov 2010 16:28:02 -0500 |
parents | acfd5e747a75 |
children |
rev | line source |
---|---|
1200
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
1 """plugin_greenlet - draft of library architecture using greenlets |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
2 |
1200
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
3 HISTORICAL - NOT ACTUALLY A PROPOSAL |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
4 ==================================== |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
5 |
1200
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
6 This was the original approach for what I renamed to plugin_JB, until I realized that I could |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
7 get the end result without using greenlets at all. |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
8 |
1200
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
9 Still, greenlets seem like they could be neat and making this program stretched my mind so I |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
10 keep it. There's something wrong when you run with the kfold validation, but until that point |
acfd5e747a75
v2planning - a few changes to plugin proposals
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1197
diff
changeset
|
11 I think it works. |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
12 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
13 """ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
14 |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
15 __license__ = None |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
16 __copyright__ = None |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
18 import copy, sys |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
19 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
20 import numpy |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
21 from greenlet import greenlet |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
22 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
23 def vm_unpack(incoming): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
24 # can't reliably distinguish between a kwargs-only switch and a switch with one dict |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
25 # argument |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
26 if incoming is None: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
27 rval = (), {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
28 if isinstance(incoming, dict): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
29 rval = (), incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
30 elif isinstance(incoming, tuple): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
31 if (len(incoming)==2 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
32 and isinstance(incoming[0], tuple) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
33 and isinstance(incoming[1], dict)): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
34 rval = incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
35 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
36 rval = incoming, {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
37 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 rval = (incoming,), {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 #print 'unpack', incoming, rval |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
40 return rval[0][0], rval[0][1], rval[0][2:], rval[1] |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
41 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
42 def unpack_from_vm(incoming): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
43 assert isinstance(incoming, tuple) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
44 assert len(incoming)==4 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
45 return incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
46 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
47 def vm_run(prog, *args, **kwargs): |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
48 #TODO: make this into a class with different ways to start the loop |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
49 # for example, if the (gr, dest, a, kw) tuple is returned, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
50 # then a program could be run for N steps and then paused, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
51 # saved, and restarted. |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
52 n_steps = kwargs.pop('n_steps', float('inf')) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
53 def vm_loop(gr, dest, a, kw): |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
54 loop_iter = 0 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
55 while loop_iter < n_steps: |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
56 if gr == 'return': |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
57 break |
1196
e9bb3340a870
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1195
diff
changeset
|
58 #print 'vm_loop gr=',gr,'args=',a, 'kwargs=', kw |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
59 gr, dest, a, kw = gr.switch(vm, gr, dest, a, kw) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
60 #print 'gmain incoming', incoming |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
61 loop_iter += 1 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
62 # permit restarting |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
63 return gr, dest, a, kw |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 vm = greenlet(vm_loop) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 return vm.switch(prog, 'return', args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
66 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
67 #################################################### |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
68 # CONTROL-FLOW CONSTRUCTS |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
69 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
70 def SEQ(glets): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
71 return REPEAT(1, glets) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
73 def REPEAT(N, glets): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 def repeat_task(vm, gself, dest, args, kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 while True: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
76 for i in xrange(N): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
77 for glet in glets: |
1196
e9bb3340a870
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1195
diff
changeset
|
78 #print 'repeat_task_i dest=%(dest)s args=%(args)s, kw=%(kwargs)s'%locals() |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
79 # jump to task `glet` |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 # with instructions to report results back to this loop `g` |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
81 _vm, _gself, _dest, args, kwargs = vm.switch(glet, gself, args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
82 assert _gself is gself |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
83 assert _dest is None # instructions can't tell us where to jump |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
84 vm, gself, dest, args, kwargs = vm.switch(dest, None, args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
85 return greenlet(repeat_task) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
87 def LOOP(seq): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
88 #TODO: implement a true infinite loop |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
89 try: |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
90 iter(seq) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
91 return REPEAT(sys.maxint, seq) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
92 except TypeError: |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
93 return REPEAT(sys.maxint, [seq]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
94 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
95 def CHOOSE(which, options): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
96 raise NotImplementedError() |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
97 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
98 def WEAVE(threads): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
99 def weave_task(vm, gself, dest, args, kwargs): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
100 # weave works by telling its threads that *it* is the vm |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
101 # and reporting back to the real vm indirectly |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
102 while True: # execution of weave is an iteration through this loop |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
103 # initially broadcast the args and kwargs to all threads |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
104 all_threads_live = True |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
105 thread_info = [(t, 'return', args, kwargs) for t in threads] |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
106 #print 'weave start -------------' |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
107 while all_threads_live: |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
108 #print 'weave iter' |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
109 for i in xrange(len(threads)): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
110 t_next, t_dest, t_args, t_kwargs = thread_info[i] |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
111 #tell the vm we're up to something, but ask it to come right back |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
112 #print 'weave 1' |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
113 _ignore = vm.switch(gself, None, (), {}) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
114 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
115 # pretend we're the vm_loop and tell the |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
116 # thread to advance by one and report back to us |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
117 #print 'weave 2', thread_info[i] |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
118 thread_info[i] = t_next.switch(gself, t_next, t_dest, t_args, t_kwargs) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
119 #print 'weave 3', thread_info[i] |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
120 if thread_info[i][0] is 'return': |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
121 #print 'thread has finished', i |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
122 all_threads_live = False |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
123 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
124 # some thread has died so we return control to parent |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
125 #####print 'weave returning', dest, args, kwargs |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
126 vm, gself, dest, args, kwargs = vm.switch(dest, None, args, kwargs) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
127 #####print 'weave continuing', dest, args, kwargs |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
128 return greenlet(weave_task) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
129 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
130 def BUFFER(N, glet): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
131 def BUFFER_loop(vm, gself, dest, args, kwargs): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
132 while True: #body runs once per execution |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
133 buf = [] |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
134 for i in xrange(N): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
135 # jump to task `glet` |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
136 # with instructions to report results back to this loop `g` |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
137 _vm, _gself, _dest, _args, _kwargs = vm.switch(glet, gself, args, kwargs) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
138 buf.append(_args[0]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
139 assert len(_args)==1 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
140 assert _kwargs=={} |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
141 assert _gself is gself |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
142 assert _dest is None # instructions can't tell us where to jump |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
143 buf = numpy.asarray(buf) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
144 vm, gself, dest, args, kwargs = vm.switch(dest, None, (buf,), {}) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
145 return greenlet(BUFFER_loop) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
146 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
147 def CALL(fn): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
148 """ |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
149 Create a greenlet whose first argument is the return-jump location. |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
150 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
151 fn must accept as the first positional argument this greenlet itself, which can be used as |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
152 the return-jump location for internal greenlet switches (ideally using gswitch). |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
153 """ |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
154 def CALL_loop(vm, gself, dest, args, kwargs): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
155 while True: |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
156 #print 'CALL calling', fn.__name__, args, kwargs |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
157 t = fn(*args, **kwargs) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
158 #TODO consider a protocol for returning args, kwargs |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
159 if t is None: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
160 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (), {}) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
161 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
162 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (t,), {}) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
163 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
164 assert gself is _gself |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
165 return greenlet(CALL_loop) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
166 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
167 #################################################### |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
168 # Components involved in the learning process |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
169 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
170 class Dataset(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
171 def __init__(self, data): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
172 self.pos = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
173 self.data = data |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
174 def next(self): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
175 rval = self.data[self.pos] |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
176 self.pos += 1 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
177 if self.pos == len(self.data): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
178 self.pos = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
179 return rval |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
180 def seek(self, pos): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
181 self.pos = pos |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
182 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
183 class KFold(object): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
184 def __init__(self, data, K): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
185 self.data = data |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
186 self.k = -1 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
187 self.scores = [None]*K |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
188 self.K = K |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
189 def next_fold(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
190 self.k += 1 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
191 self.data.seek(0) # restart the stream |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
192 def next(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
193 #TODO: skip the examples that are ommitted in this split |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
194 return self.data.next() |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
195 def init_test(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
196 pass |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
197 def next_test(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
198 return self.data.next() |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
199 def test_size(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
200 return 5 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
201 def store_scores(self, scores): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
202 self.scores[self.k] = scores |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
203 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
204 class PCA_Analysis(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
205 def __init__(self): |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
206 self.clear() |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
207 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
208 def clear(self): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
209 self.mean = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
210 self.eigvecs=0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
211 self.eigvals=0 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
212 def analyze(self, X): |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
213 self.mean = X.mean(axis=0) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
214 self.eigvecs=1 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
215 self.eigvals=1 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
216 def filt(self, X): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
217 return (X - self.mean) * self.eigvecs #TODO: divide by root eigvals? |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
218 def pseudo_inverse(self, Y): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
219 return Y |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
220 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
221 class Layer(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
222 def __init__(self, w): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
223 self.w = w |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
224 def filt(self, x): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
225 return self.w*x |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
226 def clear(self): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
227 self.w =0 |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
228 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
229 def print_obj(vm, gself, obj): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
230 print obj |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
231 def no_op(*args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
232 pass |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
233 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
234 class cd1_update(object): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
235 def __init__(self, layer, lr): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
236 self.layer = layer |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
237 self.lr = lr |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
238 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
239 def __call__(self, X): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
240 # update self.layer from observation X |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
241 print 'cd1', X |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
242 print X.mean() |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
243 self.layer.w += X.mean() * self.lr #TODO: not exactly correct math |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
244 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
245 def main(): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
246 # create components |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
247 dataset = Dataset(numpy.random.RandomState(123).randn(13,1)) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
248 pca = PCA_Analysis() |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
249 layer1 = Layer(w=4) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
250 layer2 = Layer(w=3) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
251 kf = KFold(dataset, K=10) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
252 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
253 # create algorithm |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
254 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
255 train_pca = SEQ([ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
256 BUFFER(1000, CALL(kf.next)), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
257 CALL(pca.analyze)]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
258 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
259 train_layer1 = REPEAT(10, [ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
260 BUFFER(10, CALL(kf.next)), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
261 CALL(pca.filt), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
262 CALL(cd1_update(layer1, lr=.01))]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
263 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
264 train_layer2 = REPEAT(10, [ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
265 BUFFER(10, CALL(kf.next)), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
266 CALL(pca.filt), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
267 CALL(layer1.filt), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
268 CALL(cd1_update(layer2, lr=.01))]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
269 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
270 def print_layer_w(*a,**kw): |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
271 print layer1.w |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
272 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
273 train_prog = SEQ([ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
274 train_pca, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
275 WEAVE([ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
276 train_layer1, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
277 LOOP(CALL(print_layer_w))]), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
278 train_layer2, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
279 ]) |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
280 |
1197
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
281 kfold_prog = REPEAT(10, [ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
282 CALL(kf.next_fold), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
283 CALL(pca.clear), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
284 CALL(layer1.clear), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
285 CALL(layer2.clear), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
286 train_prog, |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
287 CALL(kf.init_test), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
288 BUFFER(kf.test_size(), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
289 SEQ([ |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
290 CALL(kf.next_test), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
291 CALL(pca.filt), # may want to allow this SEQ to be |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
292 CALL(layer1.filt), # optimized into a shorter one that |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
293 CALL(layer2.filt), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
294 CALL(numpy.mean)])), # chains together theano graphs |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
295 CALL(kf.store_scores), |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
296 ]) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
297 |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
298 vm_run(kfold_prog, n_steps=500) |
a60b3472c4ba
more progress on greenlets
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1196
diff
changeset
|
299 print kf.scores |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
300 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
301 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
302 if __name__ == '__main__': |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
303 sys.exit(main()) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
304 |