@@ -77,9 +77,9 @@ representations of intermediate layers of the MLP.
7777
7878 class SdA(object):
7979
80- def __init__(self, numpy_rng, theano_rng = None, n_ins = 784,
81- hidden_layers_sizes = [500,500], n_outs = 10,
82- corruption_levels = [0.1, 0.1]):
80+ def __init__(self, numpy_rng, theano_rng= None, n_ins= 784,
81+ hidden_layers_sizes= [500, 500], n_outs= 10,
82+ corruption_levels= [0.1, 0.1]):
8383 """ This class is made to support a variable number of layers.
8484
8585 :type numpy_rng: numpy.random.RandomState
@@ -106,18 +106,18 @@ representations of intermediate layers of the MLP.
106106 """
107107
108108 self.sigmoid_layers = []
109- self.dA_layers = []
110- self.params = []
111- self.n_layers = len(hidden_layers_sizes)
109+ self.dA_layers = []
110+ self.params = []
111+ self.n_layers = len(hidden_layers_sizes)
112112
113113 assert self.n_layers > 0
114114
115115 if not theano_rng:
116- theano_rng = RandomStreams(numpy_rng.randint(2** 30))
116+ theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
117117 # allocate symbolic variables for the data
118- self.x = T.matrix('x') # the data is presented as rasterized images
119- self.y = T.ivector('y') # the labels are presented as 1D vector of
120- # [int] labels
118+ self.x = T.matrix('x') # the data is presented as rasterized images
119+ self.y = T.ivector('y') # the labels are presented as 1D vector of
120+ # [int] labels
121121
122122
123123
@@ -136,30 +136,31 @@ bias of the encoding part with its corresponding sigmoid layer.
136136
137137.. code-block:: python
138138
139- for i in xrange( self.n_layers ):
139+ for i in xrange(self.n_layers):
140140 # construct the sigmoidal layer
141141
142142 # the size of the input is either the number of hidden units of
143143 # the layer below or the input size if we are on the first layer
144- if i == 0 :
144+ if i == 0:
145145 input_size = n_ins
146146 else:
147- input_size = hidden_layers_sizes[i- 1]
147+ input_size = hidden_layers_sizes[i - 1]
148148
149149 # the input to this layer is either the activation of the hidden
150150 # layer below or the input of the SdA if you are on the first
151151 # layer
152- if i == 0 :
152+ if i == 0:
153153 layer_input = self.x
154154 else:
155155 layer_input = self.sigmoid_layers[-1].output
156156
157- sigmoid_layer = SigmoidalLayer(rng = rng,
158- input = layer_input,
159- n_in = input_size,
160- n_out = hidden_layers_sizes[i] )
157+ sigmoid_layer = SigmoidalLayer(rng= rng,
158+ input= layer_input,
159+ n_in= input_size,
160+ n_out= hidden_layers_sizes[i])
161161 # add the layer to our list of layers
162162 self.sigmoid_layers.append(sigmoid_layer)
163+
163164 # its arguably a philosophical question...
164165 # but we are going to only declare that the parameters of the
165166 # sigmoid_layers are parameters of the StackedDAA
@@ -169,11 +170,12 @@ bias of the encoding part with its corresponding sigmoid layer.
169170
170171 # Construct a denoising autoencoder that shared weights with this
171172 # layer
172- dA_layer = dA(rng = rng, trng = trng, input = layer_input,
173- n_visible = input_size,
174- n_hidden = hidden_layers_sizes[i],
175- corruption_level = corruption_levels[0],
176- W = sigmoid_layer.W, bhid = sigmoid_layer.b)
173+ dA_layer = dA(rng=rng, trng=trng, input=layer_input,
174+ n_visible=input_size,
175+ n_hidden=hidden_layers_sizes[i],
176+ corruption_level=corruption_levels[0],
177+ W=sigmoid_layer.W,
178+ bhid=sigmoid_layer.b)
177179 self.dA_layers.append(dA_layer)
178180
179181
@@ -184,9 +186,9 @@ use the ``LogisticRegression`` class introduced in :ref:`logreg`.
184186.. code-block:: python
185187
186188 # We now need to add a logistic layer on top of the MLP
187- self.logLayer = LogisticRegression(\
188- input = self.sigmoid_layers[-1].output,\
189- n_in = hidden_layers_sizes[-1], n_out = n_outs)
189+ self.logLayer = LogisticRegression(
190+ input= self.sigmoid_layers[-1].output,
191+ n_in= hidden_layers_sizes[-1], n_out= n_outs)
190192
191193 self.params.extend(self.logLayer.params)
192194 # construct a function that implements one step of finetunining
@@ -228,33 +230,33 @@ implements one step of training the ``dA`` correspoinding to layer
228230 '''
229231
230232 # index to a [mini]batch
231- index = T.lscalar('index') # index to a minibatch
233+ index = T.lscalar('index') # index to a minibatch
232234
233235In order to be able to change the corruption level or the learning rate
234236during training we associate a Theano variable to them.
235237
236238.. code-block:: python
237239
238- corruption_level = T.scalar('corruption') # amount of corruption to use
239- learning_rate = T.scalar('lr') # learning rate to use
240+ corruption_level = T.scalar('corruption') # amount of corruption to use
241+ learning_rate = T.scalar('lr') # learning rate to use
240242 # number of batches
241243 n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
242244 # begining of a batch, given `index`
243245 batch_begin = index * batch_size
244246 # ending of a batch given `index`
245- batch_end = batch_begin+ batch_size
247+ batch_end = batch_begin + batch_size
246248
247249 pretrain_fns = []
248250 for dA in self.dA_layers:
249251 # get the cost and the updates list
250- cost,updates = dA.get_cost_updates( corruption_level, learning_rate)
252+ cost,updates = dA.get_cost_updates(corruption_level, learning_rate)
251253 # compile the theano function
252- fn = theano.function( inputs = [index,
253- theano.Param(corruption_level, default = 0.2),
254- theano.Param(learning_rate, default = 0.1)],
255- outputs = cost,
256- updates = updates,
257- givens = {self.x : train_set_x[batch_begin:batch_end]})
254+ fn = theano.function(inputs= [index,
255+ theano.Param(corruption_level, default= 0.2),
256+ theano.Param(learning_rate, default= 0.1)],
257+ outputs= cost,
258+ updates= updates,
259+ givens= {self.x: train_set_x[batch_begin:batch_end]})
258260 # append `fn` to the list of functions
259261 pretrain_fns.append(fn)
260262
@@ -295,38 +297,38 @@ during finetuning ( a ``train_model``, a ``validate_model`` and a
295297
296298 (train_set_x, train_set_y) = datasets[0]
297299 (valid_set_x, valid_set_y) = datasets[1]
298- (test_set_x , test_set_y ) = datasets[2]
300+ (test_set_x, test_set_y) = datasets[2]
299301
300302 # compute number of minibatches for training, validation and testing
301303 n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] / batch_size
302- n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size
304+ n_test_batches = test_set_x.get_value(borrow=True).shape[0] / batch_size
303305
304- index = T.lscalar('index') # index to a [mini]batch
306+ index = T.lscalar('index') # index to a [mini]batch
305307
306308 # compute the gradients with respect to the model parameters
307309 gparams = T.grad(self.finetune_cost, self.params)
308310
309311 # compute list of fine-tuning updates
310312 updates = {}
311313 for param, gparam in zip(self.params, gparams):
312- updates[param] = param - gparam* learning_rate
314+ updates[param] = param - gparam * learning_rate
313315
314- train_fn = theano.function(inputs = [index],
315- outputs = self.finetune_cost,
316- updates = updates,
317- givens = {
318- self.x : train_set_x[index* batch_size:(index+1)* batch_size],
319- self.y : train_set_y[index* batch_size:(index+1)* batch_size]})
316+ train_fn = theano.function(inputs= [index],
317+ outputs= self.finetune_cost,
318+ updates= updates,
319+ givens= {
320+ self.x: train_set_x[index * batch_size: (index + 1) * batch_size],
321+ self.y: train_set_y[index * batch_size: (index + 1) * batch_size]})
320322
321323 test_score_i = theano.function([index], self.errors,
322- givens = {
323- self.x: test_set_x[index* batch_size:(index+1)* batch_size],
324- self.y: test_set_y[index* batch_size:(index+1)* batch_size]})
324+ givens= {
325+ self.x: test_set_x[index * batch_size: (index+1) * batch_size],
326+ self.y: test_set_y[index * batch_size: (index+1) * batch_size]})
325327
326328 valid_score_i = theano.function([index], self.errors,
327- givens = {
328- self.x: valid_set_x[index* batch_size:(index+1)* batch_size],
329- self.y: valid_set_y[index* batch_size:(index+1)* batch_size]})
329+ givens= {
330+ self.x: valid_set_x[index * batch_size: (index + 1) * batch_size],
331+ self.y: valid_set_y[index * batch_size: (index + 1) * batch_size]})
330332
331333 # Create a function that scans the entire validation set
332334 def valid_score():
@@ -359,9 +361,9 @@ autoencoder :
359361 numpy_rng = numpy.random.RandomState(123)
360362 print '... building the model'
361363 # construct the stacked denoising autoencoder class
362- sda = SdA( numpy_rng = numpy_rng, n_ins = 28* 28,
363- hidden_layers_sizes = [100,100,100],
364- n_outs = 10)
364+ sda = SdA(numpy_rng= numpy_rng, n_ins=28 * 28,
365+ hidden_layers_sizes= [100, 100, 100],
366+ n_outs= 10)
365367
366368
367369
@@ -382,26 +384,26 @@ to the training set for a fixed number of epochs given by
382384 # PRETRAINING THE MODEL #
383385 #########################
384386 print '... getting the pretraining functions'
385- pretraining_fns = sda.pretraining_functions(
386- train_set_x = train_set_x,
387- batch_size = batch_size )
387+ pretraining_fns = sda.pretraining_functions(
388+ train_set_x= train_set_x,
389+ batch_size= batch_size)
388390
389391 print '... pre-training the model'
390- start_time = time.clock()
391- ## Pre-train layer-wise
392+ start_time = time.clock()
393+ ## Pre-train layer-wise
392394 for i in xrange(sda.n_layers):
393- # go through pretraining epochs
395+ # go through pretraining epochs
394396 for epoch in xrange(pretraining_epochs):
395397 # go through the training set
396398 c = []
397399 for batch_index in xrange(n_train_batches):
398- c.append( pretraining_fns[i](index = batch_index,
399- corruption = 0.2, lr = pretrain_lr ) )
400- print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),numpy.mean(c)
400+ c.append( pretraining_fns[i](index= batch_index,
401+ corruption= 0.2, lr= pretrain_lr ) )
402+ print 'Pre-training layer %i, epoch %d, cost '%(i,epoch), numpy.mean(c)
401403
402404 end_time = time.clock()
403405
404- print ('Pretraining took %f minutes' %((end_time- start_time)/ 60.))
406+ print ('Pretraining took %f minutes' %((end_time - start_time) / 60.))
405407
406408
407409
0 commit comments