<jupyter_start><jupyter_text>In this notebook, we duplicate the neural network created in the TensorFlow example in [keras](http://keras.io/).  I found the model in keras to be conceptually cleaner.<jupyter_code>from __future__ import print_function
from __future__ import division
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils<jupyter_output>Using Theano backend.<jupyter_text>Set up the parameters for the model.  Nothing too exciting here.<jupyter_code>batch_size = 500
nb_classes = 10
nb_epoch = 1

# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = 32
nb2_filters = 64
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 5

# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

X_train /= 255 # normalize the data
X_test /= 255 # normalize the data

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)<jupyter_output><empty_output><jupyter_text>Below we build the neural network.  This is the same network used in the TensorFlow example.<jupyter_code>model = Sequential()

model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
                        border_mode='valid',
                        input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(nb2_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adadelta')<jupyter_output><empty_output><jupyter_text>Now we just run the neural network.  Note that one epoch here is a run through the entire set of training data, so it takes a while.<jupyter_code>model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
          show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/1
60000/60000 [==============================] - 7112s - loss: 0.3877 - acc: 0.8828 - val_loss: 0.0677 - val_acc: 0.9788
Test score: 0.067661594807
Test accuracy: 0.9788
{'fraction_non_alphanumeric': 0.11286001339584728, 'fraction_numerical': 0.0391828533154722, 'mean_word_length': 5.804100227790433, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '338660', 'license': 'MIT', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1027, 'n_words': 271, 'path': 'neural_networks/MNIST_keras.ipynb', 'repo_name': 'impavl/demo-ml', 'size': '2986'}