GeezerVison is showing github.com — I have made all tutorials (code and documentation) to use shared var… · able/DeepLearningTutorials@a074b32
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: code/convolutional_mlp.py +28-24Lines changed: 28 additions & 24 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -197,7 +197,7 @@ def shared_dataset(data_xy): | 197 | 197 | valid_set_x, valid_set_y = shared_dataset(valid_set) | 198 | 198 | train_set_x, train_set_y = shared_dataset(train_set) | 199 | 199 |
| 200 | | - batch_size = 500 # sized of the minibatch | | 200 | + batch_size = 500 # size of the minibatch | 201 | 201 |
| 202 | 202 | # compute number of minibatches for training, validation and testing | 203 | 203 | n_train_batches = train_set_x.value.shape[0] / batch_size | @@ -253,7 +253,15 @@ def shared_dataset(data_xy): | 253 | 253 | cost = layer3.negative_log_likelihood(y) | 254 | 254 |
| 255 | 255 | # create a function to compute the mistakes that are made by the model | 256 | | - test_model = theano.function([x,y], layer3.errors(y)) | | 256 | + test_model = theano.function([minibatch_offset], layer3.errors(y), | | 257 | + givens = { | | 258 | + x: test_set_x[minibatch_offset:minibatch_offset+batch_size], | | 259 | + y: test_set_y[minibatch_offset:minibatch_offset+batch_size]}) | | 260 | + | | 261 | + validate_model = theano.function([minibatch_offset], layer3.errors(y), | | 262 | + givens = { | | 263 | + x: valid_set_x[minibatch_offset:minibatch_offset+batch_size], | | 264 | + y: valid_set_y[minibatch_offset:minibatch_offset+batch_size]}) | 257 | 265 |
| 258 | 266 | # create a list of all model parameters to be fit by gradient descent | 259 | 267 | params = layer3.params+ layer2.params+ layer1.params + layer0.params | @@ -268,22 +276,25 @@ def shared_dataset(data_xy): | 268 | 276 | updates = {} | 269 | 277 | for param_i, grad_i in zip(params, grads): | 270 | 278 | updates[param_i] = param_i - learning_rate * grad_i | 271 | | - train_model = theano.function([x, y], cost, updates=updates) | | 279 | + | | 280 | + train_model = theano.function([minibatch_offset], cost, updates=updates, | | 281 | + givens = { | | 282 | + x: train_set_x[minibatch_offset:minibatch_offset+batch_size], | | 283 | + y: train_set_y[minibatch_offset:minibatch_offset+batch_size]}) | 272 | 284 |
| 273 | 285 |
| 274 | 286 | ############### | 275 | 287 | # TRAIN MODEL # | 276 | 288 | ############### | 277 | 289 |
| 278 | | - n_minibatches = len(train_batches) | 279 | | - | 280 | 290 | # early-stopping parameters | 281 | 291 | patience = 10000 # look as this many examples regardless | 282 | 292 | patience_increase = 2 # wait this much longer when a new best is | 283 | 293 | # found | 284 | 294 | improvement_threshold = 0.995 # a relative improvement of this much is | 285 | 295 | # considered significant | 286 | | - validation_frequency = n_minibatches # go through this many | | 296 | + validation_frequency = min(n_train_batches, patience/2) | | 297 | + # go through this many | 287 | 298 | # minibatche before checking the network | 288 | 299 | # on the validation set; in this case we | 289 | 300 | # check every epoch | @@ -295,30 +306,25 @@ def shared_dataset(data_xy): | 295 | 306 | start_time = time.clock() | 296 | 307 |
| 297 | 308 | # have a maximum of `n_iter` iterations through the entire dataset | 298 | | - for iter in xrange(n_iter * n_minibatches): | | 309 | + for iter in xrange(n_iter * n_train_batches): | 299 | 310 |
| 300 | 311 | # get epoch and minibatch index | 301 | | - epoch = iter / n_minibatches | 302 | | - minibatch_index = iter % n_minibatches | 303 | | - | | 312 | + epoch = iter / n_train_batches | | 313 | + minibatch_index = iter % n_train_batches | | 314 | + minibatch_offset = minibatch_index * batch_size | | 315 | + | 304 | 316 | # get the minibatches corresponding to `iter` modulo | 305 | 317 | # `len(train_batches)` | 306 | | - x,y = train_batches[ minibatch_index ] | 307 | 318 |
| 308 | 319 | if iter %100 == 0: | 309 | 320 | print 'training @ iter = ', iter | 310 | | - cost_ij = train_model(x,y) | | 321 | + cost_ij = train_model(minibatch_offset) | 311 | 322 |
| 312 | 323 | if (iter+1) % validation_frequency == 0: | 313 | 324 |
| 314 | | - # compute zero-one loss on validation set | 315 | | - this_validation_loss = 0. | 316 | | - for x,y in valid_batches: | 317 | | - # sum up the errors for each minibatch | 318 | | - this_validation_loss += test_model(x,y) | 319 | | - | 320 | | - # get the average by dividing with the number of minibatches | 321 | | - this_validation_loss /= len(valid_batches) | | 325 | + # compute zero-one loss on validation set | | 326 | + validation_losses = [validate_model(i*batch_size) for i in xrange(n_valid_batches)] | | 327 | + this_validation_loss = numpy.mean(validation_losses) | 322 | 328 | print('epoch %i, minibatch %i/%i, validation error %f %%' % \ | 323 | 329 | (epoch, minibatch_index+1, n_minibatches, \ | 324 | 330 | this_validation_loss*100.)) | @@ -337,10 +343,8 @@ def shared_dataset(data_xy): | 337 | 343 | best_iter = iter | 338 | 344 |
| 339 | 345 | # test it on the test set | 340 | | - test_score = 0. | 341 | | - for x,y in test_batches: | 342 | | - test_score += test_model(x,y) | 343 | | - test_score /= len(test_batches) | | 346 | + test_losses = [test_model(i*batch_size) for i in xrange(n_test_batches)] | | 347 | + test_score = numpy.mean(test_losses) | 344 | 348 | print((' epoch %i, minibatch %i/%i, test error of best ' | 345 | 349 | 'model %f %%') % | 346 | 350 | (epoch, minibatch_index+1, n_minibatches, | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_cg.py +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,8 +153,6 @@ def cg_optimization_mnist( n_iter=50, mnist_pkl_gz='mnist.pkl.gz' ): | 153 | 153 | train_set, valid_set, test_set = cPickle.load(f) | 154 | 154 | f.close() | 155 | 155 |
| 156 | | - # make minibatches of size 20 | 157 | | - batch_size = 500 # sized of the minibatch | 158 | 156 |
| 159 | 157 | def shared_dataset(data_xy): | 160 | 158 | data_x, data_y = data_xy | @@ -166,7 +164,7 @@ def shared_dataset(data_xy): | 166 | 164 | valid_set_x, valid_set_y = shared_dataset(valid_set) | 167 | 165 | train_set_x, train_set_y = shared_dataset(train_set) | 168 | 166 |
| 169 | | - batch_size = 500 # sized of the minibatch | | 167 | + batch_size = 600 # size of the minibatch | 170 | 168 |
| 171 | 169 | n_train_batches = train_set_x.value.shape[0] / batch_size | 172 | 170 | n_valid_batches = valid_set_x.value.shape[0] / batch_size | @@ -252,7 +250,7 @@ def callback(theta_value): | 252 | 250 | # if so, replace the old one, and compute the score on the | 253 | 251 | # testing dataset | 254 | 252 | validation_scores[0] = this_validation_loss | 255 | | - test_loses = [test_model(i*batch_size) for i in xrange(n_train_batches)] | | 253 | + test_loses = [test_model(i*batch_size) for i in xrange(n_test_batches)] | 256 | 254 | validation_scores[1] = numpy.mean(test_loses) | 257 | 255 |
| 258 | 256 | # using scipy conjugate gradient optimizer | |
Collapse file Copy file name to clipboardExpand all lines: code/logistic_sgd.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -165,7 +165,7 @@ def shared_dataset(data_xy): | 165 | 165 | valid_set_x, valid_set_y = shared_dataset(valid_set) | 166 | 166 | train_set_x, train_set_y = shared_dataset(train_set) | 167 | 167 |
| 168 | | - batch_size = 500 # sized of the minibatch | | 168 | + batch_size = 600 # size of the minibatch | 169 | 169 |
| 170 | 170 | # compute number of minibatches for training, validation and testing | 171 | 171 | n_train_batches = train_set_x.value.shape[0] / batch_size | @@ -193,7 +193,7 @@ def shared_dataset(data_xy): | 193 | 193 | x:test_set_x[minibatch_offset:minibatch_offset+batch_size], | 194 | 194 | y:test_set_y[minibatch_offset:minibatch_offset+batch_size]}) | 195 | 195 |
| 196 | | - validate_model = theano.function([minibatch_offset], classifier.errors(y), | | 196 | + validate_model =theano.function([minibatch_offset], classifier.errors(y), | 197 | 197 | givens={ | 198 | 198 | x:valid_set_x[minibatch_offset:minibatch_offset+batch_size], | 199 | 199 | y:valid_set_y[minibatch_offset:minibatch_offset+batch_size]}) | @@ -220,7 +220,8 @@ def shared_dataset(data_xy): | 220 | 220 | # found | 221 | 221 | improvement_threshold = 0.995 # a relative improvement of this much is | 222 | 222 | # considered significant | 223 | | - validation_frequency = n_train_batches # go through this many | | 223 | + validation_frequency = min(n_train_batches, patience/2) | | 224 | + # go through this many | 224 | 225 | # minibatche before checking the network | 225 | 226 | # on the validation set; in this case we | 226 | 227 | # check every epoch | |
Collapse file Copy file name to clipboardExpand all lines: code/mlp.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -188,7 +188,7 @@ def shared_dataset(data_xy): | 188 | 188 | valid_set_x, valid_set_y = shared_dataset(valid_set) | 189 | 189 | train_set_x, train_set_y = shared_dataset(train_set) | 190 | 190 |
| 191 | | - batch_size = 500 # sized of the minibatch | | 191 | + batch_size = 20 # size of the minibatch | 192 | 192 |
| 193 | 193 | # compute number of minibatches for training, validation and testing | 194 | 194 | n_train_batches = train_set_x.value.shape[0] / batch_size | @@ -252,7 +252,8 @@ def shared_dataset(data_xy): | 252 | 252 | # found | 253 | 253 | improvement_threshold = 0.995 # a relative improvement of this much is | 254 | 254 | # considered significant | 255 | | - validation_frequency = n_train_batches # go through this many | | 255 | + validation_frequency = min(n_train_batches,patience/2) | | 256 | + # go through this many | 256 | 257 | # minibatche before checking the network | 257 | 258 | # on the validation set; in this case we | 258 | 259 | # check every epoch | |
Collapse file Copy file name to clipboardExpand all lines: doc/gettingstarted.txt +65-42Lines changed: 65 additions & 42 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -.. _gettingstarted: | | 1 | +.. _gettingstarted: | 2 | 2 |
| 3 | 3 |
| 4 | 4 | =============== | @@ -55,9 +55,7 @@ MNIST Dataset | 55 | 55 | images. An image is represented as numpy 1-dimensional array of 784 (28 | 56 | 56 | x 28) float values between 0 and 1 (0 stands for black, 1 for white). | 57 | 57 | The labels are numbers between 0 and 9 indicating which digit the image | 58 | | - represents. When using the dataset, we usually divide it in minibatches | 59 | | - (see :ref:`opt_SGD`). The code block below shows how to load the | 60 | | - dataset and how to divide it in minibatches of a given size : | | 58 | + represents. The code block below shows how to load the dataset. | 61 | 59 |
| 62 | 60 |
| 63 | 61 | .. code-block:: python | @@ -69,43 +67,59 @@ MNIST Dataset | 69 | 67 | train_set, valid_set, test_set = cPickle.load(f) | 70 | 68 | f.close() | 71 | 69 |
| 72 | | - # make minibatches of size 20 | 73 | | - batch_size = 20 # sized of the minibatch | 74 | | - | 75 | | - # Dealing with the training set | 76 | | - # get the list of training images (x) and their labels (y) | 77 | | - (train_set_x, train_set_y) = train_set | 78 | | - # initialize the list of training minibatches with empty list | 79 | | - train_batches = [] | 80 | | - for i in xrange(0, len(train_set_x), batch_size): | 81 | | - # add to the list of minibatches the minibatch starting at | 82 | | - # position i, ending at position i+batch_size | 83 | | - # a minibatch is a pair ; the first element of the pair is a list | 84 | | - # of datapoints, the second element is the list of corresponding | 85 | | - # labels | 86 | | - train_batches = train_batches + \ | 87 | | - [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])] | 88 | | - | 89 | | - # Dealing with the validation set | 90 | | - (valid_set_x, valid_set_y) = valid_set | 91 | | - # initialize the list of validation minibatches | 92 | | - valid_batches = [] | 93 | | - for i in xrange(0, len(valid_set_x), batch_size): | 94 | | - valid_batches = valid_batches + \ | 95 | | - [(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])] | 96 | | - | 97 | | - # Dealing with the testing set | 98 | | - (test_set_x, test_set_y) = test_set | 99 | | - # initialize the list of testing minibatches | 100 | | - test_batches = [] | 101 | | - for i in xrange(0, len(test_set_x), batch_size): | 102 | | - test_batches = test_batches + \ | 103 | | - [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])] | 104 | | - | 105 | | - | 106 | | - # accessing training example i of minibatch j | 107 | | - image = training_set[j][0][i] | 108 | | - label = training_set[j][1][i] | | 70 | + | | 71 | + When using the dataset, we usually divide it in minibatches (see | | 72 | + :ref:`opt_SGD`). We encourage you to store the dataset into shared | | 73 | + variables and access it based on the minibatch offset, given a fixed | | 74 | + and known batch size. The reason behind shared variables is | | 75 | + related to using the GPU. There is a large overhead when copying data | | 76 | + into the GPU memory. If you would copy data on request ( each minibatch | | 77 | + individually when needed) as the code will do if you do not use shared | | 78 | + variables, due to this overhead, the GPU code will not be much faster | | 79 | + then the CPU code (maybe even slower). If you have your data into a | | 80 | + Theano shared variables though, you give Theano the possibility to copy | | 81 | + the entire data on the GPU in a single call when the shared variables are constructed. | | 82 | + Afterwards the GPU can access any minibatch by taking a slice from this | | 83 | + shared variables, without needing to copy any information from the CPU | | 84 | + memory and therefore bypassing the overhead. | | 85 | + Because the datapoints and their labels are usually of different nature | | 86 | + (labels are usually integers while datapoints are real numbers) we | | 87 | + suggest to use different variables for labes and data. Also we recomand | | 88 | + using different variables for the training set, validation set and | | 89 | + testing set to make the code more readable (resulting in 6 different | | 90 | + shared variables). | | 91 | + | | 92 | + Since now the data is in one variable, and a minibatch is defined as a | | 93 | + slice of that variable, it comes more natural to define a minibatch by | | 94 | + indicating where the slice starts (the offset) and how large it is (the | | 95 | + batch size). Note that since the batch size stays constant through out the | | 96 | + execution of the code, a function will | | 97 | + require only the offset as input in order to identify on which minibatch to work. | | 98 | + The code below shows how to store your data and how to | | 99 | + access a minibatch: | | 100 | + | | 101 | + | | 102 | + .. code-block:: python | | 103 | + | | 104 | + def shared_dataset(data_xy): | | 105 | + data_x, data_y = data_xy | | 106 | + shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX)) | | 107 | + shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX)) | | 108 | + return shared_x, T.cast(shared_y, 'int32') | | 109 | + | | 110 | + test_set_x, test_set_y = shared_dataset(test_set) | | 111 | + valid_set_x, valid_set_y = shared_dataset(valid_set) | | 112 | + train_set_x, train_set_y = shared_dataset(train_set) | | 113 | + | | 114 | + batch_size = 500 # size of the minibatch | | 115 | + | | 116 | + # accessing the third minibatch of the training set | | 117 | + | | 118 | + data = train_set_x[2*500:3*500] | | 119 | + label = train_set_y[2*500:3*500] | | 120 | + | | 121 | + | | 122 | + | 109 | 123 |
| 110 | 124 |
| 111 | 125 | .. index:: Notation | @@ -503,7 +517,8 @@ of a strategy based on a geometrically increasing amount of patience. | 503 | 517 | # validation error is found | 504 | 518 | improvement_threshold = 0.995 # a relative improvement of this much is | 505 | 519 | # considered significant | 506 | | - validation_frequency = 2500 # make this many SGD updates between validations | | 520 | + validation_frequency = min(2500, patience/2.) | | 521 | + # make this many SGD updates between validations | 507 | 522 |
| 508 | 523 | # initialize cross-validation variables | 509 | 524 | best_params = None | @@ -547,6 +562,14 @@ of a strategy based on a geometrically increasing amount of patience. | 547 | 562 | If we run out of batches of training data before running out of patience, then | 548 | 563 | we just go back to the beginning of the training set and repeat. | 549 | 564 |
| | 565 | + | | 566 | +.. note:: | | 567 | + | | 568 | + The ``validation_frequency`` should always be smaller than the | | 569 | + ``patience``. The code should check at least two times how it | | 570 | + performs before running out of patience. This is the reason we used | | 571 | + the formulation ``validation_frequency = min( 2500, patience/2.)`` | | 572 | + | 550 | 573 | .. note:: | 551 | 574 |
| 552 | 575 | This algorithm could possibly be improved by using a test of statistical significance | |
Collapse file Copy file name to clipboardExpand all lines: doc/lenet.txt +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -504,7 +504,11 @@ instantiate the network as follows. | 504 | 504 | updates = {} | 505 | 505 | for param_i, grad_i in zip(params, grads): | 506 | 506 | updates[param_i] = param_i - learning_rate * grad_i | 507 | | - train_model = theano.function([x, y], cost, updates=updates) | | 507 | + train_model = theano.function([minibatch_offset], cost, updates = updates, | | 508 | + givens={ | | 509 | + x:train_set_x[minibatch_offset:minibatch_offset+batch_size], | | 510 | + y:train_set_y[minibatch_offset:minibatch_offset+batch_size]}) | | 511 | + | 508 | 512 |
| 509 | 513 |
| 510 | 514 | We leave out the code, which performs the actual training and early-stopping, | |
|