21
21
http://yann.lecun.com/exdb/publis/pdf/lecun-98.pdf
22
22
23
23
"""
24
- import cPickle
25
- import gzip
26
24
import os
27
25
import sys
28
26
import time
@@ -53,14 +51,14 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
53
51
54
52
:type filter_shape: tuple or list of length 4
55
53
:param filter_shape: (number of filters, num input feature maps,
56
- filter height,filter width)
54
+ filter height, filter width)
57
55
58
56
:type image_shape: tuple or list of length 4
59
57
:param image_shape: (batch size, num input feature maps,
60
58
image height, image width)
61
59
62
60
:type poolsize: tuple or list of length 2
63
- :param poolsize: the downsampling (pooling) factor (#rows,#cols)
61
+ :param poolsize: the downsampling (pooling) factor (#rows, #cols)
64
62
"""
65
63
66
64
assert image_shape [1 ] == filter_shape [1 ]
@@ -104,7 +102,7 @@ def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
104
102
)
105
103
106
104
# add the bias term. Since the bias is a vector (1D array), we first
107
- # reshape it to a tensor of shape (1,n_filters,1, 1). Each bias will
105
+ # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will
108
106
# thus be broadcasted across mini-batches and feature map
109
107
# width & height
110
108
self .output = T .tanh (pooled_out + self .b .dimshuffle ('x' , 0 , 'x' , 'x' ))
@@ -155,21 +153,21 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
155
153
x = T .matrix ('x' ) # the data is presented as rasterized images
156
154
y = T .ivector ('y' ) # the labels are presented as 1D vector of
157
155
# [int] labels
158
- ishape = (28 , 28 ) # this is the size of MNIST images
159
156
160
157
######################
161
158
# BUILD ACTUAL MODEL #
162
159
######################
163
160
print '... building the model'
164
161
165
- # Reshape matrix of rasterized images of shape (batch_size,28* 28)
162
+ # Reshape matrix of rasterized images of shape (batch_size, 28 * 28)
166
163
# to a 4D tensor, compatible with our LeNetConvPoolLayer
164
+ # (28, 28) is the size of MNIST images.
167
165
layer0_input = x .reshape ((batch_size , 1 , 28 , 28 ))
168
166
169
167
# Construct the first convolutional pooling layer:
170
- # filtering reduces the image size to (28-5+1, 28-5+1)= (24,24)
171
- # maxpooling reduces this further to (24/2,24/2) = (12,12)
172
- # 4D output tensor is thus of shape (batch_size,nkerns[0],12,12)
168
+ # filtering reduces the image size to (28-5+1 , 28-5+1) = (24, 24)
169
+ # maxpooling reduces this further to (24/2, 24/2) = (12, 12)
170
+ # 4D output tensor is thus of shape (batch_size, nkerns[0], 12, 12)
173
171
layer0 = LeNetConvPoolLayer (
174
172
rng ,
175
173
input = layer0_input ,
@@ -179,9 +177,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
179
177
)
180
178
181
179
# Construct the second convolutional pooling layer
182
- # filtering reduces the image size to (12-5+1,12-5+1)= (8,8)
183
- # maxpooling reduces this further to (8/2,8/2) = (4,4)
184
- # 4D output tensor is thus of shape (nkerns[0],nkerns[1],4, 4)
180
+ # filtering reduces the image size to (12-5+1, 12-5+1) = (8, 8)
181
+ # maxpooling reduces this further to (8/2, 8/2) = (4, 4)
182
+ # 4D output tensor is thus of shape (nkerns[0], nkerns[1], 4, 4)
185
183
layer1 = LeNetConvPoolLayer (
186
184
rng ,
187
185
input = layer0 .output ,
@@ -191,8 +189,9 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
191
189
)
192
190
193
191
# the HiddenLayer being fully-connected, it operates on 2D matrices of
194
- # shape (batch_size,num_pixels) (i.e matrix of rasterized images).
195
- # This will generate a matrix of shape (20,32*4*4) = (20,512)
192
+ # shape (batch_size, num_pixels) (i.e matrix of rasterized images).
193
+ # This will generate a matrix of shape (batch_size, nkerns[1] * 4 * 4),
194
+ # or (500, 50 * 4 * 4) = (500, 800) with the default values.
196
195
layer2_input = layer1 .output .flatten (2 )
197
196
198
197
# construct a fully-connected sigmoidal layer
@@ -239,7 +238,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
239
238
# SGD Since this model has many parameters, it would be tedious to
240
239
# manually create an update rule for each model parameter. We thus
241
240
# create the updates list by automatically looping over all
242
- # (params[i],grads[i]) pairs.
241
+ # (params[i], grads[i]) pairs.
243
242
updates = [
244
243
(param_i , param_i - learning_rate * grad_i )
245
244
for param_i , grad_i in zip (params , grads )
@@ -272,7 +271,6 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
272
271
# on the validation set; in this case we
273
272
# check every epoch
274
273
275
- best_params = None
276
274
best_validation_loss = numpy .inf
277
275
best_iter = 0
278
276
test_score = 0.
@@ -330,7 +328,7 @@ def evaluate_lenet5(learning_rate=0.1, n_epochs=200,
330
328
331
329
end_time = time .clock ()
332
330
print ('Optimization complete.' )
333
- print ('Best validation score of %f %% obtained at iteration %i,'
331
+ print ('Best validation score of %f %% obtained at iteration %i, '
334
332
'with test performance %f %%' %
335
333
(best_validation_loss * 100. , best_iter + 1 , test_score * 100. ))
336
334
print >> sys .stderr , ('The code for file ' +
0 commit comments