Demonstrate conjugate gradient optimization of a log-linear model This is demonstrated on MNIST. :type n_epochs: int :param n_epochs: number of epochs to run the optimizer :type mnist_pkl_gz: string :param mnist_pkl_gz: the path of the mnist training file from
(n_epochs=50, mnist_pkl_gz='mnist.pkl.gz')
| 143 | |
| 144 | |
| 145 | def cg_optimization_mnist(n_epochs=50, mnist_pkl_gz='mnist.pkl.gz'): |
| 146 | """Demonstrate conjugate gradient optimization of a log-linear model |
| 147 | |
| 148 | This is demonstrated on MNIST. |
| 149 | |
| 150 | :type n_epochs: int |
| 151 | :param n_epochs: number of epochs to run the optimizer |
| 152 | |
| 153 | :type mnist_pkl_gz: string |
| 154 | :param mnist_pkl_gz: the path of the mnist training file from |
| 155 | http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz |
| 156 | |
| 157 | """ |
| 158 | ############# |
| 159 | # LOAD DATA # |
| 160 | ############# |
| 161 | datasets = load_data(mnist_pkl_gz) |
| 162 | |
| 163 | train_set_x, train_set_y = datasets[0] |
| 164 | valid_set_x, valid_set_y = datasets[1] |
| 165 | test_set_x, test_set_y = datasets[2] |
| 166 | |
| 167 | batch_size = 600 # size of the minibatch |
| 168 | |
| 169 | n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size |
| 170 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] // batch_size |
| 171 | n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size |
| 172 | |
| 173 | n_in = 28 * 28 # number of input units |
| 174 | n_out = 10 # number of output units |
| 175 | |
| 176 | ###################### |
| 177 | # BUILD ACTUAL MODEL # |
| 178 | ###################### |
| 179 | print('... building the model') |
| 180 | |
| 181 | # allocate symbolic variables for the data |
| 182 | minibatch_offset = T.lscalar() # offset to the start of a [mini]batch |
| 183 | x = T.matrix() # the data is presented as rasterized images |
| 184 | y = T.ivector() # the labels are presented as 1D vector of |
| 185 | # [int] labels |
| 186 | |
| 187 | # construct the logistic regression class |
| 188 | classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10) |
| 189 | |
| 190 | # the cost we minimize during training is the negative log likelihood of |
| 191 | # the model in symbolic format |
| 192 | cost = classifier.negative_log_likelihood(y).mean() |
| 193 | |
| 194 | # compile a theano function that computes the mistakes that are made by |
| 195 | # the model on a minibatch |
| 196 | test_model = theano.function( |
| 197 | [minibatch_offset], |
| 198 | classifier.errors(y), |
| 199 | givens={ |
| 200 | x: test_set_x[minibatch_offset:minibatch_offset + batch_size], |
| 201 | y: test_set_y[minibatch_offset:minibatch_offset + batch_size] |
| 202 | }, |
no test coverage detected