@@ -75,7 +75,7 @@ MNIST Dataset |
75 | 75 | import cPickle, gzip, numpy |
76 | 76 |
|
77 | 77 | # Load the dataset |
78 | | - f = gzip.open('mnist.pkl.gz','rb') |
| 78 | + f = gzip.open('mnist.pkl.gz', 'rb') |
79 | 79 | train_set, valid_set, test_set = cPickle.load(f) |
80 | 80 | f.close() |
81 | 81 |
|
@@ -141,8 +141,8 @@ MNIST Dataset |
141 | 141 |
|
142 | 142 | # accessing the third minibatch of the training set |
143 | 143 |
|
144 | | - data = train_set_x[2*500:3*500] |
145 | | - label = train_set_y[2*500:3*500] |
| 144 | + data = train_set_x[2 * 500: 3 * 500] |
| 145 | + label = train_set_y[2 * 500: 3 * 500] |
146 | 146 |
|
147 | 147 |
|
148 | 148 | The data has to be stored as floats on the GPU ( the right |
@@ -294,7 +294,7 @@ In python, using Theano this can be written as : |
294 | 294 | # expression of the zero one loss ; to get the actual value this |
295 | 295 | # symbolic expression has to be compiled into a Theano function (see |
296 | 296 | # the Theano tutorial for more details) |
297 | | - zero_one_loss = T.sum(T.neq(T.argmax(p_y_given_x),y)) |
| 297 | + zero_one_loss = T.sum(T.neq(T.argmax(p_y_given_x), y)) |
298 | 298 |
|
299 | 299 |
|
300 | 300 | .. index:: Negative Log--Likelihood Loss |
@@ -338,7 +338,7 @@ This can be computed using the following line of code : |
338 | 338 | # NLL is a symbolic variable ; to get the actual value of NLL, this symbolic |
339 | 339 | # expression has to be compiled into a Theano function (see the Theano |
340 | 340 | # tutorial for more details) |
341 | | - NLL = -T.sum(T.log(p_y_given_x)[T.arange(y.shape[0]),y]) |
| 341 | + NLL = -T.sum(T.log(p_y_given_x)[T.arange(y.shape[0]), y]) |
342 | 342 | # note on syntax: T.arange(y.shape[0]) is a vector of integers [0,1,2,...,len(y)]. |
343 | 343 | # Indexing a matrix M by the two vectors [0,1,...,K], [a,b,...,k] returns the |
344 | 344 | # elements M[0,a], M[1,b], ..., M[K,k] as a vector. Here, we use this |
@@ -438,8 +438,8 @@ algorithm in Theano can be done as follows : |
438 | 438 | d_loss_wrt_params = T.grad(loss, params) |
439 | 439 |
|
440 | 440 | # compile the MSGD step into a theano function |
441 | | - updates = { params: params - learning_rate * d_loss_wrt_params} |
442 | | - MSGD = theano.function([x_batch,y_batch], loss, updates = updates) |
| 441 | + updates = {params: params - learning_rate * d_loss_wrt_params} |
| 442 | + MSGD = theano.function([x_batch,y_batch], loss, updates=updates) |
443 | 443 |
|
444 | 444 | for (x_batch, y_batch) in train_batches: |
445 | 445 | # here x_batch and y_batch are elements of train_batches and |
@@ -525,7 +525,7 @@ L2 regularization term weighted by :math:`\lambda_2` |
525 | 525 | L1 = T.sum(abs(param)) |
526 | 526 |
|
527 | 527 | # symbolic Theano variable that represents the squared L2 term |
528 | | - L2_sqr = T.sum(param**2) |
| 528 | + L2_sqr = T.sum(param ** 2) |
529 | 529 |
|
530 | 530 | # the loss |
531 | 531 | loss = NLL + lambda_1 * L1 + lambda_2 * L2 |
@@ -557,20 +557,20 @@ of a strategy based on a geometrically increasing amount of patience. |
557 | 557 | .. code-block:: python |
558 | 558 |
|
559 | 559 | # early-stopping parameters |
560 | | - patience = 5000 # look as this many examples regardless |
561 | | - patience_increase = 2 # wait this much longer when a new best is |
| 560 | + patience = 5000 # look as this many examples regardless |
| 561 | + patience_increase = 2 # wait this much longer when a new best is |
562 | 562 | # found |
563 | | - improvement_threshold = 0.995 # a relative improvement of this much is |
564 | | - # considered significant |
565 | | - validation_frequency = min(n_train_batches, patience/2) |
| 563 | + improvement_threshold = 0.995 # a relative improvement of this much is |
| 564 | + # considered significant |
| 565 | + validation_frequency = min(n_train_batches, patience/2) |
566 | 566 | # go through this many |
567 | 567 | # minibatches before checking the network |
568 | 568 | # on the validation set; in this case we |
569 | 569 | # check every epoch |
570 | 570 |
|
571 | | - best_params = None |
| 571 | + best_params = None |
572 | 572 | best_validation_loss = numpy.inf |
573 | | - test_score = 0. |
| 573 | + test_score = 0. |
574 | 574 | start_time = time.clock() |
575 | 575 |
|
576 | 576 | done_looping = False |
@@ -593,7 +593,7 @@ of a strategy based on a geometrically increasing amount of patience. |
593 | 593 | if this_validation_loss < best_validation_loss: |
594 | 594 |
|
595 | 595 | # improve patience if loss improvement is good enough |
596 | | - if this_validation_loss < best_validation_loss*improvement_threshold: |
| 596 | + if this_validation_loss < best_validation_loss * improvement_threshold: |
597 | 597 |
|
598 | 598 | patience = max(patience, iter * patience_increase) |
599 | 599 | best_params = copy.deepcopy(params) |
@@ -682,9 +682,9 @@ like: |
682 | 682 |
|
683 | 683 | >>> import cPickle |
684 | 684 | >>> save_file = open('path', 'wb') # this will overwrite current contents |
685 | | - >>> cPickle.dump(w.get_value(borrow=True), save_file, -1) # the -1 is for HIGHEST_PROTOCOL |
686 | | - >>> cPickle.dump(v.get_value(borrow=True), save_file, -1) # .. and it triggers much more efficient |
687 | | - >>> cPickle.dump(u.get_value(borrow=True), save_file, -1) # .. storage than numpy's default |
| 685 | + >>> cPickle.dump(w.get_value(borrow=True), save_file, -1) # the -1 is for HIGHEST_PROTOCOL |
| 686 | + >>> cPickle.dump(v.get_value(borrow=True), save_file, -1) # .. and it triggers much more efficient |
| 687 | + >>> cPickle.dump(u.get_value(borrow=True), save_file, -1) # .. storage than numpy's default |
688 | 688 | >>> save_file.close() |
689 | 689 |
|
690 | 690 | Then later, you can load your data back like this: |
|