comparison scripts/stacked_dae.py @ 119:4f37755d301b

Refait stacked_dae.py en utilisant le dataset complet shared (juste reparti à 0 à partir du code du tutoriel), et préparé pour utiliser NIST (pas testé)
author fsavard
date Wed, 17 Feb 2010 17:06:54 -0500
parents 0b4080394f2c
children
comparison
equal deleted inserted replaced
118:0d083964af4b 119:4f37755d301b
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # Code for stacked denoising autoencoder 4 import numpy
5 # Tests with MNIST 5 import theano
6 # TODO: adapt for NIST
7 # Based almost entirely on deeplearning.net tutorial, modifications by
8 # François Savard
9
10 # Base LogisticRegression, SigmoidalLayer, dA, SdA code taken
11 # from the deeplearning.net tutorial. Refactored a bit.
12 # Changes (mainly):
13 # - splitted initialization in smaller methods
14 # - removed the "givens" thing involving an index in the whole dataset
15 # (to allow flexibility in how data is inputted... not necessarily one big tensor)
16 # - changed the "driver" a lot, altough for the moment the same logic is used
17
18 import time 6 import time
19 import theano
20 import theano.tensor as T 7 import theano.tensor as T
21 import theano.tensor.nnet
22 from theano.tensor.shared_randomstreams import RandomStreams 8 from theano.tensor.shared_randomstreams import RandomStreams
23 import numpy, numpy.random 9 import os.path
24 10
25 from pylearn.datasets import MNIST 11 import gzip
26 12 import cPickle
13
14 MNIST_LOCATION = '/u/savardf/datasets/mnist.pkl.gz'
27 15
28 # from pylearn codebase 16 # from pylearn codebase
29 def update_locals(obj, dct): 17 def update_locals(obj, dct):
30 if 'self' in dct: 18 if 'self' in dct:
31 del dct['self'] 19 del dct['self']
32 obj.__dict__.update(dct) 20 obj.__dict__.update(dct)
33 21
34
35 class LogisticRegression(object): 22 class LogisticRegression(object):
36 def __init__(self, input, n_in, n_out): 23 def __init__(self, input, n_in, n_out):
37 # initialize with 0 the weights W as a matrix of shape (n_in, n_out) 24 # initialize with 0 the weights W as a matrix of shape (n_in, n_out)
38 self.W = theano.shared(value=numpy.zeros((n_in,n_out), dtype = theano.config.floatX), 25 self.W = theano.shared( value=numpy.zeros((n_in,n_out),
39 name='W') 26 dtype = theano.config.floatX) )
40 # initialize the baises b as a vector of n_out 0s 27 # initialize the baises b as a vector of n_out 0s
41 self.b = theano.shared(value=numpy.zeros((n_out,), dtype = theano.config.floatX), 28 self.b = theano.shared( value=numpy.zeros((n_out,),
42 name='b') 29 dtype = theano.config.floatX) )
43
44 # compute vector of class-membership probabilities in symbolic form 30 # compute vector of class-membership probabilities in symbolic form
45 self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b) 31 self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W)+self.b)
46 32
47 # compute prediction as class whose probability is maximal in 33 # compute prediction as class whose probability is maximal in
48 # symbolic form 34 # symbolic form
49 self.y_pred=T.argmax(self.p_y_given_x, axis=1) 35 self.y_pred=T.argmax(self.p_y_given_x, axis=1)
50 36
37 # list of parameters for this layer
51 self.params = [self.W, self.b] 38 self.params = [self.W, self.b]
52 39
53 def negative_log_likelihood(self, y): 40 def negative_log_likelihood(self, y):
54 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y]) 41 return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]),y])
55 42
56 def errors(self, y): 43 def errors(self, y):
57 # check if y has same dimension of y_pred 44 # check if y has same dimension of y_pred
58 if y.ndim != self.y_pred.ndim: 45 if y.ndim != self.y_pred.ndim:
59 raise TypeError('y should have the same shape as self.y_pred', 46 raise TypeError('y should have the same shape as self.y_pred',
60 ('y', target.type, 'y_pred', self.y_pred.type)) 47 ('y', target.type, 'y_pred', self.y_pred.type))
48
61 # check if y is of the correct datatype 49 # check if y is of the correct datatype
62 if y.dtype.startswith('int'): 50 if y.dtype.startswith('int'):
63 # the T.neq operator returns a vector of 0s and 1s, where 1 51 # the T.neq operator returns a vector of 0s and 1s, where 1
64 # represents a mistake in prediction 52 # represents a mistake in prediction
65 return T.mean(T.neq(self.y_pred, y)) 53 return T.mean(T.neq(self.y_pred, y))
82 70
83 self.output = T.nnet.sigmoid(T.dot(input, self.W) + self.b) 71 self.output = T.nnet.sigmoid(T.dot(input, self.W) + self.b)
84 self.params = [self.W, self.b] 72 self.params = [self.W, self.b]
85 73
86 74
75
87 class dA(object): 76 class dA(object):
88 def __init__(self, n_visible= 784, n_hidden= 500, \ 77 def __init__(self, n_visible= 784, n_hidden= 500, corruption_level = 0.1,\
89 corruption_level = 0.1, input = None, \ 78 input = None, shared_W = None, shared_b = None):
90 shared_W = None, shared_b = None): 79 self.n_visible = n_visible
91 update_locals(self, locals()) 80 self.n_hidden = n_hidden
92 81
93 self.init_randomizer() 82 # create a Theano random generator that gives symbolic random values
94 self.init_params() 83 theano_rng = RandomStreams()
95 self.init_functions() 84
96 85 if shared_W != None and shared_b != None :
97 def init_randomizer(self): 86 self.W = shared_W
98 # create a Theano random generator that gives symbolic random values 87 self.b = shared_b
99 self.theano_rng = RandomStreams() 88 else:
100 # create a numpy random generator 89 # initial values for weights and biases
101 self.numpy_rng = numpy.random.RandomState() 90 # note : W' was written as `W_prime` and b' as `b_prime`
102 91
103 def init_params(self): 92 # W is initialized with `initial_W` which is uniformely sampled
104 if self.shared_W != None and self.shared_b != None : 93 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible)
105 self.W = self.shared_W 94 # the output of uniform if converted using asarray to dtype
106 self.b = self.shared_b 95 # theano.config.floatX so that the code is runable on GPU
107 else: 96 initial_W = numpy.asarray( numpy.random.uniform( \
108 # initial values for weights and biases 97 low = -numpy.sqrt(6./(n_hidden+n_visible)), \
109 # note : W' was written as `W_prime` and b' as `b_prime` 98 high = numpy.sqrt(6./(n_hidden+n_visible)), \
110 99 size = (n_visible, n_hidden)), dtype = theano.config.floatX)
111 # W is initialized with `initial_W` which is uniformely sampled 100 initial_b = numpy.zeros(n_hidden, dtype = theano.config.floatX)
112 # from -6./sqrt(n_visible+n_hidden) and 6./sqrt(n_hidden+n_visible) 101
113 # the output of uniform if converted using asarray to dtype 102
114 # theano.config.floatX so that the code is runable on GPU 103 # theano shared variables for weights and biases
115 initial_W = numpy.asarray( self.numpy_rng.uniform( \ 104 self.W = theano.shared(value = initial_W, name = "W")
116 low = -numpy.sqrt(6./(n_hidden+n_visible)), \ 105 self.b = theano.shared(value = initial_b, name = "b")
117 high = numpy.sqrt(6./(n_hidden+n_visible)), \ 106
118 size = (n_visible, n_hidden)), dtype = theano.config.floatX) 107
119 initial_b = numpy.zeros(n_hidden) 108 initial_b_prime= numpy.zeros(n_visible)
120 109 # tied weights, therefore W_prime is W transpose
121 # theano shared variables for weights and biases 110 self.W_prime = self.W.T
122 self.W = theano.shared(value = initial_W, name = "W") 111 self.b_prime = theano.shared(value = initial_b_prime, name = "b'")
123 self.b = theano.shared(value = initial_b, name = "b") 112
124 113 # if no input is given, generate a variable representing the input
125 initial_b_prime= numpy.zeros(self.n_visible) 114 if input == None :
126 # tied weights, therefore W_prime is W transpose 115 # we use a matrix because we expect a minibatch of several examples,
127 self.W_prime = self.W.T 116 # each example being a row
128 self.b_prime = theano.shared(value = initial_b_prime, name = "b'") 117 self.x = T.dmatrix(name = 'input')
129 118 else:
130 def init_functions(self): 119 self.x = input
131 # if no input is given, generate a variable representing the input 120 # Equation (1)
132 if self.input == None : 121 # keep 90% of the inputs the same and zero-out randomly selected subset of 10% of the inputs
133 # we use a matrix because we expect a minibatch of several examples, 122 # note : first argument of theano.rng.binomial is the shape(size) of
134 # each example being a row 123 # random numbers that it should produce
135 self.x = T.dmatrix(name = 'input') 124 # second argument is the number of trials
136 else: 125 # third argument is the probability of success of any trial
137 self.x = self.input 126 #
138 127 # this will produce an array of 0s and 1s where 1 has a
139 # keep 90% of the inputs the same and zero-out randomly selected subset of 128 # probability of 1 - ``corruption_level`` and 0 with
140 # 10% of the inputs 129 # ``corruption_level``
141 # note : first argument of theano.rng.binomial is the shape(size) of 130 self.tilde_x = theano_rng.binomial( self.x.shape, 1, 1 - corruption_level) * self.x
142 # random numbers that it should produce 131 # Equation (2)
143 # second argument is the number of trials 132 # note : y is stored as an attribute of the class so that it can be
144 # third argument is the probability of success of any trial 133 # used later when stacking dAs.
145 # 134 self.y = T.nnet.sigmoid(T.dot(self.tilde_x, self.W ) + self.b)
146 # this will produce an array of 0s and 1s where 1 has a 135 # Equation (3)
147 # probability of 1 - ``corruption_level`` and 0 with 136 self.z = T.nnet.sigmoid(T.dot(self.y, self.W_prime) + self.b_prime)
148 # ``corruption_level`` 137 # Equation (4)
149 self.tilde_x = self.theano_rng.binomial(self.x.shape, 1, 1-self.corruption_level) * self.x 138 # note : we sum over the size of a datapoint; if we are using minibatches,
150 # using tied weights 139 # L will be a vector, with one entry per example in minibatch
151 self.y = T.nnet.sigmoid(T.dot(self.tilde_x, self.W) + self.b) 140 self.L = - T.sum( self.x*T.log(self.z) + (1-self.x)*T.log(1-self.z), axis=1 )
152 self.z = T.nnet.sigmoid(T.dot(self.y, self.W_prime) + self.b_prime) 141 # note : L is now a vector, where each element is the cross-entropy cost
153 self.L = - T.sum( self.x*T.log(self.z) + (1-self.x)*T.log(1-self.z), axis=1 ) 142 # of the reconstruction of the corresponding example of the
154 # note : L is now a vector, where each element is the cross-entropy cost 143 # minibatch. We need to compute the average of all these to get
155 # of the reconstruction of the corresponding example of the 144 # the cost of the minibatch
156 # minibatch. We need to compute the average of all these to get 145 self.cost = T.mean(self.L)
157 # the cost of the minibatch 146
158 self.cost = T.mean(self.L) 147 self.params = [ self.W, self.b, self.b_prime ]
159 148
160 self.params = [ self.W, self.b, self.b_prime ] 149
161 150
162 class SdA(): 151
163 def __init__(self, batch_size, n_ins, 152 class SdA(object):
164 hidden_layers_sizes, n_outs, 153 def __init__(self, train_set_x, train_set_y, batch_size, n_ins,
165 corruption_levels, rng, pretrain_lr, finetune_lr): 154 hidden_layers_sizes, n_outs,
166 update_locals(self, locals()) 155 corruption_levels, rng, pretrain_lr, finetune_lr):
167 156
168 self.layers = [] 157 self.layers = []
169 self.pretrain_functions = [] 158 self.pretrain_functions = []
170 self.params = [] 159 self.params = []
171 self.n_layers = len(hidden_layers_sizes) 160 self.n_layers = len(hidden_layers_sizes)
172 161
173 if len(hidden_layers_sizes) < 1 : 162 if len(hidden_layers_sizes) < 1 :
174 raiseException (' You must have at least one hidden layer ') 163 raiseException (' You must have at least one hidden layer ')
175 164
165
176 # allocate symbolic variables for the data 166 # allocate symbolic variables for the data
177 self.x = T.matrix('x') # the data is presented as rasterized images 167 index = T.lscalar() # index to a [mini]batch
178 self.y = T.ivector('y') # the labels are presented as 1D vector of 168 self.x = T.matrix('x') # the data is presented as rasterized images
179 # [int] labels 169 self.y = T.ivector('y') # the labels are presented as 1D vector of
180 170 # [int] labels
181 self.create_layers() 171
182 self.init_finetuning()
183
184 def create_layers(self):
185 for i in xrange( self.n_layers ): 172 for i in xrange( self.n_layers ):
186 # construct the sigmoidal layer 173 # construct the sigmoidal layer
187 174
188 # the size of the input is either the number of hidden units of 175 # the size of the input is either the number of hidden units of
189 # the layer below or the input size if we are on the first layer 176 # the layer below or the input size if we are on the first layer
190 if i == 0 : 177 if i == 0 :
191 input_size = self.n_ins 178 input_size = n_ins
192 else: 179 else:
193 input_size = self.hidden_layers_sizes[i-1] 180 input_size = hidden_layers_sizes[i-1]
194 181
195 # the input to this layer is either the activation of the hidden 182 # the input to this layer is either the activation of the hidden
196 # layer below or the input of the SdA if you are on the first 183 # layer below or the input of the SdA if you are on the first
197 # layer 184 # layer
198 if i == 0 : 185 if i == 0 :
199 layer_input = self.x 186 layer_input = self.x
200 else: 187 else:
201 layer_input = self.layers[-1].output 188 layer_input = self.layers[-1].output
202 189
203 layer = SigmoidalLayer(self.rng, layer_input, input_size, 190 layer = SigmoidalLayer(rng, layer_input, input_size,
204 self.hidden_layers_sizes[i] ) 191 hidden_layers_sizes[i] )
205 # add the layer to the 192 # add the layer to the
206 self.layers += [layer] 193 self.layers += [layer]
207 self.params += layer.params 194 self.params += layer.params
208 195
209 # Construct a denoising autoencoder that shared weights with this 196 # Construct a denoising autoencoder that shared weights with this
210 # layer 197 # layer
211 dA_layer = dA(input_size, self.hidden_layers_sizes[i], \ 198 dA_layer = dA(input_size, hidden_layers_sizes[i], \
212 corruption_level = self.corruption_levels[0],\ 199 corruption_level = corruption_levels[0],\
213 input = layer_input, \ 200 input = layer_input, \
214 shared_W = layer.W, shared_b = layer.b) 201 shared_W = layer.W, shared_b = layer.b)
215 202
216 self.init_updates_for_layer(dA_layer) 203 # Construct a function that trains this dA
217 204 # compute gradients of layer parameters
218 def init_updates_for_layer(self, dA_layer): 205 gparams = T.grad(dA_layer.cost, dA_layer.params)
219 # Construct a function that trains this dA 206 # compute the list of updates
220 # compute gradients of layer parameters 207 updates = {}
221 gparams = T.grad(dA_layer.cost, dA_layer.params) 208 for param, gparam in zip(dA_layer.params, gparams):
222 # compute the list of updates 209 updates[param] = param - gparam * pretrain_lr
223 updates = {} 210
224 for param, gparam in zip(dA_layer.params, gparams): 211 # create a function that trains the dA
225 updates[param] = param - gparam * self.pretrain_lr 212 update_fn = theano.function([index], dA_layer.cost, \
226 213 updates = updates,
227 # create a function that trains the dA 214 givens = {
228 update_fn = theano.function([self.x], dA_layer.cost, \ 215 self.x : train_set_x[index*batch_size:(index+1)*batch_size]})
229 updates = updates) 216 # collect this function into a list
230 217 self.pretrain_functions += [update_fn]
231 # collect this function into a list 218
232 self.pretrain_functions += [update_fn] 219
233
234 def init_finetuning(self):
235 # We now need to add a logistic layer on top of the MLP 220 # We now need to add a logistic layer on top of the MLP
236 self.logLayer = LogisticRegression(\ 221 self.logLayer = LogisticRegression(\
237 input = self.layers[-1].output,\ 222 input = self.layers[-1].output,\
238 n_in = self.hidden_layers_sizes[-1], n_out = self.n_outs) 223 n_in = hidden_layers_sizes[-1], n_out = n_outs)
239 224
240 self.params += self.logLayer.params 225 self.params += self.logLayer.params
241 # construct a function that implements one step of finetunining 226 # construct a function that implements one step of finetunining
242 227
243 # compute the cost, defined as the negative log likelihood 228 # compute the cost, defined as the negative log likelihood
244 cost = self.logLayer.negative_log_likelihood(self.y) 229 cost = self.logLayer.negative_log_likelihood(self.y)
245 # compute the gradients with respect to the model parameters 230 # compute the gradients with respect to the model parameters
246 gparams = T.grad(cost, self.params) 231 gparams = T.grad(cost, self.params)
247 # compute list of updates 232 # compute list of updates
248 updates = {} 233 updates = {}
249 for param,gparam in zip(self.params, gparams): 234 for param,gparam in zip(self.params, gparams):
250 updates[param] = param - gparam*self.finetune_lr 235 updates[param] = param - gparam*finetune_lr
251 236
252 self.finetune = theano.function([self.x, self.y], cost, 237 self.finetune = theano.function([index], cost,
253 updates = updates) 238 updates = updates,
239 givens = {
240 self.x : train_set_x[index*batch_size:(index+1)*batch_size],
241 self.y : train_set_y[index*batch_size:(index+1)*batch_size]} )
254 242
255 # symbolic variable that points to the number of errors made on the 243 # symbolic variable that points to the number of errors made on the
256 # minibatch given by self.x and self.y 244 # minibatch given by self.x and self.y
257 245
258 self.errors = self.logLayer.errors(self.y) 246 self.errors = self.logLayer.errors(self.y)
259 247
260 class MnistIterators: 248 class Hyperparameters:
261 def __init__(self, minibatch_size): 249 def __init__(self, dict):
250 self.__dict__.update(dict)
251
252 def sgd_optimization_mnist(learning_rate=0.1, pretraining_epochs = 2, \
253 pretrain_lr = 0.1, training_epochs = 5, \
254 dataset='mnist.pkl.gz'):
255 # Load the dataset
256 f = gzip.open(dataset,'rb')
257 # this gives us train, valid, test (each with .x, .y)
258 dataset = cPickle.load(f)
259 f.close()
260
261 n_ins = 28*28
262 n_outs = 10
263
264 hyperparameters = Hyperparameters({'finetuning_lr':learning_rate,
265 'pretraining_lr':pretrain_lr,
266 'pretraining_epochs_per_layer':pretraining_epochs,
267 'max_finetuning_epochs':training_epochs,
268 'hidden_layers_sizes':[1000,1000,1000],
269 'corruption_levels':[0.2,0.2,0.2],
270 'minibatch_size':20})
271
272 sgd_optimization(dataset, hyperparameters, n_ins, n_outs)
273
274 class NIST:
275 def __init__(self, minibatch_size, basepath=='/data/lisa/data/nist/by_class/all'):
262 self.minibatch_size = minibatch_size 276 self.minibatch_size = minibatch_size
263 277 self.basepath = basepath
264 self.mnist = MNIST.first_1k() 278
265 279 self.train = [None, None]
266 self.len_train = len(self.mnist.train.x) 280 self.test = [None, None]
267 self.len_valid = len(self.mnist.valid.x) 281
268 self.len_test = len(self.mnist.test.x) 282 self.load_train_test()
269 283
270 def train_x_batches(self): 284 self.valid = [None, None]
271 idx = 0 285 self.split_train_valid()
272 while idx < len(self.mnist.train.x): 286
273 yield self.mnist.train.x[idx:idx+self.minibatch_size] 287 def set_filenames(self):
274 idx += self.minibatch_size 288 self.train_files = ['all_train_data.ft',
275 289 'all_train_labels.ft']
276 def train_xy_batches(self): 290
277 idx = 0 291 self.test_files = ['all_test_data.ft',
278 while idx < len(self.mnist.train.x): 292 'all_test_labels.ft']
279 mb_x = self.mnist.train.x[idx:idx+self.minibatch_size] 293
280 mb_y = self.mnist.train.y[idx:idx+self.minibatch_size] 294 def load_train_test(self):
281 yield mb_x, mb_y 295 self.load_data_labels(self.train_files, self.train)
282 idx += self.minibatch_size 296 self.load_data_labels(self.test_files, self.test)
283 297
284 def valid_xy_batches(self): 298 def load_data_labels(self, filenames, pair):
285 idx = 0 299 for i, fn in enumerate(filenames):
286 while idx < len(self.mnist.valid.x): 300 f = open(fn)
287 mb_x = self.mnist.valid.x[idx:idx+self.minibatch_size] 301 pair[i] = ft.read(os.path.join(self.base_path, fn))
288 mb_y = self.mnist.valid.y[idx:idx+self.minibatch_size] 302 f.close()
289 yield mb_x, mb_y 303
290 idx += self.minibatch_size 304 def split_train_valid(self):
291 305 test_len = len(self.test[0])
292 306
293 class MnistTrainingDriver: 307 new_train_x = self.train[0][:-test_len]
294 def __init__(self, rng=numpy.random): 308 new_train_y = self.train[1][:-test_len]
295 self.rng = rng 309
296 310 self.valid[0] = self.train[0][-test_len:]
297 self.init_SdA() 311 self.valid[1] = self.train[1][-test_len:]
298 312
299 def init_SdA(self): 313 self.train[0] = new_train_x
300 # Hyperparam 314 self.train[1] = new_train_y
301 hidden_layers_sizes = [1000, 1000, 1000] 315
302 n_outs = 10 316 def sgd_optimization_nist(dataset_dir='/data/lisa/data/nist'):
303 corruption_levels = [0.2, 0.2, 0.2] 317 pass
304 minibatch_size = 10 318
305 pretrain_lr = 0.001 319 def sgd_optimization(dataset, hyperparameters, n_ins, n_outs):
306 finetune_lr = 0.001 320 hp = hyperparameters
307 321
308 update_locals(self, locals()) 322 train_set, valid_set, test_set = dataset
309 323
310 self.mnist = MnistIterators(minibatch_size) 324 def shared_dataset(data_xy):
311 325 data_x, data_y = data_xy
312 # construct the stacked denoising autoencoder class 326 shared_x = theano.shared(numpy.asarray(data_x, dtype=theano.config.floatX))
313 self.classifier = SdA( batch_size = minibatch_size, \ 327 shared_y = theano.shared(numpy.asarray(data_y, dtype=theano.config.floatX))
314 n_ins=28*28, \ 328 return shared_x, T.cast(shared_y, 'int32')
315 hidden_layers_sizes = hidden_layers_sizes, \ 329
316 n_outs=n_outs, \ 330 test_set_x, test_set_y = shared_dataset(test_set)
317 corruption_levels = corruption_levels,\ 331 valid_set_x, valid_set_y = shared_dataset(valid_set)
318 rng = self.rng,\ 332 train_set_x, train_set_y = shared_dataset(train_set)
319 pretrain_lr = pretrain_lr, \ 333
320 finetune_lr = finetune_lr) 334 # compute number of minibatches for training, validation and testing
321 335 n_train_batches = train_set_x.value.shape[0] / hp.minibatch_size
322 def compute_validation_error(self): 336 n_valid_batches = valid_set_x.value.shape[0] / hp.minibatch_size
323 validation_error = 0.0 337 n_test_batches = test_set_x.value.shape[0] / hp.minibatch_size
324 338
325 count = 0 339 # allocate symbolic variables for the data
326 for mb_x, mb_y in self.mnist.valid_xy_batches(): 340 index = T.lscalar() # index to a [mini]batch
327 validation_error += self.classifier.errors(mb_x, mb_y) 341
328 count += 1 342 # construct the stacked denoising autoencoder class
329 343 classifier = SdA( train_set_x=train_set_x, train_set_y = train_set_y,\
330 return float(validation_error) / count 344 batch_size = hp.minibatch_size, n_ins= n_ins, \
331 345 hidden_layers_sizes = hp.hidden_layers_sizes, n_outs=10, \
332 def pretrain(self): 346 corruption_levels = hp.corruption_levels,\
333 pretraining_epochs = 20 347 rng = numpy.random.RandomState(1234),\
334 348 pretrain_lr = hp.pretraining_lr, finetune_lr = hp.finetuning_lr )
335 for layer_idx, update_fn in enumerate(self.classifier.pretrain_functions): 349
336 for epoch in xrange(pretraining_epochs): 350
337 # go through the training set 351 start_time = time.clock()
338 cost_acc = 0.0 352 ## Pre-train layer-wise
339 for i, mb_x in enumerate(self.mnist.train_x_batches()): 353 for i in xrange(classifier.n_layers):
340 cost_acc += update_fn(mb_x) 354 # go through pretraining epochs
341 355 for epoch in xrange(hp.pretraining_epochs_per_layer):
342 if i % 100 == 0: 356 # go through the training set
343 print i, "avg err = ", cost_acc / 100.0 357 for batch_index in xrange(n_train_batches):
344 cost_acc = 0.0 358 c = classifier.pretrain_functions[i](batch_index)
345 print 'Pre-training layer %d, epoch %d' % (layer_idx, epoch) 359 print 'Pre-training layer %i, epoch %d, cost '%(i,epoch),c
346 360
347 def finetune(self): 361 end_time = time.clock()
348 max_training_epochs = 1000 362
349 363 print ('Pretraining took %f minutes' %((end_time-start_time)/60.))
350 n_train_batches = self.mnist.len_train / self.minibatch_size 364 # Fine-tune the entire model
351 365
352 # early-stopping parameters 366 minibatch_size = hp.minibatch_size
353 patience = 10000 # look as this many examples regardless 367
354 patience_increase = 2. # wait this much longer when a new best is 368 # create a function to compute the mistakes that are made by the model
355 # found 369 # on the validation set, or testing set
356 improvement_threshold = 0.995 # a relative improvement of this much is 370 test_model = theano.function([index], classifier.errors,
357 # considered significant 371 givens = {
358 validation_frequency = min(n_train_batches, patience/2) 372 classifier.x: test_set_x[index*minibatch_size:(index+1)*minibatch_size],
359 # go through this many 373 classifier.y: test_set_y[index*minibatch_size:(index+1)*minibatch_size]})
360 # minibatche before checking the network 374
361 # on the validation set; in this case we 375 validate_model = theano.function([index], classifier.errors,
362 # check every epoch 376 givens = {
363 377 classifier.x: valid_set_x[index*minibatch_size:(index+1)*minibatch_size],
364 378 classifier.y: valid_set_y[index*minibatch_size:(index+1)*minibatch_size]})
365 # TODO: use this 379
366 best_params = None 380
367 best_validation_loss = float('inf') 381 # early-stopping parameters
368 test_score = 0. 382 patience = 10000 # look as this many examples regardless
369 start_time = time.clock() 383 patience_increase = 2. # wait this much longer when a new best is
370 384 # found
371 done_looping = False 385 improvement_threshold = 0.995 # a relative improvement of this much is
372 epoch = 0 386 # considered significant
373 387 validation_frequency = min(n_train_batches, patience/2)
374 while (epoch < max_training_epochs) and (not done_looping): 388 # go through this many
375 epoch = epoch + 1 389 # minibatche before checking the network
376 for minibatch_index, (mb_x, mb_y) in enumerate(self.mnist.train_xy_batches()): 390 # on the validation set; in this case we
377 cost_ij = classifier.finetune(mb_x, mb_y) 391 # check every epoch
378 iter = epoch * n_train_batches + minibatch_index 392
379 393 best_params = None
380 if (iter+1) % validation_frequency == 0: 394 best_validation_loss = float('inf')
381 this_validation_loss = self.compute_validation_error() 395 test_score = 0.
382 print('epoch %i, minibatch %i/%i, validation error %f %%' % \ 396 start_time = time.clock()
383 (epoch, minibatch_index+1, n_train_batches, \ 397
384 this_validation_loss*100.)) 398 done_looping = False
385 399 epoch = 0
386 # if we got the best validation score until now 400
387 if this_validation_loss < best_validation_loss: 401 while (epoch < hp.max_finetuning_epochs) and (not done_looping):
388 402 epoch = epoch + 1
389 #improve patience if loss improvement is good enough 403 for minibatch_index in xrange(n_train_batches):
390 if this_validation_loss < best_validation_loss * \ 404
391 improvement_threshold : 405 cost_ij = classifier.finetune(minibatch_index)
392 patience = max(patience, iter * patience_increase) 406 iter = epoch * n_train_batches + minibatch_index
393 print "Improving patience" 407
394 408 if (iter+1) % validation_frequency == 0:
395 # save best validation score and iteration number 409
396 best_validation_loss = this_validation_loss 410 validation_losses = [validate_model(i) for i in xrange(n_valid_batches)]
397 best_iter = iter 411 this_validation_loss = numpy.mean(validation_losses)
398 412 print('epoch %i, minibatch %i/%i, validation error %f %%' % \
399 # test it on the test set 413 (epoch, minibatch_index+1, n_train_batches, \
400 #test_losses = [test_model(i) for i in xrange(n_test_batches)] 414 this_validation_loss*100.))
401 #test_score = numpy.mean(test_losses) 415
402 #print((' epoch %i, minibatch %i/%i, test error of best ' 416
403 # 'model %f %%') % 417 # if we got the best validation score until now
404 # (epoch, minibatch_index+1, n_train_batches, 418 if this_validation_loss < best_validation_loss:
405 # test_score*100.)) 419
406 420 #improve patience if loss improvement is good enough
407 421 if this_validation_loss < best_validation_loss * \
408 if patience <= iter : 422 improvement_threshold :
423 patience = max(patience, iter * patience_increase)
424
425 # save best validation score and iteration number
426 best_validation_loss = this_validation_loss
427 best_iter = iter
428
429 # test it on the test set
430 test_losses = [test_model(i) for i in xrange(n_test_batches)]
431 test_score = numpy.mean(test_losses)
432 print((' epoch %i, minibatch %i/%i, test error of best '
433 'model %f %%') %
434 (epoch, minibatch_index+1, n_train_batches,
435 test_score*100.))
436
437
438 if patience <= iter :
409 done_looping = True 439 done_looping = True
410 break 440 break
411 441
412 def train():
413 driver = MnistTrainingDriver()
414 start_time = time.clock()
415 driver.pretrain()
416 print "PRETRAINING DONE. STARTING FINETUNING."
417 driver.finetune()
418 end_time = time.clock() 442 end_time = time.clock()
443 print(('Optimization complete with best validation score of %f %%,'
444 'with test performance %f %%') %
445 (best_validation_loss * 100., test_score*100.))
446 print ('The code ran for %f minutes' % ((end_time-start_time)/60.))
447
419 448
420 if __name__ == '__main__': 449 if __name__ == '__main__':
421 train() 450 import sys
422 451 args = sys.argv[1:]
452 if len(args) > 0 and args[0] == "jobman_add":
453 jobman_add()
454 else:
455 sgd_optimization_mnist(dataset=MNIST_LOCATION)
456