Mercurial > pylearn
annotate doc/v2_planning/plugin_greenlet.py @ 1196:e9bb3340a870
plugin_greenlet draft0
author | James Bergstra <bergstrj@iro.umontreal.ca> |
---|---|
date | Sun, 19 Sep 2010 13:06:16 -0400 |
parents | d3ee0d2d03e6 |
children | a60b3472c4ba |
rev | line source |
---|---|
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
1 """plugin_greenlet - draft of library architecture using greenlets""" |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
2 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
3 __license__ = None |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
4 __copyright__ = None |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
5 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
6 import copy, sys |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
7 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
8 import numpy |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
9 from greenlet import greenlet |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
10 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
11 def vm_unpack(incoming): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
12 # 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
|
13 # argument |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
14 if incoming is None: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
15 rval = (), {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
16 if isinstance(incoming, dict): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
17 rval = (), incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
18 elif isinstance(incoming, tuple): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
19 if (len(incoming)==2 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
20 and isinstance(incoming[0], tuple) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
21 and isinstance(incoming[1], dict)): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
22 rval = incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
23 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
24 rval = incoming, {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
25 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
26 rval = (incoming,), {} |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
27 #print 'unpack', incoming, rval |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
28 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
|
29 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
30 def unpack_from_vm(incoming): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
31 assert isinstance(incoming, tuple) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
32 assert len(incoming)==4 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
33 return incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
34 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
35 def vm_run(prog, *args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
36 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
37 def vm_loop(gr, dest, a, kw): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
38 while True: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
39 if gr == 'return': |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
40 return a, kw |
1196
e9bb3340a870
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1195
diff
changeset
|
41 #print 'vm_loop gr=',gr,'args=',a, 'kwargs=', kw |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
42 gr, dest, a, kw = gr.switch(vm, gr, dest, a, kw) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
43 #print 'gmain incoming', incoming |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
44 vm = greenlet(vm_loop) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
45 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
46 return vm.switch(prog, 'return', args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
47 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
48 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
49 def seq(glets): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
50 return repeat(1, glets) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
51 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
52 def repeat(N, glets): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
53 def repeat_task(vm, gself, dest, args, kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
54 while True: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
55 for i in xrange(N): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
56 for glet in glets: |
1196
e9bb3340a870
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1195
diff
changeset
|
57 #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
|
58 # jump to task `glet` |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
59 # with instructions to report results back to this loop `g` |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
60 _vm, _gself, _dest, args, kwargs = vm.switch(glet, gself, args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
61 assert _gself is gself |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
62 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
|
63 vm, gself, dest, args, kwargs = vm.switch(dest, None, args, kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
64 return greenlet(repeat_task) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
65 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
66 def choose(which, options): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
67 raise NotImplementedError() |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
68 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
69 def weave(threads): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
70 raise NotImplementedError() |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
71 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
72 def service(fn): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
73 """ |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
74 Create a greenlet whose first argument is the return-jump location. |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
75 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
76 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
|
77 the return-jump location for internal greenlet switches (ideally using gswitch). |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
78 """ |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
79 def service_loop(vm, gself, dest, args, kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
80 while True: |
1196
e9bb3340a870
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
1195
diff
changeset
|
81 #print 'service calling', fn.__name__, args, kwargs |
1195
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
82 t = fn(vm, gself, *args, **kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
83 #TODO consider a protocol for returning args, kwargs |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
84 if t is None: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
85 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (), {}) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
86 else: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
87 _vm,_gself,dest, args, kwargs = vm.switch(dest, None, (t,), {}) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
88 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
89 assert gself is _gself |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
90 return greenlet(service_loop) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
91 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
92 #################################################### |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
93 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
94 class Dataset(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
95 def __init__(self, data): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
96 self.pos = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
97 self.data = data |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
98 def next(self, vm, gself): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
99 rval = self.data[self.pos] |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
100 self.pos += 1 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
101 if self.pos == len(self.data): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
102 self.pos = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
103 return rval |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
104 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
105 class PCA_Analysis(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
106 def __init__(self): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
107 self.mean = 0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
108 self.eigvecs=0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
109 self.eigvals=0 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
110 def analyze(self, me, X): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
111 self.mean = X.mean(axis=0) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
112 self.eigvecs=1 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
113 self.eigvals=1 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
114 def filt(self,me, X): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
115 return (self.X - self.mean) * self.eigvecs #TODO: divide by root eigvals? |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
116 def pseudo_inverse(self, Y): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
117 return Y |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
118 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
119 class Layer(object): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
120 def __init__(self, w): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
121 self.w = w |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
122 def filt(self, x): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
123 return self.w*x |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
124 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
125 def batches(src, N): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
126 # src is a service |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
127 def rval(me): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
128 print 'batches src=', src, 'me=', me |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
129 return numpy.asarray([gswitch(src, me)[0][0] for i in range(N)]) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
130 return rval |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
131 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
132 def print_obj(vm, gself, obj): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
133 print obj |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
134 def no_op(*args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
135 pass |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
136 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
137 def build_pca_trainer(data_src, pca_module, N): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
138 return greenlet( |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
139 batches( |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
140 N=5, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
141 src=inf_data, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
142 dest=flow(pca_module.analyze, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
143 dest=layer1_trainer))) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
144 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
145 def main(): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
146 dataset = Dataset(numpy.random.RandomState(123).randn(10,2)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
147 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
148 prog=repeat(3, [service(dataset.next),service(print_obj)]) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
149 vm_run(prog) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
150 vm_run(prog) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
151 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
152 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
153 def main_arch(): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
154 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
155 # create components |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
156 dataset = Dataset(numpy.random.RandomState(123).randn(10,2)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
157 pca_module = PCA_Analysis() |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
158 layer1 = Layer(w=4) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
159 layer2 = Layer(w=3) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
160 kf = KFold(dataset, K=10) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
161 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
162 # create algorithm |
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 train_pca = seq([ np_batch(kf.next, 1000), pca.analyze]) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
165 train_layer1 = repeat(100, [kf.next, pca.filt, cd1_update(layer1, lr=.01)]) |
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 algo = repeat(10, [ |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
168 KFold.step, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
169 seq([train_pca, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
170 train_layer1, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
171 train_layer2, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
172 train_classifier, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
173 save_classifier, |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
174 test_classifier]), |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
175 KFold.set_score]) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
176 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
177 gswitch(algo) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
178 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
179 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
180 def main1(): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
181 dataset = Dataset(numpy.random.RandomState(123).randn(10,2)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
182 pca_module = PCA_Analysis() |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
183 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
184 # pca |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
185 next_data = service(dataset.next) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
186 b5 = service(batches(src=next_data, N=5)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
187 print_pca_analyze = flow(pca_module.analyze, dest=sink(print_obj)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
188 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
189 # layer1_training |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
190 layer1_training = driver( |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
191 fn=cd1_trainer(layer1), |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
192 srcs=[], |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
193 ) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
194 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
195 gswitch(b5, print_pca_analyze) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
196 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
197 if __name__ == '__main__': |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
198 sys.exit(main()) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
199 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
200 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
201 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
202 def flow(fn, dest): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
203 def rval(*args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
204 while True: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
205 print 'flow calling', fn.__name__, args, kwargs |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
206 t = fn(g, *args, **kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
207 args, kwargs = gswitch(dest, t) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
208 g = greenlet(rval) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
209 return g |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
210 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
211 def sink(fn): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
212 def rval(*args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
213 return fn(g, *args, **kwargs) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
214 g = greenlet(rval) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
215 return g |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
216 |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
217 def consumer(fn, src): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
218 def rval(*args, **kwargs): |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
219 while True: |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
220 fn(gswitch(src, *args, **kwargs)) |
d3ee0d2d03e6
plugin_greenlet draft0
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff
changeset
|
221 return greenlet(rval) |