GeezerVison is showing github.com — Merge pull request #132 from gyom/master · coii/DeepLearningTutorials@1bb3230
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .travis.yml +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,8 +4,8 @@ sudo: false | 4 | 4 |
| 5 | 5 | language: python | 6 | 6 | #python: | 7 | | -# - "2.7" | 8 | | -# - "3.2" | | 7 | +# - "2.6" | | 8 | +# - "3.3" | 9 | 9 | # command to install dependencies | 10 | 10 | before_install: | 11 | 11 | - wget http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh | |
Collapse file Copy file name to clipboardExpand all lines: code/DBN.py +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,7 +75,7 @@ def __init__(self, numpy_rng, theano_rng=None, n_ins=784, | 75 | 75 | # training the DBN by doing stochastic gradient descent on the | 76 | 76 | # MLP. | 77 | 77 |
| 78 | | - for i in xrange(self.n_layers): | | 78 | + for i in range(self.n_layers): | 79 | 79 | # construct the sigmoidal layer | 80 | 80 |
| 81 | 81 | # the size of the input is either the number of hidden | @@ -267,11 +267,11 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 267 | 267 |
| 268 | 268 | # Create a function that scans the entire validation set | 269 | 269 | def valid_score(): | 270 | | - return [valid_score_i(i) for i in xrange(n_valid_batches)] | | 270 | + return [valid_score_i(i) for i in range(n_valid_batches)] | 271 | 271 |
| 272 | 272 | # Create a function that scans the entire test set | 273 | 273 | def test_score(): | 274 | | - return [test_score_i(i) for i in xrange(n_test_batches)] | | 274 | + return [test_score_i(i) for i in range(n_test_batches)] | 275 | 275 |
| 276 | 276 | return train_fn, valid_score, test_score | 277 | 277 |
| @@ -329,12 +329,12 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100, | 329 | 329 | print '... pre-training the model' | 330 | 330 | start_time = timeit.default_timer() | 331 | 331 | ## Pre-train layer-wise | 332 | | - for i in xrange(dbn.n_layers): | | 332 | + for i in range(dbn.n_layers): | 333 | 333 | # go through pretraining epochs | 334 | | - for epoch in xrange(pretraining_epochs): | | 334 | + for epoch in range(pretraining_epochs): | 335 | 335 | # go through the training set | 336 | 336 | c = [] | 337 | | - for batch_index in xrange(n_train_batches): | | 337 | + for batch_index in range(n_train_batches): | 338 | 338 | c.append(pretraining_fns[i](index=batch_index, | 339 | 339 | lr=pretrain_lr)) | 340 | 340 | print 'Pre-training layer %i, epoch %d, cost ' % (i, epoch), | @@ -379,7 +379,7 @@ def test_DBN(finetune_lr=0.1, pretraining_epochs=100, | 379 | 379 |
| 380 | 380 | while (epoch < training_epochs) and (not done_looping): | 381 | 381 | epoch = epoch + 1 | 382 | | - for minibatch_index in xrange(n_train_batches): | | 382 | + for minibatch_index in range(n_train_batches): | 383 | 383 |
| 384 | 384 | minibatch_avg_cost = train_fn(minibatch_index) | 385 | 385 | iter = (epoch - 1) * n_train_batches + minibatch_index | |
Collapse file Copy file name to clipboardExpand all lines: code/SdA.py +26-24Lines changed: 26 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 29 | 29 | Systems 19, 2007 | 30 | 30 |
| 31 | 31 | """ | | 32 | + | | 33 | +from __future__ import print_function | | 34 | + | 32 | 35 | import os | 33 | 36 | import sys | 34 | 37 | import timeit | @@ -116,7 +119,7 @@ def __init__( | 116 | 119 | # stochastich gradient descent on the MLP | 117 | 120 |
| 118 | 121 | # start-snippet-2 | 119 | | - for i in xrange(self.n_layers): | | 122 | + for i in range(self.n_layers): | 120 | 123 | # construct the sigmoidal layer | 121 | 124 |
| 122 | 125 | # 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): | 254 | 257 |
| 255 | 258 | # compute number of minibatches for training, validation and testing | 256 | 259 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] | 257 | | - n_valid_batches /= batch_size | | 260 | + n_valid_batches //= batch_size | 258 | 261 | n_test_batches = test_set_x.get_value(borrow=True).shape[0] | 259 | | - n_test_batches /= batch_size | | 262 | + n_test_batches //= batch_size | 260 | 263 |
| 261 | 264 | index = T.lscalar('index') # index to a [mini]batch | 262 | 265 |
| @@ -314,11 +317,11 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate): | 314 | 317 |
| 315 | 318 | # Create a function that scans the entire validation set | 316 | 319 | 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)] | 318 | 321 |
| 319 | 322 | # Create a function that scans the entire test set | 320 | 323 | 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)] | 322 | 325 |
| 323 | 326 | return train_fn, valid_score, test_score | 324 | 327 |
| @@ -357,12 +360,12 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 357 | 360 |
| 358 | 361 | # compute number of minibatches for training, validation and testing | 359 | 362 | n_train_batches = train_set_x.get_value(borrow=True).shape[0] | 360 | | - n_train_batches /= batch_size | | 363 | + n_train_batches //= batch_size | 361 | 364 |
| 362 | 365 | # numpy random generator | 363 | 366 | # start-snippet-3 | 364 | 367 | numpy_rng = numpy.random.RandomState(89677) | 365 | | - print '... building the model' | | 368 | + print('... building the model') | 366 | 369 | # construct the stacked denoising autoencoder class | 367 | 370 | sda = SdA( | 368 | 371 | numpy_rng=numpy_rng, | @@ -374,52 +377,51 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 374 | 377 | ######################### | 375 | 378 | # PRETRAINING THE MODEL # | 376 | 379 | ######################### | 377 | | - print '... getting the pretraining functions' | | 380 | + print('... getting the pretraining functions') | 378 | 381 | pretraining_fns = sda.pretraining_functions(train_set_x=train_set_x, | 379 | 382 | batch_size=batch_size) | 380 | 383 |
| 381 | | - print '... pre-training the model' | | 384 | + print('... pre-training the model') | 382 | 385 | start_time = timeit.default_timer() | 383 | 386 | ## Pre-train layer-wise | 384 | 387 | corruption_levels = [.1, .2, .3] | 385 | | - for i in xrange(sda.n_layers): | | 388 | + for i in range(sda.n_layers): | 386 | 389 | # go through pretraining epochs | 387 | | - for epoch in xrange(pretraining_epochs): | | 390 | + for epoch in range(pretraining_epochs): | 388 | 391 | # go through the training set | 389 | 392 | c = [] | 390 | | - for batch_index in xrange(n_train_batches): | | 393 | + for batch_index in range(n_train_batches): | 391 | 394 | c.append(pretraining_fns[i](index=batch_index, | 392 | 395 | corruption=corruption_levels[i], | 393 | 396 | 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 %f' % (i, epoch, numpy.mean(c))) | 396 | 398 |
| 397 | 399 | end_time = timeit.default_timer() | 398 | 400 |
| 399 | | - print >> sys.stderr, ('The pretraining code for file ' + | 400 | | - os.path.split(__file__)[1] + | 401 | | - ' ran for %.2fm' % ((end_time - start_time) / 60.)) | | 401 | + print(('The pretraining code for file ' + | | 402 | + os.path.split(__file__)[1] + | | 403 | + ' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr) | 402 | 404 | # end-snippet-4 | 403 | 405 | ######################## | 404 | 406 | # FINETUNING THE MODEL # | 405 | 407 | ######################## | 406 | 408 |
| 407 | 409 | # get the training, validation and testing function for the model | 408 | | - print '... getting the finetuning functions' | | 410 | + print('... getting the finetuning functions') | 409 | 411 | train_fn, validate_model, test_model = sda.build_finetune_functions( | 410 | 412 | datasets=datasets, | 411 | 413 | batch_size=batch_size, | 412 | 414 | learning_rate=finetune_lr | 413 | 415 | ) | 414 | 416 |
| 415 | | - print '... finetunning the model' | | 417 | + print('... finetunning the model') | 416 | 418 | # early-stopping parameters | 417 | 419 | patience = 10 * n_train_batches # look as this many examples regardless | 418 | 420 | patience_increase = 2. # wait this much longer when a new best is | 419 | 421 | # found | 420 | 422 | improvement_threshold = 0.995 # a relative improvement of this much is | 421 | 423 | # considered significant | 422 | | - validation_frequency = min(n_train_batches, patience / 2) | | 424 | + validation_frequency = min(n_train_batches, patience // 2) | 423 | 425 | # go through this many | 424 | 426 | # minibatche before checking the network | 425 | 427 | # on the validation set; in this case we | @@ -434,7 +436,7 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 434 | 436 |
| 435 | 437 | while (epoch < training_epochs) and (not done_looping): | 436 | 438 | epoch = epoch + 1 | 437 | | - for minibatch_index in xrange(n_train_batches): | | 439 | + for minibatch_index in range(n_train_batches): | 438 | 440 | minibatch_avg_cost = train_fn(minibatch_index) | 439 | 441 | iter = (epoch - 1) * n_train_batches + minibatch_index | 440 | 442 |
| @@ -480,9 +482,9 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15, | 480 | 482 | ) | 481 | 483 | % (best_validation_loss * 100., best_iter + 1, test_score * 100.) | 482 | 484 | ) | 483 | | - print >> sys.stderr, ('The training code for file ' + | 484 | | - os.path.split(__file__)[1] + | 485 | | - ' ran for %.2fm' % ((end_time - start_time) / 60.)) | | 485 | + print(('The training code for file ' + | | 486 | + os.path.split(__file__)[1] + | | 487 | + ' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr) | 486 | 488 |
| 487 | 489 |
| 488 | 490 | if __name__ == '__main__': | |
Collapse file Copy file name to clipboardExpand all lines: code/cA.py +11-8Lines changed: 11 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 | Systems 19, 2007 | 29 | 29 |
| 30 | 30 | """ | | 31 | + | | 32 | +from __future__ import print_function | | 33 | + | 31 | 34 | import os | 32 | 35 | import sys | 33 | 36 | import timeit | @@ -205,7 +208,7 @@ def get_cost_updates(self, contraction_level, learning_rate): | 205 | 208 | axis=1) | 206 | 209 |
| 207 | 210 | # Compute the jacobian and average over the number of samples/minibatch | 208 | | - self.L_jacob = T.sum(J ** 2) / self.n_batchsize | | 211 | + self.L_jacob = T.sum(J ** 2) // self.n_batchsize | 209 | 212 |
| 210 | 213 | # note : L is now a vector, where each element is the | 211 | 214 | # cross-entropy cost of the reconstruction of the | @@ -246,7 +249,7 @@ def test_cA(learning_rate=0.01, training_epochs=20, | 246 | 249 | train_set_x, train_set_y = datasets[0] | 247 | 250 |
| 248 | 251 | # compute number of minibatches for training, validation and testing | 249 | | - n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size | | 252 | + n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size | 250 | 253 |
| 251 | 254 | # allocate symbolic variables for the data | 252 | 255 | index = T.lscalar() # index to a [mini]batch | @@ -283,22 +286,22 @@ def test_cA(learning_rate=0.01, training_epochs=20, | 283 | 286 | ############ | 284 | 287 |
| 285 | 288 | # go through training epochs | 286 | | - for epoch in xrange(training_epochs): | | 289 | + for epoch in range(training_epochs): | 287 | 290 | # go through trainng set | 288 | 291 | c = [] | 289 | | - for batch_index in xrange(n_train_batches): | | 292 | + for batch_index in range(n_train_batches): | 290 | 293 | c.append(train_ca(batch_index)) | 291 | 294 |
| 292 | 295 | 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])) | | 296 | + print('Training epoch %d, reconstruction cost ' % epoch, numpy.mean( | | 297 | + c_array[0]), ' jacobian norm ', numpy.mean(numpy.sqrt(c_array[1]))) | 295 | 298 |
| 296 | 299 | end_time = timeit.default_timer() | 297 | 300 |
| 298 | 301 | training_time = (end_time - start_time) | 299 | 302 |
| 300 | | - print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] + | 301 | | - ' ran for %.2fm' % ((training_time) / 60.)) | | 303 | + print(('The code for file ' + os.path.split(__file__)[1] + | | 304 | + ' ran for %.2fm' % ((training_time) / 60.)), file=sys.stderr) | 302 | 305 | image = Image.fromarray(tile_raster_images( | 303 | 306 | X=ca.W.get_value(borrow=True).T, | 304 | 307 | img_shape=(28, 28), tile_shape=(10, 10), | |
Collapse file Copy file name to clipboardExpand all lines: code/convolutional_mlp.py +17-14Lines changed: 17 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf | 22 | 22 |
| 23 | 23 | """ | | 24 | + | | 25 | +from __future__ import print_function | | 26 | + | 24 | 27 | import os | 25 | 28 | import sys | 26 | 29 | import timeit | @@ -70,7 +73,7 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): | 70 | 73 | # each unit in the lower layer receives a gradient from: | 71 | 74 | # "num output feature maps * filter height * filter width" / | 72 | 75 | # pooling size | 73 | | - fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) / | | 76 | + fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // | 74 | 77 | numpy.prod(poolsize)) | 75 | 78 | # initialize weights with random weights | 76 | 79 | W_bound = numpy.sqrt(6. / (fan_in + fan_out)) | @@ -145,9 +148,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 145 | 148 | n_train_batches = train_set_x.get_value(borrow=True).shape[0] | 146 | 149 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] | 147 | 150 | 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 | 151 | 154 |
| 152 | 155 | # allocate symbolic variables for the data | 153 | 156 | index = T.lscalar() # index to a [mini]batch | @@ -160,7 +163,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 160 | 163 | ###################### | 161 | 164 | # BUILD ACTUAL MODEL # | 162 | 165 | ###################### | 163 | | - print '... building the model' | | 166 | + print('... building the model') | 164 | 167 |
| 165 | 168 | # Reshape matrix of rasterized images of shape (batch_size, 28 * 28) | 166 | 169 | # to a 4D tensor, compatible with our LeNetConvPoolLayer | @@ -261,14 +264,14 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 261 | 264 | ############### | 262 | 265 | # TRAIN MODEL # | 263 | 266 | ############### | 264 | | - print '... training' | | 267 | + print('... training') | 265 | 268 | # early-stopping parameters | 266 | 269 | patience = 10000 # look as this many examples regardless | 267 | 270 | patience_increase = 2 # wait this much longer when a new best is | 268 | 271 | # found | 269 | 272 | improvement_threshold = 0.995 # a relative improvement of this much is | 270 | 273 | # considered significant | 271 | | - validation_frequency = min(n_train_batches, patience / 2) | | 274 | + validation_frequency = min(n_train_batches, patience // 2) | 272 | 275 | # go through this many | 273 | 276 | # minibatche before checking the network | 274 | 277 | # on the validation set; in this case we | @@ -284,19 +287,19 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 284 | 287 |
| 285 | 288 | while (epoch < n_epochs) and (not done_looping): | 286 | 289 | epoch = epoch + 1 | 287 | | - for minibatch_index in xrange(n_train_batches): | | 290 | + for minibatch_index in range(n_train_batches): | 288 | 291 |
| 289 | 292 | iter = (epoch - 1) * n_train_batches + minibatch_index | 290 | 293 |
| 291 | 294 | if iter % 100 == 0: | 292 | | - print 'training @ iter = ', iter | | 295 | + print('training @ iter = ', iter) | 293 | 296 | cost_ij = train_model(minibatch_index) | 294 | 297 |
| 295 | 298 | if (iter + 1) % validation_frequency == 0: | 296 | 299 |
| 297 | 300 | # compute zero-one loss on validation set | 298 | 301 | validation_losses = [validate_model(i) for i | 299 | | - in xrange(n_valid_batches)] | | 302 | + in range(n_valid_batches)] | 300 | 303 | this_validation_loss = numpy.mean(validation_losses) | 301 | 304 | print('epoch %i, minibatch %i/%i, validation error %f %%' % | 302 | 305 | (epoch, minibatch_index + 1, n_train_batches, | @@ -317,7 +320,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 317 | 320 | # test it on the test set | 318 | 321 | test_losses = [ | 319 | 322 | test_model(i) | 320 | | - for i in xrange(n_test_batches) | | 323 | + for i in range(n_test_batches) | 321 | 324 | ] | 322 | 325 | test_score = numpy.mean(test_losses) | 323 | 326 | print((' epoch %i, minibatch %i/%i, test error of ' | @@ -334,9 +337,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200, | 334 | 337 | print('Best validation score of %f %% obtained at iteration %i, ' | 335 | 338 | 'with test performance %f %%' % | 336 | 339 | (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) | 340 | 343 |
| 341 | 344 | if __name__ == '__main__': | 342 | 345 | evaluate_lenet5() | |
|