Skip to content

Commit 1d8b9fd

Browse files
committed
In the ReStructuredText (*.txt) files, many of the copies of Python code were out of date. Replaced copies of Python code with links to sections of the corresponding files. This dramatically reduced the number of lines of code to maintain. This will help keep documentation in sync with code (but will not guarantee synchronization). Renamed two ReStructuredText files to match corresponding Python files. Modified code files for improved readability. Modified .gitignore to ignore tmp directories.
1 parent afbe069 commit 1d8b9fd

20 files changed

+602
-2390
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
code/*.pyc
2+
code/tmp*
23
code/midi
34
data/mnist.pkl.gz
45
data/mnist_py3k.pkl.gz

Diff for: code/SdA.py

+51-37
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ class SdA(object):
5757
the dAs are only used to initialize the weights.
5858
"""
5959

60-
def __init__(self, numpy_rng, theano_rng=None, n_ins=784,
61-
hidden_layers_sizes=[500, 500], n_outs=10,
62-
corruption_levels=[0.1, 0.1]):
60+
def __init__(self,
61+
numpy_rng,
62+
theano_rng = None,
63+
n_ins = 784,
64+
hidden_layers_sizes = [500, 500],
65+
n_outs = 10,
66+
corruption_levels = [0.1, 0.1]
67+
):
6368
""" This class is made to support a variable number of layers.
6469
6570
:type numpy_rng: numpy.random.RandomState
@@ -241,9 +246,9 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
241246
(test_set_x, test_set_y) = datasets[2]
242247

243248
# compute number of minibatches for training, validation and testing
244-
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
249+
n_valid_batches = valid_set_x.get_value(borrow = True).shape[0]
245250
n_valid_batches /= batch_size
246-
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
251+
n_test_batches = test_set_x.get_value(borrow = True).shape[0]
247252
n_test_batches /= batch_size
248253

249254
index = T.lscalar('index') # index to a [mini]batch
@@ -252,35 +257,41 @@ def build_finetune_functions(self, datasets, batch_size, learning_rate):
252257
gparams = T.grad(self.finetune_cost, self.params)
253258

254259
# compute list of fine-tuning updates
255-
updates = []
256-
for param, gparam in zip(self.params, gparams):
257-
updates.append((param, param - gparam * learning_rate))
258-
259-
train_fn = theano.function(inputs=[index],
260-
outputs=self.finetune_cost,
261-
updates=updates,
262-
givens={
263-
self.x: train_set_x[index * batch_size:
264-
(index + 1) * batch_size],
265-
self.y: train_set_y[index * batch_size:
266-
(index + 1) * batch_size]},
267-
name='train')
268-
269-
test_score_i = theano.function([index], self.errors,
270-
givens={
271-
self.x: test_set_x[index * batch_size:
272-
(index + 1) * batch_size],
273-
self.y: test_set_y[index * batch_size:
274-
(index + 1) * batch_size]},
275-
name='test')
276-
277-
valid_score_i = theano.function([index], self.errors,
278-
givens={
279-
self.x: valid_set_x[index * batch_size:
280-
(index + 1) * batch_size],
281-
self.y: valid_set_y[index * batch_size:
282-
(index + 1) * batch_size]},
283-
name='valid')
260+
updates = [
261+
(param, param - gparam * learning_rate)
262+
for param, gparam in zip(self.params, gparams)
263+
]
264+
265+
train_fn = theano.function(
266+
inputs = [index],
267+
outputs = self.finetune_cost,
268+
updates = updates,
269+
givens = {
270+
self.x: train_set_x[index * batch_size : (index + 1) * batch_size],
271+
self.y: train_set_y[index * batch_size : (index + 1) * batch_size]
272+
},
273+
name = 'train'
274+
)
275+
276+
test_score_i = theano.function(
277+
[index],
278+
self.errors,
279+
givens = {
280+
self.x: test_set_x[index * batch_size : (index + 1) * batch_size],
281+
self.y: test_set_y[index * batch_size : (index + 1) * batch_size]
282+
},
283+
name = 'test'
284+
)
285+
286+
valid_score_i = theano.function(
287+
[index],
288+
self.errors,
289+
givens = {
290+
self.x: valid_set_x[index * batch_size : (index + 1) * batch_size],
291+
self.y: valid_set_y[index * batch_size : (index + 1) * batch_size]
292+
},
293+
name='valid'
294+
)
284295

285296
# Create a function that scans the entire validation set
286297
def valid_score():
@@ -333,9 +344,12 @@ def test_SdA(finetune_lr=0.1, pretraining_epochs=15,
333344
numpy_rng = numpy.random.RandomState(89677)
334345
print '... building the model'
335346
# construct the stacked denoising autoencoder class
336-
sda = SdA(numpy_rng=numpy_rng, n_ins=28 * 28,
337-
hidden_layers_sizes=[1000, 1000, 1000],
338-
n_outs=10)
347+
sda = SdA(
348+
numpy_rng = numpy_rng,
349+
n_ins = 28 * 28,
350+
hidden_layers_sizes = [1000, 1000, 1000],
351+
n_outs = 10
352+
)
339353

340354
#########################
341355
# PRETRAINING THE MODEL #

Diff for: code/convolutional_mlp.py

+74-37
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,32 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
7676
numpy.prod(poolsize))
7777
# initialize weights with random weights
7878
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
79-
self.W = theano.shared(numpy.asarray(
80-
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
81-
dtype=theano.config.floatX),
82-
borrow=True)
79+
self.W = theano.shared(
80+
numpy.asarray(
81+
rng.uniform(low = -W_bound, high = W_bound, size = filter_shape),
82+
dtype = theano.config.floatX
83+
),
84+
borrow = True
85+
)
8386

8487
# the bias is a 1D tensor -- one bias per output feature map
8588
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
8689
self.b = theano.shared(value=b_values, borrow=True)
8790

8891
# convolve input feature maps with filters
89-
conv_out = conv.conv2d(input=input, filters=self.W,
90-
filter_shape=filter_shape, image_shape=image_shape)
92+
conv_out = conv.conv2d(
93+
input = input,
94+
filters = self.W,
95+
filter_shape = filter_shape,
96+
image_shape = image_shape
97+
)
9198

9299
# downsample each feature map individually, using maxpooling
93-
pooled_out = downsample.max_pool_2d(input=conv_out,
94-
ds=poolsize, ignore_border=True)
100+
pooled_out = downsample.max_pool_2d(
101+
input = conv_out,
102+
ds = poolsize,
103+
ignore_border = True
104+
)
95105

96106
# add the bias term. Since the bias is a vector (1D array), we first
97107
# reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
@@ -131,9 +141,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
131141
test_set_x, test_set_y = datasets[2]
132142

133143
# compute number of minibatches for training, validation and testing
134-
n_train_batches = train_set_x.get_value(borrow=True).shape[0]
135-
n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]
136-
n_test_batches = test_set_x.get_value(borrow=True).shape[0]
144+
n_train_batches = train_set_x.get_value(borrow = True).shape[0]
145+
n_valid_batches = valid_set_x.get_value(borrow = True).shape[0]
146+
n_test_batches = test_set_x.get_value(borrow = True).shape[0]
137147
n_train_batches /= batch_size
138148
n_valid_batches /= batch_size
139149
n_test_batches /= batch_size
@@ -159,43 +169,64 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
159169
# filtering reduces the image size to (28-5+1,28-5+1)=(24,24)
160170
# maxpooling reduces this further to (24/2,24/2) = (12,12)
161171
# 4D output tensor is thus of shape (batch_size,nkerns[0],12,12)
162-
layer0 = LeNetConvPoolLayer(rng, input=layer0_input,
163-
image_shape=(batch_size, 1, 28, 28),
164-
filter_shape=(nkerns[0], 1, 5, 5), poolsize=(2, 2))
172+
layer0 = LeNetConvPoolLayer(
173+
rng,
174+
input = layer0_input,
175+
image_shape = (batch_size, 1, 28, 28),
176+
filter_shape = (nkerns[0], 1, 5, 5),
177+
poolsize = (2, 2)
178+
)
165179

166180
# Construct the second convolutional pooling layer
167181
# filtering reduces the image size to (12-5+1,12-5+1)=(8,8)
168182
# maxpooling reduces this further to (8/2,8/2) = (4,4)
169183
# 4D output tensor is thus of shape (nkerns[0],nkerns[1],4,4)
170-
layer1 = LeNetConvPoolLayer(rng, input=layer0.output,
171-
image_shape=(batch_size, nkerns[0], 12, 12),
172-
filter_shape=(nkerns[1], nkerns[0], 5, 5), poolsize=(2, 2))
184+
layer1 = LeNetConvPoolLayer(
185+
rng,
186+
input = layer0.output,
187+
image_shape = (batch_size, nkerns[0], 12, 12),
188+
filter_shape = (nkerns[1], nkerns[0], 5, 5),
189+
poolsize = (2, 2)
190+
)
173191

174192
# the HiddenLayer being fully-connected, it operates on 2D matrices of
175193
# shape (batch_size,num_pixels) (i.e matrix of rasterized images).
176194
# This will generate a matrix of shape (20,32*4*4) = (20,512)
177195
layer2_input = layer1.output.flatten(2)
178196

179197
# construct a fully-connected sigmoidal layer
180-
layer2 = HiddenLayer(rng, input=layer2_input, n_in=nkerns[1] * 4 * 4,
181-
n_out=500, activation=T.tanh)
198+
layer2 = HiddenLayer(
199+
rng,
200+
input = layer2_input,
201+
n_in = nkerns[1] * 4 * 4,
202+
n_out = 500,
203+
activation = T.tanh
204+
)
182205

183206
# classify the values of the fully-connected sigmoidal layer
184-
layer3 = LogisticRegression(input=layer2.output, n_in=500, n_out=10)
207+
layer3 = LogisticRegression(input = layer2.output, n_in = 500, n_out = 10)
185208

186209
# the cost we minimize during training is the NLL of the model
187210
cost = layer3.negative_log_likelihood(y)
188211

189212
# create a function to compute the mistakes that are made by the model
190-
test_model = theano.function([index], layer3.errors(y),
191-
givens={
192-
x: test_set_x[index * batch_size: (index + 1) * batch_size],
193-
y: test_set_y[index * batch_size: (index + 1) * batch_size]})
194-
195-
validate_model = theano.function([index], layer3.errors(y),
196-
givens={
197-
x: valid_set_x[index * batch_size: (index + 1) * batch_size],
198-
y: valid_set_y[index * batch_size: (index + 1) * batch_size]})
213+
test_model = theano.function(
214+
[index],
215+
layer3.errors(y),
216+
givens = {
217+
x: test_set_x[index * batch_size : (index + 1) * batch_size],
218+
y: test_set_y[index * batch_size : (index + 1) * batch_size]
219+
}
220+
)
221+
222+
validate_model = theano.function(
223+
[index],
224+
layer3.errors(y),
225+
givens = {
226+
x: valid_set_x[index * batch_size : (index + 1) * batch_size],
227+
y: valid_set_y[index * batch_size : (index + 1) * batch_size]
228+
}
229+
)
199230

200231
# create a list of all model parameters to be fit by gradient descent
201232
params = layer3.params + layer2.params + layer1.params + layer0.params
@@ -208,14 +239,20 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
208239
# manually create an update rule for each model parameter. We thus
209240
# create the updates list by automatically looping over all
210241
# (params[i],grads[i]) pairs.
211-
updates = []
212-
for param_i, grad_i in zip(params, grads):
213-
updates.append((param_i, param_i - learning_rate * grad_i))
214-
215-
train_model = theano.function([index], cost, updates=updates,
216-
givens={
217-
x: train_set_x[index * batch_size: (index + 1) * batch_size],
218-
y: train_set_y[index * batch_size: (index + 1) * batch_size]})
242+
updates = [
243+
(param_i, param_i - learning_rate * grad_i)
244+
for param_i, grad_i in zip(params, grads)
245+
]
246+
247+
train_model = theano.function(
248+
[index],
249+
cost,
250+
updates = updates,
251+
givens = {
252+
x: train_set_x[index * batch_size : (index + 1) * batch_size],
253+
y: train_set_y[index * batch_size : (index + 1) * batch_size]
254+
}
255+
)
219256

220257
###############
221258
# TRAIN MODEL #

Diff for: code/dA.py

+48-20
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,17 @@ class dA(object):
7575
7676
"""
7777

78-
def __init__(self, numpy_rng, theano_rng=None, input=None,
79-
n_visible=784, n_hidden=500,
80-
W=None, bhid=None, bvis=None):
78+
def __init__(
79+
self,
80+
numpy_rng,
81+
theano_rng = None,
82+
input = None,
83+
n_visible = 784,
84+
n_hidden = 500,
85+
W = None,
86+
bhid = None,
87+
bvis = None
88+
):
8189
"""
8290
Initialize the dA class by specifying the number of visible units (the
8391
dimension d of the input ), the number of hidden units ( the dimension
@@ -232,9 +240,10 @@ def get_cost_updates(self, corruption_level, learning_rate):
232240
# to its parameters
233241
gparams = T.grad(cost, self.params)
234242
# generate the list of updates
235-
updates = []
236-
for param, gparam in zip(self.params, gparams):
237-
updates.append((param, param - learning_rate * gparam))
243+
updates = [
244+
(param, param - learning_rate * gparam)
245+
for param, gparam in zip(self.params, gparams)
246+
]
238247

239248
return (cost, updates)
240249

@@ -277,15 +286,27 @@ def test_dA(learning_rate=0.1, training_epochs=15,
277286
rng = numpy.random.RandomState(123)
278287
theano_rng = RandomStreams(rng.randint(2 ** 30))
279288

280-
da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
281-
n_visible=28 * 28, n_hidden=500)
282-
283-
cost, updates = da.get_cost_updates(corruption_level=0.,
284-
learning_rate=learning_rate)
285-
286-
train_da = theano.function([index], cost, updates=updates,
287-
givens={x: train_set_x[index * batch_size:
288-
(index + 1) * batch_size]})
289+
da = dA(
290+
numpy_rng = rng,
291+
theano_rng = theano_rng,
292+
input = x,
293+
n_visible = 28 * 28,
294+
n_hidden = 500
295+
)
296+
297+
cost, updates = da.get_cost_updates(
298+
corruption_level=0.,
299+
learning_rate=learning_rate
300+
)
301+
302+
train_da = theano.function(
303+
[index],
304+
cost,
305+
updates = updates,
306+
givens = {
307+
x: train_set_x[index * batch_size : (index + 1) * batch_size]
308+
}
309+
)
289310

290311
start_time = time.clock()
291312

@@ -322,11 +343,18 @@ def test_dA(learning_rate=0.1, training_epochs=15,
322343
rng = numpy.random.RandomState(123)
323344
theano_rng = RandomStreams(rng.randint(2 ** 30))
324345

325-
da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
326-
n_visible=28 * 28, n_hidden=500)
327-
328-
cost, updates = da.get_cost_updates(corruption_level=0.3,
329-
learning_rate=learning_rate)
346+
da = dA(
347+
numpy_rng = rng,
348+
theano_rng = theano_rng,
349+
input = x,
350+
n_visible = 28 * 28,
351+
n_hidden = 500
352+
)
353+
354+
cost, updates = da.get_cost_updates(
355+
corruption_level = 0.3,
356+
learning_rate=learning_rate
357+
)
330358

331359
train_da = theano.function([index], cost, updates=updates,
332360
givens={x: train_set_x[index * batch_size:

0 commit comments

Comments
 (0)