@@ -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 #
0 commit comments