Skip to content

Commit 36c0080

Browse files
committed
I changed the pickled file such that it is not divided in minibatches before hand. Note that the code now generates some warning .. this is because of an issue with advanced indexing that Pascal will fix later today. The error is caused the fact that I am using mean instead of sum in the negative log likelihood function.
1 parent 483a28f commit 36c0080

6 files changed

Lines changed: 175 additions & 38 deletions

File tree

code/logistic_cg.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
- textbooks: "Pattern Recognition and Machine Learning" -
3333
Christopher M. Bishop, section 4.3.2
3434
35-
TODO: recommended preprocessing, lr ranges, regularization ranges (explain
36-
to do lr first, then add regularization)
3735
3836
"""
3937
__docformat__ = 'restructedtext en'
@@ -146,16 +144,47 @@ def cg_optimization_mnist( n_iter=50 ):
146144
:param n_iter: number of iterations ot run the optimizer
147145
148146
"""
149-
#TODO: Tzanetakis
150147

151-
# Load the dataset ; note that the dataset is already divided in
152-
# minibatches of size 20;
148+
# Load the dataset
153149
f = gzip.open('mnist.pkl.gz','rb')
154-
train_batches, valid_batches, test_batches = cPickle.load(f)
150+
train_set, valid_set, test_set = cPickle.load(f)
155151
f.close()
156152

