@@ -78,8 +78,8 @@ def shared(data):
78
78
return [shared (training_data ), shared (validation_data ), shared (test_data )]
79
79
80
80
#### Main class used to construct and train networks
81
- class Network ():
82
-
81
+ class Network (object ):
82
+
83
83
def __init__ (self , layers , mini_batch_size ):
84
84
"""Takes a list of `layers`, describing the network architecture, and
85
85
a value for the `mini_batch_size` to be used during training
@@ -89,7 +89,7 @@ def __init__(self, layers, mini_batch_size):
89
89
self .layers = layers
90
90
self .mini_batch_size = mini_batch_size
91
91
self .params = [param for layer in self .layers for param in layer .params ]
92
- self .x = T .matrix ("x" )
92
+ self .x = T .matrix ("x" )
93
93
self .y = T .ivector ("y" )
94
94
init_layer = self .layers [0 ]
95
95
init_layer .set_inpt (self .x , self .x , self .mini_batch_size )
@@ -100,7 +100,7 @@ def __init__(self, layers, mini_batch_size):
100
100
self .output = self .layers [- 1 ].output
101
101
self .output_dropout = self .layers [- 1 ].output_dropout
102
102
103
- def SGD (self , training_data , epochs , mini_batch_size , eta ,
103
+ def SGD (self , training_data , epochs , mini_batch_size , eta ,
104
104
validation_data , test_data , lmbda = 0.0 ):
105
105
"""Train the network using mini-batch stochastic gradient descent."""
106
106
training_x , training_y = training_data
@@ -117,7 +117,7 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
117
117
cost = self .layers [- 1 ].cost (self )+ \
118
118
0.5 * lmbda * l2_norm_squared / num_training_batches
119
119
grads = T .grad (cost , self .params )
120
- updates = [(param , param - eta * grad )
120
+ updates = [(param , param - eta * grad )
121
121
for param , grad in zip (self .params , grads )]
122
122
123
123
# define functions to train a mini-batch, and to compute the
@@ -128,37 +128,37 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
128
128
givens = {
129
129
self .x :
130
130
training_x [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ],
131
- self .y :
131
+ self .y :
132
132
training_y [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ]
133
133
})
134
134
validate_mb_accuracy = theano .function (
135
135
[i ], self .layers [- 1 ].accuracy (self .y ),
136
136
givens = {
137
- self .x :
137
+ self .x :
138
138
validation_x [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ],
139
- self .y :
139
+ self .y :
140
140
validation_y [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ]
141
141
})
142
142
test_mb_accuracy = theano .function (
143
143
[i ], self .layers [- 1 ].accuracy (self .y ),
144
144
givens = {
145
- self .x :
145
+ self .x :
146
146
test_x [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ],
147
- self .y :
147
+ self .y :
148
148
test_y [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ]
149
149
})
150
150
self .test_mb_predictions = theano .function (
151
151
[i ], self .layers [- 1 ].y_out ,
152
152
givens = {
153
- self .x :
153
+ self .x :
154
154
test_x [i * self .mini_batch_size : (i + 1 )* self .mini_batch_size ]
155
155
})
156
156
# Do the actual training
157
157
best_validation_accuracy = 0.0
158
158
for epoch in xrange (epochs ):
159
159
for minibatch_index in xrange (num_training_batches ):
160
160
iteration = num_training_batches * epoch + minibatch_index
161
- if iteration % 1000 == 0 :
161
+ if iteration % 1000 == 0 :
162
162
print ("Training mini-batch number {0}" .format (iteration ))
163
163
cost_ij = train_mb (minibatch_index )
164
164
if (iteration + 1 ) % num_training_batches == 0 :
@@ -182,18 +182,18 @@ def SGD(self, training_data, epochs, mini_batch_size, eta,
182
182
183
183
#### Define layer types
184
184
185
- class ConvPoolLayer ():
185
+ class ConvPoolLayer (object ):
186
186
"""Used to create a combination of a convolutional and a max-pooling
187
187
layer. A more sophisticated implementation would separate the
188
188
two, but for our purposes we'll always use them together, and it
189
189
simplifies the code, so it makes sense to combine them.
190
190
191
191
"""
192
192
193
- def __init__ (self , filter_shape , image_shape , poolsize = (2 , 2 ),
193
+ def __init__ (self , filter_shape , image_shape , poolsize = (2 , 2 ),
194
194
activation_fn = sigmoid ):
195
195
"""`filter_shape` is a tuple of length 4, whose entries are the number
196
- of filters, the number of input feature maps, the filter height, and the
196
+ of filters, the number of input feature maps, the filter height, and the
197
197
filter width.
198
198
199
199
`image_shape` is a tuple of length 4, whose entries are the
@@ -233,7 +233,7 @@ def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
233
233
pooled_out + self .b .dimshuffle ('x' , 0 , 'x' , 'x' ))
234
234
self .output_dropout = self .output # no dropout in the convolutional layers
235
235
236
- class FullyConnectedLayer ():
236
+ class FullyConnectedLayer (object ):
237
237
238
238
def __init__ (self , n_in , n_out , activation_fn = sigmoid , p_dropout = 0.0 ):
239
239
self .n_in = n_in
@@ -267,7 +267,7 @@ def accuracy(self, y):
267
267
"Return the accuracy for the mini-batch."
268
268
return T .mean (T .eq (y , self .y_out ))
269
269
270
- class SoftmaxLayer ():
270
+ class SoftmaxLayer (object ):
271
271
272
272
def __init__ (self , n_in , n_out , p_dropout = 0.0 ):
273
273
self .n_in = n_in
0 commit comments