Mercurial > pylearn
annotate doc/v2_planning/plugin_RP.py @ 1168:77b6ed85d3f7
Update doc of learner's API
author | Pascal Lamblin <lamblinp@iro.umontreal.ca> |
---|---|
date | Fri, 17 Sep 2010 14:29:40 -0400 |
parents | 6993fef088d1 |
children | fe6c25eb1e37 |
rev | line source |
---|---|
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
1 ''' |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
2 ================================================= |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
3 Plugin system for interative algortithm Version B |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
4 ================================================= |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
5 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
6 After the meeting (September 16) we sort of stumbled on |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
7 two possible versions of the plug-in system. This represents |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
8 the second version. It suffered a few changes after seeing |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
9 Olivier's code and talking to him. |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
10 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
11 Concept |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
12 ======= |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
13 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
14 The basic idea behind this version is not to have a list of all |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
15 possible events, but rather have plugin register to events.By |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
16 specifying what plugin listens to which event produced by what |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
17 plugin you define a sort of dependency graph. Structuring things |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
18 in such a graph might make the script more intuitive when reading. |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
19 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
20 I will first go through pseudo-code for two example and then enumerate |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
21 my insights and concepts on the matter |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
22 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
23 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
24 Example : Producer - Consumer that Guillaume described |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
25 ====================================================== |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
26 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
27 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
28 .. code-block:: |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
29 ''' |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
30 sch = Schedular() |
1154 | 31 |
32 @FnPlugin(sch) | |
33 def producer(self,event): | |
34 self.fire('stuff', value = 'some text') | |
35 | |
36 @FnPlugin(sch) | |
37 def consumer(self,event): | |
38 print event.value | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
39 |
1154 | 40 @FnPlugin(sch) |
41 def prod_consumer(self,event): | |
42 print event.value | |
43 self.fire('stuff2', value = 'stuff') | |
44 | |
45 producer.act( on = Event('begin'), when = once() ) | |
46 producer.act( on = Event('stuff'), when = always() ) | |
47 consumer.act( on = Event('stuff'), when = always() ) | |
48 prod_consumer.act( on = Event('stuff'), when = always() ) | |
49 | |
50 sch.run() | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
51 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
52 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
53 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
54 ''' |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
55 Example : Logistic regression |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
56 ============================= |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
57 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
58 Task description |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
59 ---------------- |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
60 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
61 Apply a logistic regression network to some dataset. Use early stopping. |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
62 Save the weights everytime a new best score is obtained. Print trainnig score |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
63 after each epoch. |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
64 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
65 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
66 Possible script |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
67 --------------- |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
68 |
1154 | 69 Notes : This would look the same for any other architecture that does not |
70 imply pre-training ( i.e. deep networks). For example the mlp. | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
71 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
72 .. code-block:: |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
73 ''' |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
74 |
1154 | 75 sched = Schedular() |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
76 |
1154 | 77 # Data / Model Building : |
78 # I skiped over how to design this part | |
79 # though I have some ideas | |
80 real_train_data, real_valid_data = load_mnist() | |
81 model = logreg() | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
82 |
1154 | 83 # Main Plugins ( already provided in the library ); |
84 # This wrappers also registers the plugin | |
85 valid_data = create_data_plugin( sched, data = real_valid_data) | |
86 train_model = create_train_model(sched, model = model) | |
87 validate_model = create_valid_model(sched, model = model, data = valid_data) | |
88 early_stopper = create_early_stopper(sched) | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
89 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
90 |
1154 | 91 # On the fly plugins ( print random stuff); the main difference from my |
92 # FnPlugin from Olivier's version is that it also register the plugin in sched | |
93 @FnPlugin(sched) | |
94 def print_error(self, event): | |
95 if event.type == Event('begin'): | |
96 self.value = [] | |
97 elif event.type == train_model.error(): | |
98 self.value += [event.value] | |
99 else event.type == train_data.eod(): | |
100 print 'Error :', numpy.mean(self.value) | |
101 | |
102 @FnPlugin(sched) | |
103 def save_model(self, event): | |
104 if event.type == early_stopper.new_best_error(): | |
105 cPickle.dump(model.parameters(), open('best_params.pkl','wb')) | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
106 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
107 |
1154 | 108 # Create the dependency graph describing what does what |
109 train_model.act(on = train_data.batch(), when = always()) | |
110 validate_model.act(on = train_model.done(), when = every(n=10000)) | |
111 early_stopper.act(on = validate_model.error(), when = always()) | |
112 print_error.act( on = train_model.error(), when = always() ) | |
113 print_error.act( on = train_data.eod(), when = always() ) | |
114 save_model.act( on = eraly_stopper.new_best_errot(), when = always() ) | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
115 |
1154 | 116 # Run the entire thing |
117 sched.run() | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
118 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
119 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
120 ''' |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
121 Notes |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
122 ===== |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
123 |
1154 | 124 * I think we should have a FnPlugin decorator ( exactly like Olivier's) just |
125 that also attaches the new created plugin to the schedule. This way you | |
126 can create plugin on the fly ( as long as they are simple functions that | |
127 print stuff, or compute simple statitics ). | |
128 * I added a method act to a Plugin. You use that to create the dependency | |
129 graph ( it could also be named listen to be more plugin like interface) | |
130 * Plugins are obtained in 3 ways : | |
131 - by wrapping a dataset / model or something similar | |
132 - by a function that constructs it from nothing | |
133 - by decorating a function | |
134 In all cases I would suggest then when creating them you should provide | |
135 the schedular as well, and the constructor also registers the plugin | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
136 |
1154 | 137 * The plugin concept works well as long as the plugins are a bit towards |
138 heavy duty computation, disregarding printing plugins and such. If you have | |
139 many small plugins this system might only introduce an overhead. I would | |
140 argue that using theano is restricted to each plugin. Therefore I would | |
141 strongly suggest that the architecture to be done outside the schedular | |
142 with a different approach. | |
143 | |
144 * I would suggest that the framework to be used only for the training loop | |
145 (after you get the adapt function, compute error function) so is more about | |
146 the meta-learner, hyper-learner learner level. | |
147 | |
148 * A general remark that I guess everyone will agree on. We should make | |
149 sure that implementing a new plugin is as easy/simple as possible. We | |
150 have to hide all the complexity in the schedular ( it is the part of the | |
151 code we will not need or we would rarely need to work on). | |
152 | |
153 * I have not went into how to implement the different components, but | |
154 following Olivier's code I think that part would be more or less straight | |
155 forward. | |
156 | |
157 ''' | |
1153
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
158 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
159 |
ae5ba6206fd3
a first draft of pseudo-code for logreg .. using version B (?) approach
Razvan Pascanu <r.pascanu@gmail.com>
parents:
diff
changeset
|
160 ''' |