153+
# make minibatches of size 20
154+
batch_size = 20 # sized of the minibatch
155+
156+
# Dealing with the training set
157+
# get the list of training images (x) and their labels (y)
158+
(train_set_x, train_set_y) = train_set
159+
# initialize the list of training minibatches with empty list
160+
train_batches = []
161+
for i in xrange(0, len(train_set_x), batch_size):
162+
# add to the list of minibatches the minibatch starting at
163+
# position i, ending at position i+batch_size
164+
# a minibatch is a pair ; the first element of the pair is a list
165+
# of datapoints, the second element is the list of corresponding
166+
# labels
167+
train_batches = train_batches + \
168+
[(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
169+
170+
# Dealing with the validation set
171+
(valid_set_x, valid_set_y) = valid_set
172+
# initialize the list of validation minibatches
173+
valid_batches = []
174+
for i in xrange(0, len(valid_set_x), batch_size):
175+
valid_batches = valid_batches + \
176+
[(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
177+
178+
# Dealing with the testing set
179+
(test_set_x, test_set_y) = test_set
180+
# initialize the list of testing minibatches
181+
test_batches = []
182+
for i in xrange(0, len(test_set_x), batch_size):
183+
test_batches = test_batches + \
184+
[(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
185+
186+
157187
ishape = (28,28) # this is the size of MNIST images
158-
batch_size = 20 # size of the minibatch
159188
n_in = 28*28 # number of input units
160189
n_out = 10 # number of output units
161190
# allocate symbolic variables for the data

code/logistic_sgd.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
- textbooks: "Pattern Recognition and Machine Learning" -
3333
Christopher M. Bishop, section 4.3.2
3434
35-
TODO: recommended preprocessing, lr ranges, regularization ranges (explain
36-
to do lr first, then add regularization)
3735
3836
"""
3937
__docformat__ = 'restructedtext en'
@@ -150,14 +148,46 @@ def sgd_optimization_mnist( learning_rate=0.01, n_iter=100):
150148
151149
"""
152150

153-
# Load the dataset ; note that the dataset is already divided in
154-
# minibatches of size 10;
151+
# Load the dataset
155152
f = gzip.open('mnist.pkl.gz','rb')
156-
train_batches, valid_batches, test_batches = cPickle.load(f)
153+
train_set, valid_set, test_set = cPickle.load(f)
157154
f.close()
158155

156+
# make minibatches of size 20
157+
batch_size = 20 # sized of the minibatch
158+
159+
# Dealing with the training set
160+
# get the list of training images (x) and their labels (y)
161+
(train_set_x, train_set_y) = train_set
162+
# initialize the list of training minibatches with empty list
163+
train_batches = []
164+
for i in xrange(0, len(train_set_x), batch_size):
165+
# add to the list of minibatches the minibatch starting at
166+
# position i, ending at position i+batch_size
167+
# a minibatch is a pair ; the first element of the pair is a list
168+
# of datapoints, the second element is the list of corresponding
169+
# labels
170+
train_batches = train_batches + \
171+
[(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
172+
173+
# Dealing with the validation set
174+
(valid_set_x, valid_set_y) = valid_set
175+
# initialize the list of validation minibatches
176+
valid_batches = []
177+
for i in xrange(0, len(valid_set_x), batch_size):
178+
valid_batches = valid_batches + \
179+
[(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
180+
181+
# Dealing with the testing set
182+
(test_set_x, test_set_y) = test_set
183+
# initialize the list of testing minibatches
184+
test_batches = []
185+
for i in xrange(0, len(test_set_x), batch_size):
186+
test_batches = test_batches + \
187+
[(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
188+
189+
159190
ishape = (28,28) # this is the size of MNIST images
160-
batch_size = 20 # size of the minibatch
161191

162192
# allocate symbolic variables for the data
163193
x = T.fmatrix() # the data is presented as rasterized images

code/mlp.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,46 @@ def sgd_optimization_mnist( learning_rate=0.01, L1_reg = 0.0, \
170170
regularization)
171171
"""
172172

173-
# Load the dataset ; note that the dataset is already divided in
174-
# minibatches of size 10;
173+
# Load the dataset
175174
f = gzip.open('mnist.pkl.gz','rb')
176-
train_batches, valid_batches, test_batches = cPickle.load(f)
175+
train_set, valid_set, test_set = cPickle.load(f)
177176
f.close()
178177

178+
# make minibatches of size 20
179+
batch_size = 20 # sized of the minibatch
180+
181+
# Dealing with the training set
182+
# get the list of training images (x) and their labels (y)
183+
(train_set_x, train_set_y) = train_set
184+
# initialize the list of training minibatches with empty list
185+
train_batches = []
186+
for i in xrange(0, len(train_set_x), batch_size):
187+
# add to the list of minibatches the minibatch starting at
188+
# position i, ending at position i+batch_size
189+
# a minibatch is a pair ; the first element of the pair is a list
190+
# of datapoints, the second element is the list of corresponding
191+
# labels
192+
train_batches = train_batches + \
193+
[(train_set_x[i:i+batch_size], train_set_y[i:i+batch_size])]
194+
195+
# Dealing with the validation set
196+
(valid_set_x, valid_set_y) = valid_set
197+
# initialize the list of validation minibatches
198+
valid_batches = []
199+
for i in xrange(0, len(valid_set_x), batch_size):
200+
valid_batches = valid_batches + \
201+
[(valid_set_x[i:i+batch_size], valid_set_y[i:i+batch_size])]
202+
203+
# Dealing with the testing set
204+
(test_set_x, test_set_y) = test_set
205+
# initialize the list of testing minibatches
206+
test_batches = []
207+
for i in xrange(0, len(test_set_x), batch_size):
208+
test_batches = test_batches + \
209+
[(test_set_x[i:i+batch_size], test_set_y[i:i+batch_size])]
210+
211+
179212
ishape = (28,28) # this is the size of MNIST images
180-
batch_size = 20 # size of the minibatch
181213

182214
# allocate symbolic variables for the data
183215
x = T.fmatrix() # the data is presented as rasterized images
@@ -216,7 +248,8 @@ def sgd_optimization_mnist( learning_rate=0.01, L1_reg = 0.0, \
216248
# the same time updates the parameter of the model based on the rules
217249
# defined in `updates`
218250
train_model = theano.function([x, y], cost, updates = updates )
219-
251+
n_minibatches = len(train_batches)
252+
220253
# early-stopping parameters
221254
patience = 10000 # look as this many examples regardless
222255
patience_increase = 2 # wait this much longer when a new best is
@@ -232,7 +265,6 @@ def sgd_optimization_mnist( learning_rate=0.01, L1_reg = 0.0, \
232265
best_params = None
233266
best_validation_loss = float('inf')
234267
test_score = 0.
235-
n_minibatches = len(train_batches)
236268
start_time = time.clock()
237269
# have a maximum of `n_iter` iterations through the entire dataset
238270
for iter in xrange(n_iter* n_minibatches):

doc/gettingstarted.txt

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
257292
Stochastic Gradient Descent
258293
+++++++++++++++++++++++++++
259294

doc/logreg.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
.. _logreg:
2-
31
.. index:: Logistic Regression
42

3+
.. _logreg :
4+
5+
56
Classifying MNIST digits using Logistic Regression
67
==================================================
78

@@ -14,7 +15,7 @@ TODO : shared variables documentation not up !!
1415

1516
.. _shared variables: http://www.pylearn.org/theano/basic_tutorial
1617

17-
.. _basic arthmetic ops: http://www.pylearn.org/theano/basic_tutorial/adding.html
18+
.. _basic arithmetic ops: http://www.pylearn.org/theano/basic_tutorial/adding.html
1819

1920
.. _T.grad: http://www.pylearn.org/theano/basic_tutorial/examples.html#computing-gradients
2021

doc/mlp.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ Multilayer Perceptron
66
=====================
77

88
.. note::
9-
This section assumes the reader has already read through :doc:`logreg.txt`.
9+
This section assumes the reader has already read through :ref:`logreg` .
1010
Additionally, it uses the following new Theano functions and concepts:
11-
T.tanh, abs, L1 and L2 regularization
11+
`T.tanh`_, `shared variables`_, `basic arithmetic ops`_, `T.grad`_,
12+
:ref:`L1_L2_regularization`
13+
14+
.. _T.tanh: http://www.pylearn.org/theano/basic_tutorial/examples.html?highlight=tanh#logistic-function
15+
16+
.. _shared variables: http://www.pylearn.org/theano/basic_tutorial
17+
18+
.. _basic arithmetic ops: http://www.pylearn.org/theano/basic_tutorial/adding.html
19+
20+
.. _T.grad: http://www.pylearn.org/theano/basic_tutorial/examples.html#computing-gradients
21+
1222

1323
The next architecture we are going to present using Theano is the single-hidden
1424
layer Multi-Layer Perceptron (MLP). An MLP can be viewed as a logistic

0 commit comments

Comments
 (0)