Skip to content

Commit a59a2bf

Browse files
author
Razvan Pascanu
committed
merging
1 parent 4e592ce commit a59a2bf

18 files changed

Lines changed: 427 additions & 1219 deletions

code/DBN.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
import os
44

5-
import numpy, time, cPickle, gzip
5+
import numpy, time, cPickle, gzip, os, sys
66

77
import theano
88
import theano.tensor as T
@@ -141,6 +141,7 @@ def pretraining_functions(self, train_set_x, batch_size):
141141
# index to a [mini]batch
142142
index = T.lscalar('index') # index to a minibatch
143143
learning_rate = T.scalar('lr') # learning rate to use
144+
k = T.lscalar('k')
144145

145146
# number of batches
146147
n_batches = train_set_x.value.shape[0] / batch_size
@@ -154,11 +155,12 @@ def pretraining_functions(self, train_set_x, batch_size):
154155

155156
# get the cost and the updates list
156157
# TODO: change cost function to reconstruction error
157-
cost,updates = rbm.cd(learning_rate, persistent=None)
158+
cost,updates = rbm.get_cost_updates(learning_rate, persistent=None, k =k)
158159

159160
# compile the theano function
160161
fn = theano.function(inputs = [index,
161-
theano.Param(learning_rate, default = 0.1)],
162+
theano.Param(learning_rate, default = 0.1),
163+
theano.Param(k, default = 1)],
162164
outputs = cost,
163165
updates = updates,
164166
givens = {self.x :train_set_x[batch_begin:batch_end]})
@@ -229,13 +231,10 @@ def test_score():
229231
return train_fn, valid_score, test_score
230232

231233

232-
233-
234-
235-
236234
def test_DBN( finetune_lr = 0.1, pretraining_epochs = 10, \
237-
pretrain_lr = 0.1, training_epochs = 1000, \
238-
dataset='mnist.pkl.gz'):
235+
pretrain_lr = 0.1, k = 1, training_epochs = 1000, \
236+
dataset='../data/mnist.pkl.gz', batch_size = 1,
237+
output_folder = 'DBN_plots'):
239238
"""
240239
Demonstrates how to train and test a Deep Belief Network.
241240
@@ -253,18 +252,13 @@ def test_DBN( finetune_lr = 0.1, pretraining_epochs = 10, \
253252
:param dataset: path the the pickled dataset
254253
"""
255254

256-
print 'finetune_lr = ', finetune_lr
257-
print 'pretrain_lr = ', pretrain_lr
258255

259256
datasets = load_data(dataset)
260257

261258
train_set_x, train_set_y = datasets[0]
262259
valid_set_x, valid_set_y = datasets[1]
263260
test_set_x , test_set_y = datasets[2]
264261

265-
266-
batch_size = 20 # size of the minibatch
267-
268262
# compute number of minibatches for training, validation and testing
269263
n_train_batches = train_set_x.value.shape[0] / batch_size
270264

@@ -295,7 +289,7 @@ def test_DBN( finetune_lr = 0.1, pretraining_epochs = 10, \
295289
c = []
296290
for batch_index in xrange(n_train_batches):
297291
c.append(pretraining_fns[i](index = batch_index,
298-
lr = pretrain_lr ) )
292+
lr = pretrain_lr, k = k ) )
299293
print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),numpy.mean(c)
300294

301295
end_time = time.clock()

code/SdA.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_score():
281281

282282
def test_SdA( finetune_lr = 0.1, pretraining_epochs = 15, \
283283
pretrain_lr = 0.05, training_epochs = 1000, \
284-
dataset='mnist.pkl.gz'):
284+
dataset='../data/mnist.pkl.gz', batch_size = 1):
285285
"""
286286
Demonstrates how to train and test a stochastic denoising autoencoder.
287287
@@ -312,8 +312,6 @@ def test_SdA( finetune_lr = 0.1, pretraining_epochs = 15, \
312312
test_set_x , test_set_y = datasets[2]
313313

314314

315-
batch_size = 20 # size of the minibatch
316-
317315
# compute number of minibatches for training, validation and testing
318316
n_train_batches = train_set_x.value.shape[0] / batch_size
319317

code/convolutional_mlp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2,2)):
9696

9797

9898

99-
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nkerns=[20,50]):
99+
def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='../data/mnist.pkl.gz',
100+
nkerns=[20,50], batch_size = 500):
100101
""" Demonstrates lenet on MNIST dataset
101102
102103
:type learning_rate: float
@@ -122,8 +123,6 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, dataset='mnist.pkl.gz', nke
122123
test_set_x , test_set_y = datasets[2]
123124

124125

125-
batch_size = 500 # size of the minibatch
126-
127126
# compute number of minibatches for training, validation and testing
128127
n_train_batches = train_set_x.value.shape[0] / batch_size
129128
n_valid_batches = valid_set_x.value.shape[0] / batch_size

code/dA.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ def __init__(self, numpy_rng, theano_rng = None, input = None, n_visible= 784, n
129129
# converted using asarray to dtype
130130
# theano.config.floatX so that the code is runable on GPU
131131
initial_W = numpy.asarray( numpy_rng.uniform(
132-
low = -numpy.sqrt(6./(n_hidden+n_visible)),
133-
high = numpy.sqrt(6./(n_hidden+n_visible)),
132+
low = -4*numpy.sqrt(6./(n_hidden+n_visible)),
133+
high = 4*numpy.sqrt(6./(n_hidden+n_visible)),
134134
size = (n_visible, n_hidden)), dtype = theano.config.floatX)
135135
W = theano.shared(value = initial_W, name ='W')
136136

@@ -219,7 +219,8 @@ def get_cost_updates(self, corruption_level, learning_rate):
219219

220220

221221

222-
def test_dA( learning_rate = 0.1, training_epochs = 15, dataset ='mnist.pkl.gz' ):
222+
def test_dA( learning_rate = 0.1, training_epochs = 15, dataset ='../data/mnist.pkl.gz',
223+
batch_size = 20, output_folder = 'dA_plots' ):
223224

224225
"""
225226
This demo is tested on MNIST
@@ -237,15 +238,17 @@ def test_dA( learning_rate = 0.1, training_epochs = 15, dataset ='mnist.pkl.gz'
237238
datasets = load_data(dataset)
238239
train_set_x, train_set_y = datasets[0]
239240

240-
batch_size = 20 # size of the minibatch
241-
242241
# compute number of minibatches for training, validation and testing
243242
n_train_batches = train_set_x.value.shape[0] / batch_size
244243

245244
# allocate symbolic variables for the data
246245
index = T.lscalar() # index to a [mini]batch
247246
x = T.matrix('x') # the data is presented as rasterized images
248247

248+
249+
if not os.path.isdir(output_folder):
250+
os.makedirs(output_folder)
251+
os.chdir(output_folder)
249252
####################################
250253
# BUILDING THE MODEL NO CORRUPTION #
251254
####################################
@@ -331,6 +334,7 @@ def test_dA( learning_rate = 0.1, training_epochs = 15, dataset ='mnist.pkl.gz'
331334
tile_spacing=(1,1)))
332335
image.save('filters_corruption_30.png')
333336

337+
os.chdir('../')
334338

335339

336340
if __name__ == '__main__':

0 commit comments

Comments
 (0)