@@ -146,42 +146,42 @@ classification.
146146
147147 class DBN(object):
148148
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):
151151 """This class is made to support a variable number of layers.
152152
153153 :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
155155 weights
156156
157157 :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
159159 generated based on a seed drawn from `rng`
160160
161161 :type n_ins: int
162162 :param n_ins: dimension of the input to the DBN
163163
164164 :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
166166 at least one value
167167
168168 :type n_outs: int
169169 :param n_outs: dimension of the output of the network
170170 """
171171
172172 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)
176176
177177 assert self.n_layers > 0
178178
179179 if not theano_rng:
180- theano_rng = RandomStreams(numpy_rng.randint(2** 30))
180+ theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
181181
182182 # 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
185185 # [int] labels
186186
187187``self.sigmoid_layers`` will store the feed-forward graphs which together form
@@ -199,28 +199,28 @@ hidden bias with its corresponding sigmoid layer.
199199
200200.. code-block:: python
201201
202- for i in xrange( self.n_layers ):
202+ for i in xrange(self.n_layers):
203203 # construct the sigmoidal layer
204204
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:
208208 input_size = n_ins
209209 else:
210- input_size = hidden_layers_sizes[i- 1]
210+ input_size = hidden_layers_sizes[i - 1]
211211
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:
215215 layer_input = self.x
216216 else:
217217 layer_input = self.sigmoid_layers[-1].output
218218
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)
224224
225225 # add the layer to our list of layers
226226 self.sigmoid_layers.append(sigmoid_layer)
@@ -231,13 +231,14 @@ hidden bias with its corresponding sigmoid layer.
231231 self.params.extend(sigmoid_layer.params)
232232
233233 # 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)
241242
242243
243244All 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
247248.. code-block:: python
248249
249250 # 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)
253254 self.params.extend(self.logLayer.params)
254255
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
258259 self.finetune_cost = self.logLayer.negative_log_likelihood(self.y)
259260
260261 # 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
284285 '''
285286
286287 # index to a [mini]batch
287- index = T.lscalar('index') # index to a minibatch
288+ index = T.lscalar('index') # index to a minibatch
288289
289290In order to be able to change the learning rate during training, we associate a
290291Theano variable to it that has a default value.
291292
292293.. code-block:: python
293294
294- learning_rate = T.scalar('lr') # learning rate to use
295+ learning_rate = T.scalar('lr') # learning rate to use
295296
296297 # number of batches
297298 n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
298299 # begining of a batch, given `index`
299300 batch_begin = index * batch_size
300301 # ending of a batch given `index`
301- batch_end = batch_begin+ batch_size
302+ batch_end = batch_begin + batch_size
302303
303304 pretrain_fns = []
304305 for rbm in self.rbm_layers:
@@ -311,13 +312,14 @@ Theano variable to it that has a default value.
311312 # compile the Theano function; check if k is also a Theano
312313 # variable, if so added to the inputs of the function
313314 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]
315316 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]})
321323 # append `fn` to the list of functions
322324 pretrain_fns.append(fn)
323325
@@ -355,7 +357,7 @@ and a ``test_model`` function).
355357
356358 (train_set_x, train_set_y) = datasets[0]
357359 (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]
359361
360362 # compute number of minibatches for training, validation and testing
361363 n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
@@ -371,22 +373,22 @@ and a ``test_model`` function).
371373 for param, gparam in zip(self.params, gparams):
372374 updates[param] = param - gparam*learning_rate
373375
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]})
380382
381383 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]})
385387
386388 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]})
390392
391393 # Create a function that scans the entire validation set
392394 def valid_score():
@@ -415,9 +417,9 @@ The few lines of code below constructs the deep belief network :
415417 numpy_rng = numpy.random.RandomState(123)
416418 print '... building the model'
417419 # 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)
421423
422424
423425
@@ -440,21 +442,21 @@ given by ``pretraining_epochs``.
440442 print '... getting the pretraining functions'
441443 # We are using CD-1 here
442444 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)
446448
447449 print '... pre-training the model'
448450 start_time = time.clock()
449- ## Pre-train layer-wise
451+ ## Pre-train layer-wise
450452 for i in xrange(dbn.n_layers):
451- # go through pretraining epochs
453+ # go through pretraining epochs
452454 for epoch in xrange(pretraining_epochs):
453455 # go through the training set
454456 c = []
455457 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) )
458460 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),numpy.mean(c)
459461
460462 end_time = time.clock()
0 commit comments