Skip to content

Commit 2c022d1

Browse files
committed
fixed everything except rnnrbm and rnnslu, partial tests run but not to completion
1 parent d2764f2 commit 2c022d1

File tree

11 files changed

+169
-130
lines changed

11 files changed

+169
-130
lines changed

code/SdA.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
Systems 19, 2007
3030
3131
"""
32+
33+
from __future__ import print_function
34+
3235
import os
3336
import sys
3437
import timeit
@@ -116,7 +119,7 @@ def __init__(
116119
# stochastich gradient descent on the MLP
117120

118121
# start-snippet-2
119-
for i in xrange(self.n_layers):
122+
for i in range(self.n_layers):
120123
# construct the sigmoidal layer
121124

122125
# the size of the input is either the number of hidden units of
@@ -254,9 +257,9 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
254257

255258
# compute number of minibatches for training, validation and testing
256259
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
257-
n_valid_batches /= batch_size
260+
n_valid_batches //= batch_size
258261
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
259-
n_test_batches /= batch_size
262+
n_test_batches //= batch_size
260263

261264
index = T.lscalar('index') # index to a [mini]batch
262265

@@ -314,11 +317,11 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
314317

315318
# Create a function that scans the entire validation set
316319
def valid_score():
317-
return [valid_score_i(i) for i in xrange(n_valid_batches)]
320+
return [valid_score_i(i) for i in range(n_valid_batches)]
318321

319322
# Create a function that scans the entire test set
320323
def test_score():
321-
return [test_score_i(i) for i in xrange(n_test_batches)]
324+
return [test_score_i(i) for i in range(n_test_batches)]
322325

323326
return train_fn, valid_score, test_score
324327

@@ -357,12 +360,12 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
357360

358361
# compute number of minibatches for training, validation and testing
359362
n_train_batches = train_set_x.get_value(borrow=True).shape[0]
360-
n_train_batches /= batch_size
363+
n_train_batches //= batch_size
361364

362365
# numpy random generator
363366
# start-snippet-3
364367
numpy_rng = numpy.random.RandomState(89677)
365-
print '... building the model'
368+
print('... building the model')
366369
# construct the stacked denoising autoencoder class
367370
sda = SdA(
368371
numpy_rng=numpy_rng,
@@ -374,52 +377,52 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
374377
#########################
375378
# PRETRAINING THE MODEL #
376379
#########################
377-
print '... getting the pretraining functions'
380+
print('... getting the pretraining functions')
378381
pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x,
379382
batch_size=batch_size)
380383

381-
print '... pre-training the model'
384+
print('... pre-training the model')
382385
start_time = timeit.default_timer()
383386
## Pre-train layer-wise
384387
corruption_levels = [.1, .2, .3]
385-
for i in xrange(sda.n_layers):
388+
for i in range(sda.n_layers):
386389
# go through pretraining epochs
387-
for epoch in xrange(pretraining_epochs):
390+
for epoch in range(pretraining_epochs):
388391
# go through the training set
389392
c = []
390-
for batch_index in xrange(n_train_batches):
393+
for batch_index in range(n_train_batches):
391394
c.append(pretraining_fns[i](index=batch_index,
392395
corruption=corruption_levels[i],
393396
lr=pretrain_lr))
394-
print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch),
395-
print numpy.mean(c)
397+
print('Pre-training layer %i, epoch %d, cost ' % (i, epoch))
398+
print(numpy.mean(c))
396399

397400
end_time = timeit.default_timer()
398401

399-
print >> sys.stderr, ('The pretraining code for file ' +
400-
os.path.split(__file__)[1] +
401-
' ran for %.2fm' % ((end_time - start_time) / 60.))
402+
print(('The pretraining code for file ' +
403+
os.path.split(__file__)[1] +
404+
' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr)
402405
# end-snippet-4
403406
########################
404407
# FINETUNING THE MODEL #
405408
########################
406409

407410
# get the training, validation and testing function for the model
408-
print '... getting the finetuning functions'
411+
print('... getting the finetuning functions')
409412
train_fn, validate_model, test_model = sda.build_finetune_functions(
410413
datasets=datasets,
411414
batch_size=batch_size,
412415
learning_rate=finetune_lr
413416
)
414417

415-
print '... finetunning the model'
418+
print('... finetunning the model')
416419
# early-stopping parameters
417420
patience = 10 * n_train_batches # look as this many examples regardless
418421
patience_increase = 2. # wait this much longer when a new best is
419422
# found
420423
improvement_threshold = 0.995 # a relative improvement of this much is
421424
# considered significant
422-
validation_frequency = min(n_train_batches, patience / 2)
425+
validation_frequency = min(n_train_batches, patience // 2)
423426
# go through this many
424427
# minibatche before checking the network
425428
# on the validation set; in this case we
@@ -434,7 +437,7 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
434437

435438
while (epoch < training_epochs) and (not done_looping):
436439
epoch = epoch + 1
437-
for minibatch_index in xrange(n_train_batches):
440+
for minibatch_index in range(n_train_batches):
438441
minibatch_avg_cost = train_fn(minibatch_index)
439442
iter = (epoch - 1) * n_train_batches + minibatch_index
440443

@@ -480,9 +483,9 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
480483
)
481484
% (best_validation_loss * 100., best_iter + 1, test_score * 100.)
482485
)
483-
print >> sys.stderr, ('The training code for file ' +
484-
os.path.split(__file__)[1] +
485-
' ran for %.2fm' % ((end_time - start_time) / 60.))
486+
print(('The training code for file ' +
487+
os.path.split(__file__)[1] +
488+
' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr)
486489

487490

488491
if __name__ == '__main__':

code/cA.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
Systems 19, 2007
2929
3030
"""
31+
32+
from __future__ import print_function
33+
from six.moves import xrange
34+
3135
import os
3236
import sys
3337
import timeit
@@ -205,7 +209,7 @@ def get_cost_updates(self, contraction_level, learning_rate):
205209
axis=1)
206210

