annotate doc/v2_planning/plugin_JB.py @ 1199:98954d8cb92d

v2planning - modifs to plugin_JB
author James Bergstra <bergstrj@iro.umontreal.ca>
date Mon, 20 Sep 2010 02:56:11 -0400
parents 1387771296a8
children acfd5e747a75
rev   line source
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
1 """plugin_JB - draft of library architecture using iterators"""
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
2
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
3
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
4 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
5
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
6 - PICKLABLE - algorithms are serializable at all points during execution
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
7
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
8 - ITERATOR walks through algorithms with fine granularity
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
9
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
10 - COMPONENTS - library provides components on which programs operate
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
11
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
12 - ALGORITHMS - library provides algorithms in clean (no hooks) form
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
13
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
14 - HOOKS - user can insert print / debug logic with search/replace type calls
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
15 e.g. prog.find(CALL(cd1_update)).replace_with(SEQ([CALL(cd1_update), CALL(debugfn)]))
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
16
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
17 - PRINTING - user can print the 'program code' of an algorithm built from library pieces
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
18
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
19 - MODULAR EXPERIMENTS - an experiment object with one (or more?) programs and all of the objects referred to by
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
20 those programs. It is the preferred type of object to be serialized. The main components of
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
21 the algorithms should be top-level attributes of the package. This object can be serialized
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
22 and loaded in another process to implement job migration.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
23
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
24 - OPTIMIZATION - program can be optimized automatically
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
25 e.g. BUFFER(N, CALL(dataset.next)) can be replaced if dataset.next implements the right
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
26 attribute/protocol for 'bufferable' or something.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
27
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
28 e.g. SEQ([a,b,c,d]) can be compiled with Theano if sub-sequence is compatible
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
29
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
30 - don't need greenlets to get efficiency, the implementations of control flow ops can manage a
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
31 stack or stack tree in the vm (like greenlets do I think) we don't really need
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
32 greenlets/stackless I don't think
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
33
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
34 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
35
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
36 __license__ = None
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
37 __copyright__ = None
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
38
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
39 import copy, sys, cPickle
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
40
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
41 import numpy
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
42
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
43
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
44 ###################################################
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
45 # Virtual Machine for executing programs
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
46
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
47 class VirtualMachine(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
48 def __init__(self, prog):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
49 self.prog = prog
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
50 self.started = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
51 self.finished=False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
52 def __iter__(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
53 assert not self.started
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
54 self.prog.start(None)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
55 self.started = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
56 return self
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
57 def next(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
58 if self.finished:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
59 raise StopIteration()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
60 r = self.prog.step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
61 if r is INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
62 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
63 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
64 self.finished=True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
65 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
66 def run(self,n_steps=float('inf')):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
67 i = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
68 for r in self:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
69 i += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
70 if i > n_steps:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
71 break
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
72 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
73
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
74
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
75 ####################################################
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
76 # CONTROL-FLOW CONSTRUCTS
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
77
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
78 class INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
79 """Return value for Element.step"""
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
80
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
81 class ELEMENT(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
82 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
83 every execution block has a driver
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
84
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
85 the driver calls start when entering a new control element
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
86 - this would be called once per e.g. outer loop iteration
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
87
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
88 the driver calls step to advance the control element
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
89 - which returns INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
90 - which returns any other object to indicate completion
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
91 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
92
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
93 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
94 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
95 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
96 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
97
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
98 class BUFFER_REPEAT(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
99 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
100 Accumulate a number of return values into one list / array.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
101
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
102 The source of return values `src` is a control element that will be restarted repeatedly in
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
103 order to fulfil the requiement of gathering N samples.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
104
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
105 TODO: support accumulating of tuples of arrays
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
106 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
107 def __init__(self, N, src, storage=None):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
108 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
109 TODO: use preallocated `storage`
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
110 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
111 self.N = N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
112 self.n = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
113 self.src = src
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
114 self.storage = storage
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
115 self.src.start(None)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
116 if self.storage != None:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
117 raise NotImplementedError()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
118 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
119 self.buf = [None] * self.N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
120 self.n = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
121 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
122 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
123 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
124 r = self.src.step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
125 if r is INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
126 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
127 self.src.start(None) # restart our stream
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
128 self.buf[self.n] = r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
129 self.n += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
130 if self.n == self.N:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
131 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
132 return self.buf
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
133 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
134 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
135 assert 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
136
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
137 class CALL(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
138 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
139 Control flow terminal - call a python function or method.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
140
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
141 Returns the return value of the call.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
142 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
143 def __init__(self, fn, *args, **kwargs):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
144 self.fn = fn
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
145 self.args = args
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
146 self.kwargs=kwargs
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
147 self.use_start_arg = kwargs.pop('use_start_arg', False)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
148 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
149 self.start_arg = arg
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
150 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
151 return self
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
152 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
153 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
154 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
155 if self.use_start_arg:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
156 if self.args:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
157 raise TypeError('cant get positional args both ways')
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
158 return self.fn(self.start_arg, **self.kwargs)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
159 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
160 return self.fn(*self.args, **self.kwargs)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
161 def __getstate__(self):
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
162 rval = dict(self.__dict__)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
163 if type(self.fn) is type(self.step): #instancemethod
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
164 fn = rval.pop('fn')
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
165 rval['i fn'] = fn.im_func, fn.im_self, fn.im_class
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
166 return rval
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
167 def __setstate__(self, dct):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
168 if 'i fn' in dct:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
169 dct['fn'] = type(self.step)(*dct.pop('i fn'))
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
170 self.__dict__.update(dct)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
171
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
172 def FILT(fn, **kwargs):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
173 """
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
174 Return a CALL object that uses the return value from the previous CALL as the first and
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
175 only positional argument.
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
176 """
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
177 return CALL(fn, use_start_arg=True, **kwargs)
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
178
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
179 def CHOOSE(which, options):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
180 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
181 Execute one out of a number of optional control flow paths
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
182 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
183 raise NotImplementedError()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
184
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
185 def LOOP(elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
186 #TODO: implement a true infinite loop
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
187 try:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
188 iter(elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
189 return REPEAT(sys.maxint, elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
190 except TypeError:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
191 return REPEAT(sys.maxint, [elements])
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
192
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
193 class REPEAT(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
194 def __init__(self, N, elements, pass_rvals=False):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
195 self.N = N
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
196 self.elements = elements
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
197 self.pass_rvals = pass_rvals
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
198 #TODO: check for N being callable
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
199 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
200 self.n = 0 #loop iteration
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
201 self.idx = 0 #element idx
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
202 self.finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
203 self.elements[0].start(arg)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
204 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
205 assert not self.finished
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
206 r = self.elements[self.idx].step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
207 if r is INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
208 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
209 self.idx += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
210 if self.idx < len(self.elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
211 self.elements[self.idx].start(r)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
212 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
213 self.n += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
214 if self.n < self.N:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
215 self.idx = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
216 self.elements[self.idx].start(r)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
217 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
218 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
219 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
220 return r
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
221
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
222 def SEQ(elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
223 return REPEAT(1, elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
224
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
225 class WEAVE(ELEMENT):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
226 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
227 Interleave execution of a number of elements.
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
228
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
229 TODO: allow a schedule (at least relative frequency) of elements from each program
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
230 """
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
231 def __init__(self, elements):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
232 self.elements = elements
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
233 def start(self, arg):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
234 for el in self.elements:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
235 el.start(arg)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
236 self.idx = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
237 self.any_is_finished = False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
238 self.finished= False
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
239 def step(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
240 assert not self.finished # if this is triggered, we have a broken driver
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
241 self.idx = self.idx % len(self.elements)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
242 r = self.elements[self.idx].step()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
243 if r is not INCOMPLETE:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
244 self.any_is_finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
245 self.idx += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
246 if self.idx == len(self.elements) and self.any_is_finished:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
247 self.finished = True
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
248 return None # dummy completion value
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
249 else:
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
250 return INCOMPLETE
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
251
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
252
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
253 ####################################################
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
254 # [Dummy] Components involved in learning algorithms
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
255
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
256 class Dataset(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
257 def __init__(self, data):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
258 self.pos = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
259 self.data = data
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
260 def next(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
261 rval = self.data[self.pos]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
262 self.pos += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
263 if self.pos == len(self.data):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
264 self.pos = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
265 return rval
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
266 def seek(self, pos):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
267 self.pos = pos
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
268
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
269 class KFold(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
270 def __init__(self, data, K):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
271 self.data = data
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
272 self.k = -1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
273 self.scores = [None]*K
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
274 self.K = K
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
275 def next_fold(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
276 self.k += 1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
277 self.data.seek(0) # restart the stream
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
278 def next(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
279 #TODO: skip the examples that are ommitted in this split
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
280 return self.data.next()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
281 def init_test(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
282 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
283 def next_test(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
284 return self.data.next()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
285 def test_size(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
286 return 5
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
287 def store_scores(self, scores):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
288 self.scores[self.k] = scores
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
289
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
290 def prog(self, clear, train, test):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
291 return REPEAT(self.K, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
292 CALL(self.next_fold),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
293 clear,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
294 train,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
295 CALL(self.init_test),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
296 BUFFER_REPEAT(self.test_size(),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
297 SEQ([ CALL(self.next_test), test])),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
298 FILT(self.store_scores) ])
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
299
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
300 class PCA_Analysis(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
301 def __init__(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
302 self.clear()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
303
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
304 def clear(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
305 self.mean = 0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
306 self.eigvecs=0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
307 self.eigvals=0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
308 def analyze(self, X):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
309 self.mean = numpy.mean(X, axis=0)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
310 self.eigvecs=1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
311 self.eigvals=1
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
312 def filt(self, X):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
313 return (X - self.mean) * self.eigvecs #TODO: divide by root eigvals?
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
314 def pseudo_inverse(self, Y):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
315 return Y
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
316
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
317 class Layer(object):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
318 def __init__(self, w):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
319 self.w = w
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
320 def filt(self, x):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
321 return self.w*x
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
322 def clear(self):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
323 self.w =0
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
324
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
325 def print_obj(obj):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
326 print obj
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
327 def print_obj_attr(obj, attr):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
328 print getattr(obj, attr)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
329 def no_op(*args, **kwargs):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
330 pass
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
331
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
332 def cd1_update(X, layer, lr):
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
333 # update self.layer from observation X
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
334 layer.w += X.mean() * lr #TODO: not exactly correct math!
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
335
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
336 def simple_main():
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
337
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
338 l = [0]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
339 def f(a):
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
340 print l
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
341 l[0] += a
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
342 return l[0]
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
343
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
344 print VirtualMachine(WEAVE([
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
345 BUFFER_REPEAT(3,CALL(f,1)),
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
346 BUFFER_REPEAT(5,CALL(f,1)),
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
347 ])).run()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
348
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
349 def main():
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
350 # create components
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
351 dataset = Dataset(numpy.random.RandomState(123).randn(13,1))
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
352 pca = PCA_Analysis()
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
353 layer1 = Layer(w=4)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
354 layer2 = Layer(w=3)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
355 kf = KFold(dataset, K=10)
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
356
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
357 pca_batchsize=1000
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
358 cd_batchsize = 5
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
359 n_cd_updates_layer1 = 10
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
360 n_cd_updates_layer2 = 10
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
361
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
362 # create algorithm
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
363
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
364 train_pca = SEQ([
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
365 BUFFER_REPEAT(pca_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
366 FILT(pca.analyze)])
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
367
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
368 train_layer1 = REPEAT(n_cd_updates_layer1, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
369 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
370 FILT(pca.filt),
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
371 FILT(cd1_update, layer=layer1, lr=.01)])
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
372
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
373 train_layer2 = REPEAT(n_cd_updates_layer2, [
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
374 BUFFER_REPEAT(cd_batchsize, CALL(kf.next)),
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
375 FILT(pca.filt),
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
376 FILT(layer1.filt),
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
377 FILT(cd1_update, layer=layer2, lr=.01)])
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
378
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
379 kfold_prog = kf.prog(
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
380 clear = SEQ([ # FRAGMENT 1: this bit is the reset/clear stage
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
381 CALL(pca.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
382 CALL(layer1.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
383 CALL(layer2.clear),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
384 ]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
385 train = SEQ([
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
386 train_pca,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
387 WEAVE([ # Silly example of how to do debugging / loggin with WEAVE
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
388 train_layer1,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
389 LOOP(CALL(print_obj_attr, layer1, 'w'))]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
390 train_layer2,
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
391 ]),
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
392 test=SEQ([
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
393 FILT(pca.filt), # may want to allow this SEQ to be
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
394 FILT(layer1.filt), # optimized into a shorter one that
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
395 FILT(layer2.filt), # compiles these calls together with
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
396 FILT(numpy.mean)])) # Theano
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
397
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
398 pkg1 = dict(prog=kfold_prog, kf=kf)
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
399 pkg2 = copy.deepcopy(pkg1) # programs can be copied
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
400
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
401 try:
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
402 pkg3 = cPickle.loads(cPickle.dumps(pkg1))
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
403 except:
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
404 print >> sys.stderr, "pickling doesnt work, but it can be fixed I think"
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
405
1199
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
406 pkg = pkg2
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
407
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
408 # running a program updates the variables in its package, but not the other package
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
409 VirtualMachine(pkg['prog']).run()
98954d8cb92d v2planning - modifs to plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents: 1198
diff changeset
410 print pkg['kf'].scores
1198
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
411
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
412
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
413 if __name__ == '__main__':
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
414 sys.exit(main())
1387771296a8 v2planning adding plugin_JB
James Bergstra <bergstrj@iro.umontreal.ca>
parents:
diff changeset
415