Generates a function `train` that implements one step of finetuning, a function `validate` that computes the error on a batch from the validation set, and a function `test` that computes the error on a batch from the testing set :type datasets: list of pairs of thean
(self, datasets, batch_size, learning_rate)
| 232 | return pretrain_fns |
| 233 | |
| 234 | def build_finetune_functions(self, datasets, batch_size, learning_rate): |
| 235 | '''Generates a function `train` that implements one step of |
| 236 | finetuning, a function `validate` that computes the error on |
| 237 | a batch from the validation set, and a function `test` that |
| 238 | computes the error on a batch from the testing set |
| 239 | |
| 240 | :type datasets: list of pairs of theano.tensor.TensorType |
| 241 | :param datasets: It is a list that contain all the datasets; |
| 242 | the has to contain three pairs, `train`, |
| 243 | `valid`, `test` in this order, where each pair |
| 244 | is formed of two Theano variables, one for the |
| 245 | datapoints, the other for the labels |
| 246 | |
| 247 | :type batch_size: int |
| 248 | :param batch_size: size of a minibatch |
| 249 | |
| 250 | :type learning_rate: float |
| 251 | :param learning_rate: learning rate used during finetune stage |
| 252 | ''' |
| 253 | |
| 254 | (train_set_x, train_set_y) = datasets[0] |
| 255 | (valid_set_x, valid_set_y) = datasets[1] |
| 256 | (test_set_x, test_set_y) = datasets[2] |
| 257 | |
| 258 | # compute number of minibatches for training, validation and testing |
| 259 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] |
| 260 | n_valid_batches //= batch_size |
| 261 | n_test_batches = test_set_x.get_value(borrow=True).shape[0] |
| 262 | n_test_batches //= batch_size |
| 263 | |
| 264 | index = T.lscalar('index') # index to a [mini]batch |
| 265 | |
| 266 | # compute the gradients with respect to the model parameters |
| 267 | gparams = T.grad(self.finetune_cost, self.params) |
| 268 | |
| 269 | # compute list of fine-tuning updates |
| 270 | updates = [ |
| 271 | (param, param - gparam * learning_rate) |
| 272 | for param, gparam in zip(self.params, gparams) |
| 273 | ] |
| 274 | |
| 275 | train_fn = theano.function( |
| 276 | inputs=[index], |
| 277 | outputs=self.finetune_cost, |
| 278 | updates=updates, |
| 279 | givens={ |
| 280 | self.x: train_set_x[ |
| 281 | index * batch_size: (index + 1) * batch_size |
| 282 | ], |
| 283 | self.y: train_set_y[ |
| 284 | index * batch_size: (index + 1) * batch_size |
| 285 | ] |
| 286 | }, |
| 287 | name='train' |
| 288 | ) |
| 289 | |
| 290 | test_score_i = theano.function( |
| 291 | [index], |