Skip to content

Commit 7d867f0

Browse files
committed
Used start-snippet-# and end-snippet-# for precise inclusion of code in ReStructuredText files. Many cosmetic changes to reduce pep8 warnings.
1 parent e3f9cd2 commit 7d867f0

File tree

20 files changed

+564
-301
lines changed

20 files changed

+564
-301
lines changed

code/DBN.py

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from rbm import RBM
1818

1919

20+
# start-snippet-1
2021
class DBN(object):
2122
"""Deep Belief Network
2223
@@ -65,7 +66,7 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
6566
self.x = T.matrix('x') # the data is presented as rasterized images
6667
self.y = T.ivector('y') # the labels are presented as 1D vector
6768
# of [int] labels
68-
69+
# end-snippet-1
6970
# The DBN is an MLP, for which all weights of intermediate
7071
# layers are shared with a different RBM. We will first
7172
# construct the DBN as a deep multilayer perceptron, and when
@@ -174,12 +175,14 @@ def pretraining_functions(self, train_set_x, batch_size, k):
174175
persistent=None, k=k)
175176

176177
# compile the theano function
177-
fn = theano.function(inputs=[index,
178-
theano.Param(learning_rate, default=0.1)],
179-
outputs=cost,
180-
updates=updates,
181-
givens={self.x:
182-
train_set_x[batch_begin:batch_end]})
178+
fn = theano.function(
179+
inputs=[index, theano.Param(learning_rate, default=0.1)],
180+
outputs=cost,
181+
updates=updates,
182+
givens={
183+
self.x: train_set_x[batch_begin:batch_end]
184+
}
185+
)
183186
# append `fn` to the list of functions
184187
pretrain_fns.append(fn)
185188

@@ -224,25 +227,45 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
224227
for param, gparam in zip(self.params, gparams):
225228
updates.append((param, param - gparam * learning_rate))
226229

227-
train_fn = theano.function(inputs=[index],
228-
outputs=self.finetune_cost,
229-
updates=updates,
230-
givens={self.x: train_set_x[index * batch_size:
231-
(index + 1) * batch_size],
232-
self.y: train_set_y[index * batch_size:
233-
(index + 1) * batch_size]})
234-
235-
test_score_i = theano.function([index], self.errors,
236-
givens={self.x: test_set_x[index * batch_size:
237-
(index + 1) * batch_size],
238-
self.y: test_set_y[index * batch_size:
239-
(index + 1) * batch_size]})
240-
241-
valid_score_i = theano.function([index], self.errors,
242-
givens={self.x: valid_set_x[index * batch_size:
243-
(index + 1) * batch_size],
244-
self.y: valid_set_y[index * batch_size:
245-
(index + 1) * batch_size]})
230+
train_fn = theano.function(
231+
inputs=[index],
232+
outputs=self.finetune_cost,
233+
updates=updates,
234+
givens={
235+
self.x: train_set_x[
236+
index * batch_size: (index + 1) * batch_size
237+
],
238+
self.y: train_set_y[
239+
index * batch_size: (index + 1) * batch_size
240+
]
241+
}
242+
)
243+
244+
test_score_i = theano.function(
245+
[index],
246+
self.errors,
247+
givens={
248+
self.x: test_set_x[
249+
index * batch_size: (index + 1) * batch_size
250+
],
251+
self.y: test_set_y[
252+
index * batch_size: (index + 1) * batch_size
253+
]
254+
}
255+
)
256+
257+
valid_score_i = theano.function(
258+
[index],
259+
self.errors,
260+
givens={
261+
self.x: valid_set_x[
262+
index * batch_size: (index + 1) * batch_size
263+
],
264+
self.y: valid_set_y[
265+
index * batch_size: (index + 1) * batch_size
266+
]
267+
}
268+
)
246269

247270
# Create a function that scans the entire validation set
248271
def valid_score():
@@ -296,6 +319,7 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100,
296319
hidden_layers_sizes=[1000, 1000, 1000],
297320
n_outs=10)
298321

322+
# start-snippet-2
299323
#########################
300324
# PRETRAINING THE MODEL #
301325
#########################
@@ -319,21 +343,23 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100,
319343
print numpy.mean(c)
320344

321345
end_time = time.clock()
346+
# end-snippet-2
322347
print >> sys.stderr, ('The pretraining code for file ' +
323348
os.path.split(__file__)[1] +
324349
' ran for %.2fm' % ((end_time - start_time) / 60.))
325-
326350
########################
327351
# FINETUNING THE MODEL #
328352
########################
329353

330354
# get the training, validation and testing function for the model
331355
print '... getting the finetuning functions'
332356
train_fn, validate_model, test_model = dbn.build_finetune_functions(
333-
datasets=datasets, batch_size=batch_size,
334-
learning_rate=finetune_lr)
357+
datasets=datasets,
358+
batch_size=batch_size,
359+
learning_rate=finetune_lr
360+
)
335361

336-
print '... finetunning the model'
362+
print '... finetuning the model'
337363
# early-stopping parameters
338364
patience = 4 * n_train_batches # look as this many examples regardless
339365
patience_increase = 2. # wait this much longer when a new best is
@@ -342,7 +368,7 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100,
342368
# considered significant
343369
validation_frequency = min(n_train_batches, patience / 2)
344370
# go through this many
345-
# minibatche before checking the network
371+
# minibatches before checking the network
346372
# on the validation set; in this case we
347373
# check every epoch
348374

@@ -365,16 +391,24 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100,
365391

366392
validation_losses = validate_model()
367393
this_validation_loss = numpy.mean(validation_losses)
368-
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
369-
(epoch, minibatch_index + 1, n_train_batches,
370-
this_validation_loss * 100.))
394+
print(
395+
'epoch %i, minibatch %i/%i, validation error %f %%'
396+
% (
397+
epoch,
398+
minibatch_index + 1,
399+
n_train_batches,
400+
this_validation_loss * 100.
401+
)
402+
)
371403

372404
# if we got the best validation score until now
373405
if this_validation_loss < best_validation_loss:
374406

375407
#improve patience if loss improvement is good enough
376-
if (this_validation_loss < best_validation_loss *
377-
improvement_threshold):
408+
if (
409+
this_validation_loss < best_validation_loss *
410+
improvement_threshold
411+
):
378412
patience = max(patience, iter * patience_increase)
379413

380414
# save best validation score and iteration number
@@ -394,9 +428,12 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100,
394428
break
395429

396430
end_time = time.clock()
397-
print(('Optimization complete with best validation score of %f %%,'
398-
'with test performance %f %%') %
399-
(best_validation_loss * 100., test_score * 100.))
431+
print(
432+
(
433+
'Optimization complete with best validation score of %f %%,'
434+
'with test performance %f %%'
435+
) % (best_validation_loss * 100., test_score * 100.)
436+
)
400437
print >> sys.stderr, ('The fine tuning code for file ' +
401438
os.path.split(__file__)[1] +
402439
' ran for %.2fm' % ((end_time - start_time)

code/SdA.py

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from dA import dA
4747

4848

49+
# start-snippet-1
4950
class SdA(object):
5051
"""Stacked denoising auto-encoder class (SdA)
5152
@@ -104,6 +105,7 @@ def __init__(
104105
self.x = T.matrix('x') # the data is presented as rasterized images
105106
self.y = T.ivector('y') # the labels are presented as 1D vector of
106107
# [int] labels
108+
# end-snippet-1
107109

108110
# The SdA is an MLP, for which all weights of intermediate layers
109111
# are shared with a different denoising autoencoders
@@ -115,6 +117,7 @@ def __init__(
115117
# During finetunining we will finish training the SdA by doing
116118
# stochastich gradient descent on the MLP
117119

120+
# start-snippet-2
118121
for i in xrange(self.n_layers):
119122
# construct the sigmoidal layer
120123

@@ -157,11 +160,13 @@ def __init__(
157160
W=sigmoid_layer.W,
158161
bhid=sigmoid_layer.b)
159162
self.dA_layers.append(dA_layer)
160-
163+
# end-snippet-2
161164
# We now need to add a logistic layer on top of the MLP
162165
self.logLayer = LogisticRegression(
163-
input=self.sigmoid_layers[-1].output,
164-
n_in=hidden_layers_sizes[-1], n_out=n_outs)
166+
input=self.sigmoid_layers[-1].output,
167+
n_in=hidden_layers_sizes[-1],
168+
n_out=n_outs
169+
)
165170

166171
self.params.extend(self.logLayer.params)
167172
# construct a function that implements one step of finetunining
@@ -210,13 +215,18 @@ def pretraining_functions(self, train_set_x, batch_size):
210215
cost, updates = dA.get_cost_updates(corruption_level,
211216
learning_rate)
212217
# compile the theano function
213-
fn = theano.function(inputs=[index,
214-
theano.Param(corruption_level, default=0.2),
215-
theano.Param(learning_rate, default=0.1)],
216-
outputs=cost,
217-
updates=updates,
218-
givens={self.x: train_set_x[batch_begin:
219-
batch_end]})
218+
fn = theano.function(
219+
inputs=[
220+
index,
221+
theano.Param(corruption_level, default=0.2),
222+
theano.Param(learning_rate, default=0.1)
223+
],
224+
outputs=cost,
225+
updates=updates,
226+
givens={
227+
self.x: train_set_x[batch_begin: batch_end]
228+
}
229+
)
220230
# append `fn` to the list of functions
221231
pretrain_fns.append(fn)
222232

@@ -282,12 +292,12 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
282292
[index],
283293
self.errors,
284294
givens={
285-
self.x: test_set_x[
286-
index * batch_size: (index + 1) * batch_size
287-
],
288-
self.y: test_set_y[
289-
index * batch_size: (index + 1) * batch_size
290-
]
295+
self.x: test_set_x[
296+
index * batch_size: (index + 1) * batch_size
297+
],
298+
self.y: test_set_y[
299+
index * batch_size: (index + 1) * batch_size
300+
]
291301
},
292302
name='test'
293303
)
@@ -354,6 +364,7 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
354364
n_train_batches /= batch_size
355365

