Skip to content

Commit c8fc56d

Browse files
author
Razvan Pascanu
committed
working version of SdA.py
1 parent e495a6c commit c8fc56d

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

code/SdA.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ class dA(object):
135135
136136
"""
137137

138-
def __init__(self, n_visible= 784, n_hidden= 500, input= None):
138+
def __init__(self, n_visible= 784, n_hidden= 500, input= None, corruption_level = 0.1):
139139
"""
140140
Initialize the dA class by specifying the number of visible units (the
141141
dimension d of the input ), the number of hidden units ( the dimension
142142
d' of the latent or hidden space ) and by giving a symbolic variable
143143
for the input. Such a symbolic variable is useful when the input is
144144
the result of some computations. For example when dealing with SdAs,
145-
the dA on layer 2 gets as input the output of the DAE on layer 1.
145+
the dA on layer 2 gets as input the output of the dA on layer 1.
146146
This output can be written as a function of the input to the entire
147147
model, and as such can be computed by theano whenever needed.
148148
@@ -152,6 +152,13 @@ def __init__(self, n_visible= 784, n_hidden= 500, input= None):
152152
153153
:param input: a symbolic description of the input or None
154154
155+
:param corruption_level: the corruption mechanism picks up randomly this fraction
156+
of entries of the input and turns them to 0
157+
158+
159+
amount of entries from the input to 0 from the input, defaul
160+
is 0.1, which means 10% of entries are corrupted to 0
161+
155162
"""
156163
self.n_visible = n_visible
157164
self.n_hidden = n_hidden
@@ -198,8 +205,8 @@ def __init__(self, n_visible= 784, n_hidden= 500, input= None):
198205
# third argument is the probability of success of any trial
199206
#
200207
# this will produce an array of 0s and 1s where 1 has a
201-
# probability of 0.9 and 0 if 0.1
202-
self.tilde_x = theano_rng.binomial( self.x.shape, 1, 0.9) * self.x
208+
# probability of 1 - corruption_level and 0 if corruption_level
209+
self.tilde_x = theano_rng.binomial( self.x.shape, 1, 1 - corruption_level) * self.x
203210
# Equation (2)
204211
# note : y is stored as an attribute of the class so that it can be
205212
# used later when stacking dAs.
@@ -358,7 +365,9 @@ def shared_dataset(data_xy):
358365
# construct the logistic regression class
359366
classifier = SdA( input=x, n_ins=28*28, \
360367
hidden_layers_sizes = [1000, 1000, 1000], n_outs=10)
361-
368+
369+
370+
start_time = time.clock()
362371
## Pre-train layer-wise
363372
for i in xrange(classifier.n_layers):
364373
cost = classifier.layers[i].cost
@@ -387,7 +396,9 @@ def shared_dataset(data_xy):
387396

388397

389398

399+
end_time = time.clock()
390400

401+
print ('Pretraining took %f minutes' %((end_time-start_time)/60.))
391402
# Fine-tune the entire model
392403
# the cost we minimize during training is the negative log likelihood of
393404
# the model
@@ -435,9 +446,9 @@ def shared_dataset(data_xy):
435446

436447
# early-stopping parameters
437448
patience = 10000 # look as this many examples regardless
438-
patience_increase = 2 # wait this much longer when a new best is
449+
patience_increase = 2. # wait this much longer when a new best is
439450
# found
440-
improvement_threshold = 0.995 # a relative improvement of this much is
451+
improvement_threshold = 0.99 # a relative improvement of this much is
441452
# considered significant
442453
validation_frequency = min(n_train_batches, patience/2)
443454
# go through this many
@@ -450,15 +461,19 @@ def shared_dataset(data_xy):
450461
best_validation_loss = float('inf')
451462
test_score = 0.
452463
start_time = time.clock()
453-
cost_ij = []
454-
for epoch in xrange(training_epochs):
464+
465+
done_looping = False
466+
epoch = 0
467+
468+
while (epoch < training_epochs) and (not done_looping):
469+
epoch = epoch + 1
455470
for minibatch_index in xrange(n_train_batches):
456471

457-
cost_ij += [train_model(minibatch_index)]
472+
cost_ij = train_model(minibatch_index)
458473
iter = epoch * n_train_batches + minibatch_index
459474

460475
if (iter+1) % validation_frequency == 0:
461-
cost_ij = []
476+
462477
validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
463478
this_validation_loss = numpy.mean(validation_losses)
464479
print('epoch %i, minibatch %i/%i, validation error %f %%' % \
@@ -488,6 +503,7 @@ def shared_dataset(data_xy):
488503

489504

490505
if patience <= iter :
506+
done_looping = True
491507
break
492508

493509
end_time = time.clock()

code/convolutional_mlp.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@ def shared_dataset(data_xy):
313313
test_score = 0.
314314
start_time = time.clock()
315315

316-
for epoch in xrange(n_epochs):
316+
epoch = 0
317+
done_looping = False
318+
319+
while (epoch < n_epoch) and (not done_looping):
320+
epoch = epoch + 1
317321
for minibatch_index in xrange(n_train_batches):
318322

319323
iter = epoch * n_train_batches + minibatch_index
@@ -353,6 +357,7 @@ def shared_dataset(data_xy):
353357
test_score*100.))
354358

355359
if patience <= iter :
360+
done_looping = False
356361
break
357362

358363
end_time = time.clock()

code/logistic_cg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ def errors(self, y):
136136

137137

138138

139-
def cg_optimization_mnist( n_iter=50, mnist_pkl_gz='mnist.pkl.gz' ):
139+
def cg_optimization_mnist( n_epochs=50, mnist_pkl_gz='mnist.pkl.gz' ):
140140
"""Demonstrate conjugate gradient optimization of a log-linear model
141141
142142
This is demonstrated on MNIST.
143143
144-
:param n_iter: number of iterations ot run the optimizer
144+
:param n_epochs: number of epochs to run the optimizer
145145
146146
:param mnist_pkl_gz: the path of the mnist training file from
147147
http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz
@@ -263,7 +263,7 @@ def callback(theta_value):
263263
fprime=train_fn_grad,
264264
callback=callback,
265265
disp=0,
266-
maxiter=n_iter)
266+
maxiter=n_epochs)
267267
end_time = time.clock()
268268
print(('Optimization complete with best validation score of %f %%, with '
269269
'test performance %f %%') %

code/logistic_sgd.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ def shared_dataset(data_xy):
230230
best_validation_loss = float('inf')
231231
test_score = 0.
232232
start_time = time.clock()
233-
234-
for epoch in xrange(n_epochs):
233+
234+
done_looping = False
235+
epoch = 0
236+
while (epoch < n_epochs) and (not done_looping):
237+
epoch = epoch + 1
235238
for minibatch_index in xrange(n_train_batches):
236239

237240
cost_ij = train_model(minibatch_index)
@@ -266,6 +269,7 @@ def shared_dataset(data_xy):
266269
(epoch, minibatch_index+1, n_train_batches,test_score*100.))
267270

268271
if patience <= iter :
272+
done_looping = True
269273
break
270274

271275
end_time = time.clock()

code/mlp.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ def shared_dataset(data_xy):
265265
test_score = 0.
266266
start_time = time.clock()
267267

268-
for epoch in xrange(n_epochs):
268+
epoch = 0
269+
done_looping = False
270+
271+
while (epoch < n_epochs) and (not done_looping):
272+
epoch = epoch + 1
269273
for minibatch_index in xrange(n_train_batches):
270274

271275
cost_ij = train_model(minibatch_index)
@@ -300,6 +304,7 @@ def shared_dataset(data_xy):
300304
(epoch, minibatch_index+1, n_train_batches,test_score*100.))
301305

302306
if patience <= iter :
307+
done_looping = True
303308
break
304309

305310

code/test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ def test_dbn():
1515
def test_rbm():
1616
raise SkipTest('Implementation not finished')
1717
def test_SdA():
18-
raise SkipTest('Implementation not finished')
1918
SdA.sgd_optimization_mnist(pretraining_epochs = 2, n_epochs = 3)

0 commit comments

Comments
 (0)