<jupyter_start><jupyter_text>MNIST digit classifier example  Imports<jupyter_code>%matplotlib inline

import numpy as np

from matplotlib import pyplot as plt

try:
    from tqdm import tqdm_notebook
except ImportError:
    tqdm_notebook = None

import sklearn.cross_validation

import lasagne
import theano.tensor as T
from britefury_lasagne import basic_dnn, trainer, image_window_extractor, mnist<jupyter_output><empty_output><jupyter_text>Define network architectureWe define the `build_network` function that takes the input variables as an optional argument and build the network using the Lasagne API.NOTE that the final dense layer does *NOT* use the `softmax` nonlinearity as it is supplied by the classifier builder (see below).<jupyter_code>def build_network(input_vars=None):
    # Input layer
    x_var = input_vars[0] if input_vars is not None else None
    net = lasagne.layers.InputLayer(shape=(None, 1, 28, 28),
                                        input_var=x_var)

    # A 32 unit 5x5 conv layer, followed by 2x2 max-pool
    net = lasagne.layers.Conv2DLayer(net, num_filters=32, filter_size=(5, 5), W=lasagne.init.HeUniform())
    net = lasagne.layers.MaxPool2DLayer(net, pool_size=(2, 2))

    # Two 32 unit 3x3 conv layers, followed by 2x2 max-pool
    net = lasagne.layers.Conv2DLayer(net, num_filters=32, filter_size=(3, 3), W=lasagne.init.HeUniform())
    net = lasagne.layers.Conv2DLayer(net, num_filters=32, filter_size=(3, 3), W=lasagne.init.HeUniform())
    net = lasagne.layers.MaxPool2DLayer(net, pool_size=(2, 2))

    # A fully-connected layer of 64 units followed by 50% dropout
    net = lasagne.layers.DenseLayer(net, num_units=64, W=lasagne.init.HeUniform())
    net = lasagne.layers.DropoutLayer(net, p=0.5)

    # Final 10-unit dense layer, with no nonlinearity
    net = lasagne.layers.DenseLayer(net, num_units=10, nonlinearity=None)

    return net<jupyter_output><empty_output><jupyter_text>Load the train, validation and test sets<jupyter_code>dataset = mnist.MNIST()<jupyter_output><empty_output><jupyter_text>Train the classifier<jupyter_code># Build the image classifier for the given model builder
print('Building network')
clf = basic_dnn.simple_classifier(build_network, n_input_spatial_dims=2,
            updates_fn=lambda loss, params: lasagne.updates.adam(loss, params, learning_rate=0.001))

# Train with a batch size of 128, for 10 epochs, reporting after each epoch.
print('Training')
clf.train([dataset.train_X[:2048], dataset.train_y[:2048]],
          [dataset.val_X[:2048], dataset.val_y[:2048]],
          [dataset.test_X[:2048], dataset.test_y[:2048]], batchsize=128,
          num_epochs=10, verbosity=trainer.VERBOSITY_EPOCH,
          progress_iter_func=tqdm_notebook)<jupyter_output><empty_output><jupyter_text>Use the classifier to predict on the test set<jupyter_code># Predict probabilities for test samples
test_y_pred_prob = clf.predict([dataset.test_X])[0]
# Use `np.argmax` to get class predictions
test_y_pred = np.argmax(test_y_pred_prob, axis=1)

# Show the error rate
print('Test error rate={:.2%}'.format(np.mean(test_y_pred != dataset.test_y)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10167880899588216, 'fraction_numerical': 0.029458346531517264, 'mean_word_length': 5.049808429118774, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '653345', 'license': 'MIT', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1052, 'n_words': 264, 'path': 'examples/MNIST digit classifier.ipynb', 'repo_name': 'Britefury/britefury_lasagne_helpers', 'size': '3157'}