@@ -47,24 +47,59 @@ MNIST Dataset
4747 For convenience we pickled the dataset to make it easier to use in python.
4848 It is available for download `here <http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz>`_.
4949 The pickled file represents a tuple of 3 lists : the training set, the
50- validation set and the testing set. Each element of any of the three lists
51- represents a minibatch of 20 examples. Such an element is a tuple composed
52- of the list of 20 images and the list of class labels for each of the
53- images. An image is represented as numpy 1-dimensional array of 784 (28 x 28) float
54- values between 0 and 1 ( 0 stands for black, 1 for white). The labels
55- are numbers between 0 and 9 indicating which digit the image
56- represents. Loading and accessing the dataset in the python can be done as
57- follows:
50+ validation set and the testing set. Each of the three lists is a pair
51+ formed from a list of images and a list of class labels for each of the
52+ images. An image is represented as numpy 1-dimensional array of 784 (28
53+ x 28) float values between 0 and 1 (0 stands for black, 1 for white).
54+ The labels are numbers between 0 and 9 indicating which digit the image
55+ represents. When using the dataset, we usually divide it in minibatches
56+ (see :ref:`opt_SGD`). The code block below shows how to load the
57+ dataset and how to divide it in minibatches of a given size :
58+
5859
5960 .. code-block:: python
6061
6162 import cPickle, gzip, numpy
6263
64+ # Load the dataset
65+ f = gzip.open('mnist.pkl.gz','rb')
66+ train_set, valid_set, test_set = cPickle.load(f)
67+ f.close()
68+
69+ # make minibatches of size 20
70+ batch_size = 20 # sized of the minibatch
71+
72+ # Dealing with the training set
73+ # get the list of training images (x) and their labels (y)
74+ (train_set_x, train_set_y) = train_set
75+ # initialize the list of training minibatches with empty list
76+ train_batches = []
77+ for i in xrange(0, len(train_set_x), batch_size):
78+ # add to the list of minibatches the minibatch starting at
79+ # position i, ending at position i+batch_size
80+ # a minibatch is a pair ; the first element of the pair is a list
81+ # of datapoints, the second element is the list of corresponding
82+ # labels
83+ train_batches = train_batches + \
84+ [(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
85+
86+ # Dealing with the validation set
87+ (valid_set_x, valid_set_y) = valid_set
88+ # initialize the list of validation minibatches
89+ valid_batches = []
90+ for i in xrange(0, len(valid_set_x), batch_size):
91+ valid_batches = valid_batches + \
92+ [(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
93+
94+ # Dealing with the testing set
95+ (test_set_x, test_set_y) = test_set
96+ # initialize the list of testing minibatches
97+ test_batches = []
98+ for i in xrange(0, len(test_set_x), batch_size):
99+ test_batches = test_batches + \
100+ [(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
101+
63102
64- f = gzip.open('mnist.pkl.gz','rb')
65- (training_set, validation_set, testing_set) = cPickle.load(f)
66- f.close()
67-
68103 # accessing training example i of minibatch j
69104 image = training_set[j][0][i]
70105 label = training_set[j][1][i]
@@ -250,10 +285,10 @@ This can be computed using the following line of code :
250285 # syntax to retrieve the log-probability of the correct labels, y.
251286
252287
253- .. _opt_SGD:
254-
255288.. index:: Stochastic Gradient Descent
256289
290+ .. _opt_SGD:
291+
257292Stochastic Gradient Descent
258293+++++++++++++++++++++++++++
259294
0 commit comments