356366
# numpy random generator
367+
# start-snippet-3
357368
numpy_rng = numpy.random.RandomState(89677)
358369
print '... building the model'
359370
# construct the stacked denoising autoencoder class
@@ -363,7 +374,7 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
363374
hidden_layers_sizes=[1000, 1000, 1000],
364375
n_outs=10
365376
)
366-
377+
# end-snippet-3 start-snippet-4
367378
#########################
368379
# PRETRAINING THE MODEL #
369380
#########################
@@ -392,16 +403,18 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
392403
print >> sys.stderr, ('The pretraining code for file ' +
393404
os.path.split(__file__)[1] +
394405
' ran for %.2fm' % ((end_time - start_time) / 60.))
395-
406+
# end-snippet-4
396407
########################
397408
# FINETUNING THE MODEL #
398409
########################
399410

400411
# get the training, validation and testing function for the model
401412
print '... getting the finetuning functions'
402413
train_fn, validate_model, test_model = sda.build_finetune_functions(
403-
datasets=datasets, batch_size=batch_size,
404-
learning_rate=finetune_lr)
414+
datasets=datasets,
415+
batch_size=batch_size,
416+
learning_rate=finetune_lr
417+
)
405418

406419
print '... finetunning the model'
407420
# early-stopping parameters
@@ -441,8 +454,10 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
441454
if this_validation_loss < best_validation_loss:
442455

