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)
| 189 | return pretrain_fns |
| 190 | |
| 191 | def build_finetune_functions(self, datasets, batch_size, learning_rate): |
| 192 | '''Generates a function `train` that implements one step of |
| 193 | finetuning, a function `validate` that computes the error on a |
| 194 | batch from the validation set, and a function `test` that |
| 195 | computes the error on a batch from the testing set |
| 196 | |
| 197 | :type datasets: list of pairs of theano.tensor.TensorType |
| 198 | :param datasets: It is a list that contain all the datasets; |
| 199 | the has to contain three pairs, `train`, |
| 200 | `valid`, `test` in this order, where each pair |
| 201 | is formed of two Theano variables, one for the |
| 202 | datapoints, the other for the labels |
| 203 | :type batch_size: int |
| 204 | :param batch_size: size of a minibatch |
| 205 | :type learning_rate: float |
| 206 | :param learning_rate: learning rate used during finetune stage |
| 207 | |
| 208 | ''' |
| 209 | |
| 210 | (train_set_x, train_set_y) = datasets[0] |
| 211 | (valid_set_x, valid_set_y) = datasets[1] |
| 212 | (test_set_x, test_set_y) = datasets[2] |
| 213 | |
| 214 | # compute number of minibatches for training, validation and testing |
| 215 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] |
| 216 | n_valid_batches //= batch_size |
| 217 | n_test_batches = test_set_x.get_value(borrow=True).shape[0] |
| 218 | n_test_batches //= batch_size |
| 219 | |
| 220 | index = T.lscalar('index') # index to a [mini]batch |
| 221 | |
| 222 | # compute the gradients with respect to the model parameters |
| 223 | gparams = T.grad(self.finetune_cost, self.params) |
| 224 | |
| 225 | # compute list of fine-tuning updates |
| 226 | updates = [] |
| 227 | for param, gparam in zip(self.params, gparams): |
| 228 | updates.append((param, param - gparam * learning_rate)) |
| 229 | |
| 230 | train_fn = theano.function( |
| 231 | inputs=[index], |
| 232 | outputs=self.finetune_cost, |
| 233 | updates=updates, |
| 234 | givens={ |
| 235 | self.x: train_set_x[ |
| 236 | index * batch_size: (index + 1) * batch_size |
| 237 | ], |
| 238 | self.y: train_set_y[ |
| 239 | index * batch_size: (index + 1) * batch_size |
| 240 | ] |
| 241 | } |
| 242 | ) |
| 243 | |
| 244 | test_score_i = theano.function( |
| 245 | [index], |
| 246 | self.errors, |
| 247 | givens={ |
| 248 | self.x: test_set_x[ |