@@ -146,42 +146,42 @@ classification. |
146 | 146 |
|
147 | 147 | class DBN(object): |
148 | 148 |
|
149 | | - def __init__(self, numpy_rng, theano_rng = None, n_ins = 784, |
150 | | - hidden_layers_sizes=[500,500], n_outs = 10): |
| 149 | + def __init__(self, numpy_rng, theano_rng=None, n_ins=784, |
| 150 | + hidden_layers_sizes=[500, 500], n_outs=10): |
151 | 151 | """This class is made to support a variable number of layers. |
152 | 152 |
|
153 | 153 | :type numpy_rng: numpy.random.RandomState |
154 | | - :param numpy_rng: numpy random number generator used to draw initial |
| 154 | + :param numpy_rng: numpy random number generator used to draw initial |
155 | 155 | weights |
156 | 156 |
|
157 | 157 | :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams |
158 | | - :param theano_rng: Theano random generator; if None is given one is |
| 158 | + :param theano_rng: Theano random generator; if None is given one is |
159 | 159 | generated based on a seed drawn from `rng` |
160 | 160 |
|
161 | 161 | :type n_ins: int |
162 | 162 | :param n_ins: dimension of the input to the DBN |
163 | 163 |
|
164 | 164 | :type n_layers_sizes: list of ints |
165 | | - :param n_layers_sizes: intermediate layers size, must contain |
| 165 | + :param n_layers_sizes: intermediate layers size, must contain |
166 | 166 | at least one value |
167 | 167 |
|
168 | 168 | :type n_outs: int |
169 | 169 | :param n_outs: dimension of the output of the network |
170 | 170 | """ |
171 | 171 |
|
172 | 172 | self.sigmoid_layers = [] |
173 | | - self.rbm_layers = [] |
174 | | - self.params = [] |
175 | | - self.n_layers = len(hidden_layers_sizes) |
| 173 | + self.rbm_layers = [] |
| 174 | + self.params = [] |
| 175 | + self.n_layers = len(hidden_layers_sizes) |
176 | 176 |
|
177 | 177 | assert self.n_layers > 0 |
178 | 178 |
|
179 | 179 | if not theano_rng: |
180 | | - theano_rng = RandomStreams(numpy_rng.randint(2**30)) |
| 180 | + theano_rng = RandomStreams(numpy_rng.randint(2 ** 30)) |
181 | 181 |
|
182 | 182 | # allocate symbolic variables for the data |
183 | | - self.x = T.matrix('x') # the data is presented as rasterized images |
184 | | - self.y = T.ivector('y') # the labels are presented as 1D vector of |
| 183 | + self.x = T.matrix('x') # the data is presented as rasterized images |
| 184 | + self.y = T.ivector('y') # the labels are presented as 1D vector of |
185 | 185 | # [int] labels |
186 | 186 |
|
187 | 187 | ``self.sigmoid_layers`` will store the feed-forward graphs which together form |
@@ -199,28 +199,28 @@ hidden bias with its corresponding sigmoid layer. |
199 | 199 |
|
200 | 200 | .. code-block:: python |
201 | 201 |
|
202 | | - for i in xrange( self.n_layers ): |
| 202 | + for i in xrange(self.n_layers): |
203 | 203 | # construct the sigmoidal layer |
204 | 204 |
|
205 | | - # the size of the input is either the number of hidden units of the layer below or |
206 | | - # the input size if we are on the first layer |
207 | | - if i == 0 : |
| 205 | + # the size of the input is either the number of hidden units of the |
| 206 | + # layer below or the input size if we are on the first layer |
| 207 | + if i == 0: |
208 | 208 | input_size = n_ins |
209 | 209 | else: |
210 | | - input_size = hidden_layers_sizes[i-1] |
| 210 | + input_size = hidden_layers_sizes[i - 1] |
211 | 211 |
|
212 | | - # the input to this layer is either the activation of the hidden layer below or the |
213 | | - # input of the DBN if you are on the first layer |
214 | | - if i == 0 : |
| 212 | + # the input to this layer is either the activation of the hidden |
| 213 | + # layer below or the input of the DBN if you are on the first layer |
| 214 | + if i == 0: |
215 | 215 | layer_input = self.x |
216 | 216 | else: |
217 | 217 | layer_input = self.sigmoid_layers[-1].output |
218 | 218 |
|
219 | | - sigmoid_layer = HiddenLayer(rng = numpy_rng, |
220 | | - input = layer_input, |
221 | | - n_in = input_size, |
222 | | - n_out = hidden_layers_sizes[i], |
223 | | - activation = T.nnet.sigmoid) |
| 219 | + sigmoid_layer = HiddenLayer(rng=numpy_rng, |
| 220 | + input=layer_input, |
| 221 | + n_in=input_size, |
| 222 | + n_out=hidden_layers_sizes[i], |
| 223 | + activation=T.nnet.sigmoid) |
224 | 224 |
|
225 | 225 | # add the layer to our list of layers |
226 | 226 | self.sigmoid_layers.append(sigmoid_layer) |
@@ -231,13 +231,14 @@ hidden bias with its corresponding sigmoid layer. |
231 | 231 | self.params.extend(sigmoid_layer.params) |
232 | 232 |
|
233 | 233 | # Construct an RBM that shared weights with this layer |
234 | | - rbm_layer = RBM(numpy_rng = numpy_rng, theano_rng = theano_rng, |
235 | | - input = layer_input, |
236 | | - n_visible = input_size, |
237 | | - n_hidden = hidden_layers_sizes[i], |
238 | | - W = sigmoid_layer.W, |
239 | | - hbias = sigmoid_layer.b) |
240 | | - self.rbm_layers.append(rbm_layer) |
| 234 | + rbm_layer = RBM(numpy_rng=numpy_rng, |
| 235 | + theano_rng=theano_rng, |
| 236 | + input=layer_input, |
| 237 | + n_visible=input_size, |
| 238 | + n_hidden=hidden_layers_sizes[i], |
| 239 | + W=sigmoid_layer.W, |
| 240 | + hbias=sigmoid_layer.b) |
| 241 | + self.rbm_layers.append(rbm_layer) |
241 | 242 |
|
242 | 243 |
|
243 | 244 | All that is left is to stack one last logistic regression layer in order to |
@@ -247,14 +248,14 @@ form an MLP. We will use the ``LogisticRegression`` class introduced in |
247 | 248 | .. code-block:: python |
248 | 249 |
|
249 | 250 | # We now need to add a logistic layer on top of the MLP |
250 | | - self.logLayer = LogisticRegression(\ |
251 | | - input = self.sigmoid_layers[-1].output,\ |
252 | | - n_in = hidden_layers_sizes[-1], n_out = n_outs) |
| 251 | + self.logLayer = LogisticRegression( |
| 252 | + input=self.sigmoid_layers[-1].output, |
| 253 | + n_in=hidden_layers_sizes[-1], n_out=n_outs) |
253 | 254 | self.params.extend(self.logLayer.params) |
254 | 255 |
|
255 | | - # construct a function that implements one step of fine-tuning compute the cost for |
256 | | - # second phase of training, defined as the negative log likelihood |
257 | | - # of the logistic regression (output) layer |
| 256 | + # construct a function that implements one step of fine-tuning compute |
| 257 | + # the cost for second phase of training, defined as the negative log |
| 258 | + # likelihood of the logistic regression (output) layer |
258 | 259 | self.finetune_cost = self.logLayer.negative_log_likelihood(self.y) |
259 | 260 |
|
260 | 261 | # compute the gradients with respect to the model parameters |
@@ -284,21 +285,21 @@ function which implements one step of training for the ``RBM`` at layer |
284 | 285 | ''' |
285 | 286 |
|
286 | 287 | # index to a [mini]batch |
287 | | - index = T.lscalar('index') # index to a minibatch |
| 288 | + index = T.lscalar('index') # index to a minibatch |
288 | 289 |
|
289 | 290 | In order to be able to change the learning rate during training, we associate a |
290 | 291 | Theano variable to it that has a default value. |
291 | 292 |
|
292 | 293 | .. code-block:: python |
293 | 294 |
|
294 | | - learning_rate = T.scalar('lr') # learning rate to use |
| 295 | + learning_rate = T.scalar('lr') # learning rate to use |
295 | 296 |
|
296 | 297 | # number of batches |
297 | 298 | n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size |
298 | 299 | # begining of a batch, given `index` |
299 | 300 | batch_begin = index * batch_size |
300 | 301 | # ending of a batch given `index` |
301 | | - batch_end = batch_begin+batch_size |
| 302 | + batch_end = batch_begin + batch_size |
302 | 303 |
|
303 | 304 | pretrain_fns = [] |
304 | 305 | for rbm in self.rbm_layers: |
@@ -311,13 +312,14 @@ Theano variable to it that has a default value. |
311 | 312 | # compile the Theano function; check if k is also a Theano |
312 | 313 | # variable, if so added to the inputs of the function |
313 | 314 | if isinstance(k, theano.Variable): |
314 | | - inputs = [ index, theano.Param(learning_rate, default=0.1),k] |
| 315 | + inputs = [index, theano.Param(learning_rate, default=0.1), k] |
315 | 316 | else: |
316 | | - inputs = [ index, theano.Param(learning_rate, default=0.1)] |
317 | | - fn = theano.function(inputs = inputs, |
318 | | - outputs = cost, |
319 | | - updates = updates, |
320 | | - givens = {self.x :train_set_x[batch_begin:batch_end]}) |
| 317 | + inputs = index, theano.Param(learning_rate, default=0.1)] |
| 318 | + fn = theano.function(inputs=inputs, |
| 319 | + outputs=cost, |
| 320 | + updates=updates, |
| 321 | + givens={self.x: train_set_x[batch_begin: |
| 322 | + batch_end]}) |
321 | 323 | # append `fn` to the list of functions |
322 | 324 | pretrain_fns.append(fn) |
323 | 325 |
|
@@ -355,7 +357,7 @@ and a ``test_model`` function). |
355 | 357 |
|
356 | 358 | (train_set_x, train_set_y) = datasets[0] |
357 | 359 | (valid_set_x, valid_set_y) = datasets[1] |
358 | | - (test_set_x , test_set_y ) = datasets[2] |
| 360 | + (test_set_x, test_set_y) = datasets[2] |
359 | 361 |
|
360 | 362 | # compute number of minibatches for training, validation and testing |
361 | 363 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size |
@@ -371,22 +373,22 @@ and a ``test_model`` function). |
371 | 373 | for param, gparam in zip(self.params, gparams): |
372 | 374 | updates[param] = param - gparam*learning_rate |
373 | 375 |
|
374 | | - train_fn = theano.function(inputs = [index], |
375 | | - outputs = self.finetune_cost, |
376 | | - updates = updates, |
377 | | - givens = { |
378 | | - self.x : train_set_x[index*batch_size:(index+1)*batch_size], |
379 | | - self.y : train_set_y[index*batch_size:(index+1)*batch_size]}) |
| 376 | + train_fn = theano.function(inputs=[index], |
| 377 | + outputs= self.finetune_cost, |
| 378 | + updates=updates, |
| 379 | + givens={ |
| 380 | + self.x: train_set_x[index * batch_size: (index + 1) * batch_size], |
| 381 | + self.y: train_set_y[index * batch_size: (index + 1) * batch_size]}) |
380 | 382 |
|
381 | 383 | test_score_i = theano.function([index], self.errors, |
382 | | - givens = { |
383 | | - self.x: test_set_x[index*batch_size:(index+1)*batch_size], |
384 | | - self.y: test_set_y[index*batch_size:(index+1)*batch_size]}) |
| 384 | + givens={ |
| 385 | + self.x: test_set_x[index * batch_size: (index + 1) * batch_size], |
| 386 | + self.y: test_set_y[index * batch_size: (index + 1) * batch_size]}) |
385 | 387 |
|
386 | 388 | valid_score_i = theano.function([index], self.errors, |
387 | | - givens = { |
388 | | - self.x: valid_set_x[index*batch_size:(index+1)*batch_size], |
389 | | - self.y: valid_set_y[index*batch_size:(index+1)*batch_size]}) |
| 389 | + givens={ |
| 390 | + self.x: valid_set_x[index * batch_size: (index + 1) * batch_size], |
| 391 | + self.y: valid_set_y[index * batch_size: (index + 1) * batch_size]}) |
390 | 392 |
|
391 | 393 | # Create a function that scans the entire validation set |
392 | 394 | def valid_score(): |
@@ -415,9 +417,9 @@ The few lines of code below constructs the deep belief network : |
415 | 417 | numpy_rng = numpy.random.RandomState(123) |
416 | 418 | print '... building the model' |
417 | 419 | # construct the Deep Belief Network |
418 | | - dbn = DBN(numpy_rng = numpy_rng, n_ins = 28*28, |
419 | | - hidden_layers_sizes = [1000,1000,1000], |
420 | | - n_outs = 10) |
| 420 | + dbn = DBN(numpy_rng=numpy_rng, n_ins=28 * 28, |
| 421 | + hidden_layers_sizes=[1000, 1000, 1000], |
| 422 | + n_outs=10) |
421 | 423 |
|
422 | 424 |
|
423 | 425 |
|
@@ -440,21 +442,21 @@ given by ``pretraining_epochs``. |
440 | 442 | print '... getting the pretraining functions' |
441 | 443 | # We are using CD-1 here |
442 | 444 | pretraining_fns = dbn.pretraining_functions( |
443 | | - train_set_x = train_set_x, |
444 | | - batch_size = batch_size, |
445 | | - k = k) |
| 445 | + train_set_x=train_set_x, |
| 446 | + batch_size=batch_size, |
| 447 | + k=k) |
446 | 448 |
|
447 | 449 | print '... pre-training the model' |
448 | 450 | start_time = time.clock() |
449 | | - ## Pre-train layer-wise |
| 451 | + ## Pre-train layer-wise |
450 | 452 | for i in xrange(dbn.n_layers): |
451 | | - # go through pretraining epochs |
| 453 | + # go through pretraining epochs |
452 | 454 | for epoch in xrange(pretraining_epochs): |
453 | 455 | # go through the training set |
454 | 456 | c = [] |
455 | 457 | for batch_index in xrange(n_train_batches): |
456 | | - c.append(pretraining_fns[i](index = batch_index, |
457 | | - lr = pretrain_lr ) ) |
| 458 | + c.append(pretraining_fns[i](index=batch_index, |
| 459 | + lr=pretrain_lr)) |
458 | 460 | print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),numpy.mean(c) |
459 | 461 |
|
460 | 462 | end_time = time.clock() |
|