annotate doc/v2_planning/plugin_RP_GD.py @ 1287:4fa2a32e8fde

mcRBM - renamed shared variable
author James Bergstra <bergstrj@iro.umontreal.ca>
date Wed, 29 Sep 2010 18:34:47 -0400
parents 6f76ecef869e
children
rev   line source
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
1 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
2 BASIC IDEA
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
3 ==========
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
4
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
5 The main ideas of the proposal are as follows:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
6 1. Basic components are defined in the traditional way (classes, methods, etc.)
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
7 2. Components are then "wrapped" into **plugins**
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
8 3. Plugins are registered through a **scheduler** to act on certain **events**
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
9 4. We have defined two basic types of events:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
10 4.1 scheduler events: these are emitted by the scheduler itself. For now we have defined:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
11 scheduler.begin() : sent at the very beginning of scheduler execution
1259
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
12 scheduler.end() : sent when the scheduler ends ( this makes sense since there is
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
13 at least from the user perspective a hierarchy of schedulars)
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
14 4.2 plugin generate events: plugins act as iterators and generate 2 basic events,
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
15 plugin.next() : sent of every "iteration". Each plugin is free to define what an
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
16 iteration actually means.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
17 plugin.end() : sent when the plugin is done iterating.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
18 5. Using Olivier Breuleux's schedular, plugins can decide to act on every event, every n-th
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
19 occurence of an event, etc.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
20
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
21 OVERVIEW
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
22 ========
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
23
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
24 The code below attempts to show how one would code a training experiment, where we pre-train +
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
25 fine-tune a DBN, while using cross-validation. We have ommitted data preprocessing for now, but
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
26 do not think it would have much of an impact on the finished product.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
27 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
28
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
29 class Counter(Plugin):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
30 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
31 This is an example of a plugin which takes care of counting "stuff". It is generic enough
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
32 that it would probably be included in the library somewhere.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
33 """
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
34
1259
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
35 def __init__(self, name, end_count, next_count = 1):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
36 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
37 :param name: name of the event we are counting (could be useful for debugging)
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
38 :param next_count: number of iterations before triggering a "next" event
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
39 :param end_count: number of iterations before triggering an "end" event
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
40 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
41 super(self, Counter).__init__()
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
42 self.n = 0
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
43 self.next_count = next_count
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
44 self.end_count = end_count
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
45
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
46 def execute(self, msg):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
47 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
48 The execute function is the one which gets executed each time the plugin is "acted"
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
49 upon. This will send "next" and "end" events to the scheduler, which other plugins can
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
50 listen to. We show an example of this later.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
51 """
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
52 self.n += 1
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
53 if self.n > self.end_count:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
54 self.fire(Event('end', value = self.n))
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
55 elif self.n > self.next_count:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
56 self.fire(Event('next', value = self.n))
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
57
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
58
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
59 def fixed_epoch_trainer(model, save, n_epochs):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
60 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
61 This is an example of a meta-plugin. Meta-plugin are just like any other plugins, except
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
62 they themselves contain other plugins and their own schedulers. A meta-plugin would replace
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
63 code-blocks which are usually found inside for-loop or if-else statements.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
64
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
65 The fixed_epoch_trainer meta-plugin takes care of training a given model, for a fixed
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
66 number of epochs and then saving the resulting model.
1259
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
67
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
68 Other arguments for having meta-plugin :
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
69 * they can define a semantically separable block of code
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
70 * they are what the library provides 99% of the code ( so you can define a certain
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
71 template of connecting plugins as a meta-plugin and ship it without worry of things)
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
72 * they can be breaked apart by the main schedular ( so you would not have "different"
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
73 schedulars running in the same time; is just one schedular, this way of constructing
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
74 things is just to improve understanding and intuitions of the syste
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
75 * they help pushing all the complexity over the backbone of the library ( i.e. the
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
76 schedular )
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
77 * all plugins registered inside a hyper-plugin are active only when the hyper-plugin is
6f76ecef869e a few changes
Razvan Pascanu <r.pascanu@gmail.com>
parents: 1258
diff changeset
78 active; In this sense they can help definining scopes ( as for variables ) - optional
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
79 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
80 # we start by defining our own private scheduler
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
81 sched = Scheduler()
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
82
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
83 # convert "components" to plugins. Maybe this could be automated or done in another way...
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
84 # syntax is not really important here.
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
85 [model, validate, save] = map(pluggin_wrapper, [model, validate, save])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
86
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
87 # instantiate the counter plugin to "end" after n_epochs
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
88 counter = Counter('epoch', 1, n_epochs)
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
89
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
90 ####
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
91 # Registering actions: overview of syntax
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
92 # plugin1.act(sched, on=[event1, event2]) means that that plugin1 will perform one
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
93 # "iteration" on every occurence of event1 and event2.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
94 ####
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
95
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
96 # In our example, we assume that 1 iteration of "model" means 1 minibatch update.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
97 # Model performs an iteration:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
98 # * when program first starts
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
99 # * after is it done with previous iteration (auto-loop)
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
100 # * after each epoch, as defined by the counter. The counter counts epochs by "trapping"
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
101 # the model's end event.
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
102 model.act(sched, on=[sched.begin(), model.step(), counter.step()])
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
103 # counter is incremented every time the
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
104 counter.act(sched, on=model.end())
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
105 # the save_model plugin then takes care of saving everything once the counter expires
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
106 save_model.act(sched, on=counter.end())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
107
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
108 # Meta-plugins also generate events: they map "internal" events to "external" events
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
109 # fixed_epoch_trainer.end() will thus correspond to counter.end()
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
110 sched.end(on=counter.end())
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
111 # in the same way, you could have:
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
112 # sched.next(on=counter.next()) but this is not useful here
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
113
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
114 return sched
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
115
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
116 def early_stop_trainer(model, validate, save, **kw):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
117 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
118 This is another plugin example, which takes care of training the model but using early
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
119 stopping.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
120 """
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
121 sched = Scheduler()
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
122
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
123 # define plugins
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
124 [model, validate, save] = map(pluggin_wrapper, [model, validate, save])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
125
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
126 early_stop = Stopper(**kw)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
127
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
128 # register actions
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
129 model.act(sched, on=[sched.begin(), model.step(), validate.step()])
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
130 # we measure validation error after every epoch (model.end)
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
131 validate.act(sched, on=model.end())
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
132 # early-stopper gets triggered every time we have a new validation error
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
133 # the error itself is passed within the "step" message
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
134 early_stop.act(sched, on=validate.step())
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
135 # model is saved whenever we find a new best model (early_stop.step) or when we have found
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
136 # THE best model (early_stop.end)
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
137 save_model.act(sched, on=[early_stop.step(), early_stop.end()])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
138
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
139 sched.end(on=early_stop.end())
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
140
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
141 return sched
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
142
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
143 def dbn_trainer(rbm1, rbm2):
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
144 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
145 This meta-plugin pre-trains a two-layer DBN for a fixed number of epochs, and then performs
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
146 fine-tuning on the resulting MLP. This should hopefully be self-explanatory.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
147 """
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
148 sched = Scheduler()
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
149
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
150 pretrain_layer1 = fixed_epoch_trainer(rbm1, save)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
151 pretrain_layer1.act(sched, on=sched.begin())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
152
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
153 pretrain_layer2 = fixed_epoch_trainer(rbm2, save)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
154 pretrain_layer2.act(sched, on=pretrain_layer1.end())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
155
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
156 ## TBD: by the layer committee
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
157 mlp = function(rbm1, rbm2)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
158
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
159 fine_tuning = early_stop_trainer(mlp, validate_mlp, save_mlp)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
160 fine_tuning.act(sched, on=pretrain_layer2.end())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
161
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
162 return sched
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
163
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
164 def single_crossval_run(trainer, kfold_plugin, kfold_measure)
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
165 """
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
166 For a fixed set of hyper-parameters, this evaluates the generalization error using KFold
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
167 cross-validation.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
168 """
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
169
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
170 sched = Scheduler()
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
171
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
172 # k-fold plugin will call rbm.change_dataset using various splits of the data
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
173 kfold_plugin.act(sched, on=[sched.begin(), trainer.end()])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
174 trainer.act(sched, on=[kfold_plugin.step()])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
175
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
176 # trainer ends on early_stop.end(). This means that trainer.end() will forward
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
177 # the early-stopping message which contains the best validation error.
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
178 kfold_measure.act(sched, on=[trainer.end(), kill=kfold_plugin.end()]
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
179
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
180 # this best validation error is then forwarded by single_crossval_run
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
181 sched.end(on=kfold_measure.end())
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
182
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
183 return sched
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
184
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
185
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
186 #### MAIN LOOP ####
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
187 rbm1 = ...
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
188 rbm2 = ...
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
189 dataset = ....
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
190 dbn_trainer = dbn_trainer(rbm1, rbm2)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
191 kfold_plugin = KFold([rbm1, rbm2], dataset)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
192 kfold_measure = ...
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
193
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
194 ## In our view, the meta-plugins defined above would live in the library somewhere. Hooks can be
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
195 ## added without modifying the library code. The meta-plugin's scheduler contains a dictionary
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
196 ## of "registered" plugins along with their events. We can thus register "user-plugins" based on
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
197 ## any of these events.
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
198 # define a logger plugin of some sort
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
199 print_stat = ....
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
200 # act on each iteration of the early-stopping plugin
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
201 # NB: the message is forwarded as is. It is up to the print_stat plugin to parse it properly.
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
202 print_stat.act(pretrain_layer1, on=pretrain_layer1.plugins['early_stop'].step())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
203
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
204 #### THIS SHOULD CORRESPOND TO THE OUTER LOOP ####
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
205 # this is the final outer-loop which tests various configurations of hyperparameters
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
206
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
207 sched = Scheduler()
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
208
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
209 hyperparam_change = DBN_HyperParam([rbm1, rbm2])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
210 hyperparam_test = single_crossval_run(dbn_trainer, kfold_plugin, kfold_measure)
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
211
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
212 hyperparam_change.act(sched, on=[sched.begin(), hyperparam_test.end()])
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
213 hyperparam_test.act(sched, on=hyperparam_change.step())
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
214
1258
c88db30f4e08 added comments to our plugin proposal
gdesjardins
parents: 1256
diff changeset
215 sched.end(hyperparam_change.end())
1256
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
216
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
217 ##### RUN THE WHOLE DAMN THING #####
bf41991692ea new plugin approach
gdesjardins
parents:
diff changeset
218 sched.run()