Demonstrate stochastic gradient descent optimization for a multilayer perceptron This is demonstrated on MNIST. :type learning_rate: float :param learning_rate: learning rate used (factor for the stochastic gradient :type L1_reg: float :param L1_reg: L1-norm's wei
(learning_rate=0.01, L1_reg=0.00, L2_reg=0.0001, n_epochs=1000,
dataset='mnist.pkl.gz', batch_size=20, n_hidden=500)
| 199 | |
| 200 | |
| 201 | def test_mlp(learning_rate=0.01, L1_reg=0.00, L2_reg=0.0001, n_epochs=1000, |
| 202 | dataset='mnist.pkl.gz', batch_size=20, n_hidden=500): |
| 203 | """ |
| 204 | Demonstrate stochastic gradient descent optimization for a multilayer |
| 205 | perceptron |
| 206 | |
| 207 | This is demonstrated on MNIST. |
| 208 | |
| 209 | :type learning_rate: float |
| 210 | :param learning_rate: learning rate used (factor for the stochastic |
| 211 | gradient |
| 212 | |
| 213 | :type L1_reg: float |
| 214 | :param L1_reg: L1-norm's weight when added to the cost (see |
| 215 | regularization) |
| 216 | |
| 217 | :type L2_reg: float |
| 218 | :param L2_reg: L2-norm's weight when added to the cost (see |
| 219 | regularization) |
| 220 | |
| 221 | :type n_epochs: int |
| 222 | :param n_epochs: maximal number of epochs to run the optimizer |
| 223 | |
| 224 | :type dataset: string |
| 225 | :param dataset: the path of the MNIST dataset file from |
| 226 | http://www.iro.umontreal.ca/~lisa/deep/data/mnist/mnist.pkl.gz |
| 227 | |
| 228 | |
| 229 | """ |
| 230 | datasets = load_data(dataset) |
| 231 | |
| 232 | train_set_x, train_set_y = datasets[0] |
| 233 | valid_set_x, valid_set_y = datasets[1] |
| 234 | test_set_x, test_set_y = datasets[2] |
| 235 | |
| 236 | # compute number of minibatches for training, validation and testing |
| 237 | n_train_batches = train_set_x.get_value(borrow=True).shape[0] // batch_size |
| 238 | n_valid_batches = valid_set_x.get_value(borrow=True).shape[0] // batch_size |
| 239 | n_test_batches = test_set_x.get_value(borrow=True).shape[0] // batch_size |
| 240 | |
| 241 | ###################### |
| 242 | # BUILD ACTUAL MODEL # |
| 243 | ###################### |
| 244 | print('... building the model') |
| 245 | |
| 246 | # allocate symbolic variables for the data |
| 247 | index = T.lscalar() # index to a [mini]batch |
| 248 | x = T.matrix('x') # the data is presented as rasterized images |
| 249 | y = T.ivector('y') # the labels are presented as 1D vector of |
| 250 | # [int] labels |
| 251 | |
| 252 | rng = numpy.random.RandomState(1234) |
| 253 | |
| 254 | # construct the MLP class |
| 255 | classifier = MLP( |
| 256 | rng=rng, |
| 257 | input=x, |
| 258 | n_in=28 * 28, |
no test coverage detected