443456
#improve patience if loss improvement is good enough
444-
if (this_validation_loss < best_validation_loss *
445-
improvement_threshold):
457+
if (
458+
this_validation_loss < best_validation_loss *
459+
improvement_threshold
460+
):
446461
patience = max(patience, iter * patience_increase)
447462

448463
# save best validation score and iteration number
@@ -462,9 +477,13 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
462477
break
463478

464479
end_time = time.clock()
465-
print(('Optimization complete with best validation score of %f %%,'
466-
'with test performance %f %%') %
467-
(best_validation_loss * 100., test_score * 100.))
480+
print(
481+
(
482+
'Optimization complete with best validation score of %f %%,'
483+
'with test performance %f %%'
484+
)
485+
% (best_validation_loss * 100., test_score * 100.)
486+
)
468487
print >> sys.stderr, ('The training code for file ' +
469488
os.path.split(__file__)[1] +
470489
' ran for %.2fm' % ((end_time - start_time) / 60.))

code/cA.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,14 @@ def __init__(self, numpy_rng, input=None, n_visible=784, n_hidden=100,
131131
# 4*sqrt(6./(n_hidden+n_visible))the output of uniform if
132132
# converted using asarray to dtype
133133
# theano.config.floatX so that the code is runable on GPU
134-
initial_W = numpy.asarray(numpy_rng.uniform(
135-
low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
136-
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
137-
size=(n_visible, n_hidden)),
138-
dtype=theano.config.floatX)
134+
initial_W = numpy.asarray(
135+
numpy_rng.uniform(
136+
low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
137+
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
138+
size=(n_visible, n_hidden)
139+
),
140+
dtype=theano.config.floatX
141+
)
139142
W = theano.shared(value=initial_W, name='W', borrow=True)
140143

141144
if not bvis:
@@ -186,7 +189,7 @@ def get_reconstructed_input(self, hidden):
186189
hidden layer
187190
188191
"""
189-
return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
192+
return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
190193

191194
def get_cost_updates(self, contraction_level, learning_rate):
192195
""" This function computes the cost and the updates for one trainng
@@ -265,10 +268,14 @@ def test_cA(learning_rate=0.01, training_epochs=20,
265268
cost, updates = ca.get_cost_updates(contraction_level=contraction_level,
266269
learning_rate=learning_rate)
267270

268-
train_ca = theano.function([index], [T.mean(ca.L_rec), ca.L_jacob],
269-
updates=updates,
270-
givens={x: train_set_x[index * batch_size:
271-
(index + 1) * batch_size]})
271+
train_ca = theano.function(
272+
[index],
273+
[T.mean(ca.L_rec), ca.L_jacob],
274+
updates=updates,
275+
givens={
276+
x: train_set_x[index * batch_size: (index + 1) * batch_size]
277+
}
278+
)
272279

273280
start_time = time.clock()
274281

0 commit comments

Comments
 (0)