207211
# Compute the jacobian and average over the number of samples/minibatch
208-
self.L_jacob = T.sum(J ** 2) / self.n_batchsize
212+
self.L_jacob = T.sum(J ** 2) // self.n_batchsize
209213

210214
# note : L is now a vector, where each element is the
211215
# cross-entropy cost of the reconstruction of the
@@ -246,7 +250,7 @@ def test_cA(learning_rate=0.01, training_epochs=20,
246250
train_set_x, train_set_y = datasets[0]
247251

248252
# compute number of minibatches for training, validation and testing
249-
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
253+
n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size
250254

251255
# allocate symbolic variables for the data
252256
index = T.lscalar() # index to a [mini]batch
@@ -290,15 +294,15 @@ def test_cA(learning_rate=0.01, training_epochs=20,
290294
c.append(train_ca(batch_index))
291295

292296
c_array = numpy.vstack(c)
293-
print 'Training epoch %d, reconstruction cost ' % epoch, numpy.mean(
294-
c_array[0]), ' jacobian norm ', numpy.mean(numpy.sqrt(c_array[1]))
297+
print('Training epoch %d, reconstruction cost ' % epoch, numpy.mean(
298+
c_array[0]), ' jacobian norm ', numpy.mean(numpy.sqrt(c_array[1])))
295299

296300
end_time = timeit.default_timer()
297301

298302
training_time = (end_time - start_time)
299303

300-
print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] +
301-
' ran for %.2fm' % ((training_time) / 60.))
304+
print(('The code for file ' + os.path.split(__file__)[1] +
305+
' ran for %.2fm' % ((training_time) / 60.)), file=sys.stderr)
302306
image = Image.fromarray(tile_raster_images(
303307
X=ca.W.get_value(borrow=True).T,
304308
img_shape=(28, 28), tile_shape=(10, 10),

code/convolutional_mlp.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf
2222
2323
"""
24+
25+
from __future__ import print_function
26+
2427
import os
2528
import sys
2629
import timeit
@@ -70,7 +73,7 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
7073
# each unit in the lower layer receives a gradient from:
7174
# "num output feature maps * filter height * filter width" /
7275
# pooling size
73-
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
76+
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) //
7477
numpy.prod(poolsize))
7578
# initialize weights with random weights
7679
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
@@ -145,9 +148,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
145148
n_train_batches = train_set_x.get_value(borrow=True).shape[0]
146149
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
147150
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
148-
n_train_batches /= batch_size
149-
n_valid_batches /= batch_size
150-
n_test_batches /= batch_size
151+
n_train_batches //= batch_size
152+
n_valid_batches //= batch_size
153+
n_test_batches //= batch_size
151154

152155
# allocate symbolic variables for the data
153156
index = T.lscalar() # index to a [mini]batch
@@ -160,7 +163,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
160163
######################
161164
# BUILD ACTUAL MODEL #
162165
######################
163-
print '... building the model'
166+
print('... building the model')
164167

165168
# Reshape matrix of rasterized images of shape (batch_size, 28 * 28)
166169
# to a 4D tensor, compatible with our LeNetConvPoolLayer
@@ -261,14 +264,14 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
261264
###############
262265
# TRAIN MODEL #
263266
###############
264-
print '... training'
267+
print('... training')
265268
# early-stopping parameters
266269
patience = 10000 # look as this many examples regardless
267270
patience_increase = 2 # wait this much longer when a new best is
268271
# found
269272
improvement_threshold = 0.995 # a relative improvement of this much is
270273
# considered significant
271-
validation_frequency = min(n_train_batches, patience / 2)
274+
validation_frequency = min(n_train_batches, patience // 2)
272275
# go through this many
273276
# minibatche before checking the network
274277
# on the validation set; in this case we
@@ -284,19 +287,19 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
284287

285288
while (epoch < n_epochs) and (not done_looping):
286289
epoch = epoch + 1
287-
for minibatch_index in xrange(n_train_batches):
290+
for minibatch_index in range(n_train_batches):
288291

289292
iter = (epoch - 1) * n_train_batches + minibatch_index
290293

291294
if iter % 100 == 0:
292-
print 'training @ iter = ', iter
295+
print('training @ iter = ', iter)
293296
cost_ij = train_model(minibatch_index)
294297

295298
if (iter + 1) % validation_frequency == 0:
296299

297300
# compute zero-one loss on validation set
298301
validation_losses = [validate_model(i) for i
299-
in xrange(n_valid_batches)]
302+
in range(n_valid_batches)]
300303
this_validation_loss = numpy.mean(validation_losses)
301304
print('epoch %i, minibatch %i/%i, validation error %f %%' %
302305
(epoch, minibatch_index + 1, n_train_batches,
@@ -317,7 +320,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
317320
# test it on the test set
318321
test_losses = [
319322
test_model(i)
320-
for i in xrange(n_test_batches)
323+
for i in range(n_test_batches)
321324
]
322325
test_score = numpy.mean(test_losses)
323326
print((' epoch %i, minibatch %i/%i, test error of '
@@ -334,9 +337,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
334337
print('Best validation score of %f %% obtained at iteration %i, '
335338
'with test performance %f %%' %
336339
(best_validation_loss * 100., best_iter + 1, test_score * 100.))
337-
print >> sys.stderr, ('The code for file ' +
338-
os.path.split(__file__)[1] +
339-
' ran for %.2fm' % ((end_time - start_time) / 60.))
340+
print(('The code for file ' +
341+
os.path.split(__file__)[1] +
342+
' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr)
340343

341344
if __name__ == '__main__':
342345
evaluate_lenet5()

code/dA.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
3131
"""
3232

33+
from __future__ import print_function
34+
3335
import os
3436
import sys
3537
import timeit
@@ -280,7 +282,7 @@ def test_dA(learning_rate=0.1, training_epochs=15,
280282
train_set_x, train_set_y = datasets[0]
281283

282284
# compute number of minibatches for training, validation and testing
283-
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
285+
n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size
284286

285287
# start-snippet-2
286288
# allocate symbolic variables for the data
@@ -328,21 +330,21 @@ def test_dA(learning_rate=0.1, training_epochs=15,
328330
############
329331

330332
# go through training epochs
331-
for epoch in xrange(training_epochs):
333+
for epoch in range(training_epochs):
332334
# go through trainng set
333335
c = []
334-
for batch_index in xrange(n_train_batches):
336+
for batch_index in range(n_train_batches):
335337
c.append(train_da(batch_index))
336338

337-
print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
339+
print('Training epoch %d, cost ' % epoch, numpy.mean(c))
338340

339341
end_time = timeit.default_timer()
340342

341343
training_time = (end_time - start_time)
342344

343-
print >> sys.stderr, ('The no corruption code for file ' +
344-
os.path.split(__file__)[1] +
345-
' ran for %.2fm' % ((training_time) / 60.))
345+
print(('The no corruption code for file ' +
346+
os.path.split(__file__)[1] +
347+
' ran for %.2fm' % ((training_time) / 60.)), file=sys.stderr)
346348
image = Image.fromarray(
347349
tile_raster_images(X=da.W.get_value(borrow=True).T,
348350
img_shape=(28, 28), tile_shape=(10, 10),
@@ -386,21 +388,21 @@ def test_dA(learning_rate=0.1, training_epochs=15,
386388
############
387389

388390
# go through training epochs
389-
for epoch in xrange(training_epochs):
391+
for epoch in range(training_epochs):
390392
# go through trainng set
391393
c = []
392-
for batch_index in xrange(n_train_batches):
394+
for batch_index in range(n_train_batches):
393395
c.append(train_da(batch_index))
394396

395-
print 'Training epoch %d, cost ' % epoch, numpy.mean(c)
397+
print('Training epoch %d, cost ' % epoch, numpy.mean(c))
396398

397399
end_time = timeit.default_timer()
398400

399401
training_time = (end_time - start_time)
400402

401-
print >> sys.stderr, ('The 30% corruption code for file ' +
402-
os.path.split(__file__)[1] +
403-
' ran for %.2fm' % (training_time / 60.))
403+
print(('The 30% corruption code for file ' +
404+
os.path.split(__file__)[1] +
405+
' ran for %.2fm' % (training_time / 60.)), file=sys.stderr)
404406
# end-snippet-3
405407

406408
# start-snippet-4

code/hmc/hmc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ def leapfrog(pos, vel, step):
128128
rval2: dictionary
129129
Dictionary of updates for the Scan Op
130130
"""
131-
# from pos(t) and vel(t-stepsize/2), compute vel(t+stepsize/2)
131+
# from pos(t) and vel(t-stepsize//2), compute vel(t+stepsize//2)
132132
dE_dpos = TT.grad(energy_fn(pos).sum(), pos)
133133
new_vel = vel - step * dE_dpos
134-
# from vel(t+stepsize/2) compute pos(t+stepsize)
134+
# from vel(t+stepsize//2) compute pos(t+stepsize)
135135
new_pos = pos + step * new_vel
136136
return [new_pos, new_vel], {}
137137

138-
# compute velocity at time-step: t + stepsize/2
138+
# compute velocity at time-step: t + stepsize//2
139139
initial_energy = energy_fn(initial_pos)
140140
dE_dpos = TT.grad(initial_energy.sum(), initial_pos)
141141
vel_half_step = initial_vel - 0.5 * stepsize * dE_dpos

0 commit comments

Comments
 (0)