<jupyter_start><jupyter_text>Set things up: General Python and Keras imports:<jupyter_code># Larger CNN for the MNIST Dataset
import numpy
import random
import matplotlib.pyplot as plt 
import git
from keras.datasets import mnist
from keras.utils import np_utils
from keras import backend as K
from keras.callbacks import TensorBoard<jupyter_output>Using TensorFlow backend.<jupyter_text>Import the neural net from the local file, as generated by GiraffeTools:<jupyter_code>from GIRAFFE.code.neural_net import NeuralNet<jupyter_output><empty_output><jupyter_text>Some constants and initialisations<jupyter_code>K.set_image_dim_ordering('th')
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)<jupyter_output><empty_output><jupyter_text>Load data: Load data from MNIST standard data set:<jupyter_code># load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
# normalize inputs from 0-255 to 0-1
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]<jupyter_output><empty_output><jupyter_text>This is what the data looks like:<jupyter_code># plot 4 images as gray scale
dimensions = list(X_train[0].shape)[1:]
plt.subplot(141)
plt.imshow(X_train[0].reshape(dimensions), cmap=plt.get_cmap('gray'))
plt.subplot(142)
plt.imshow(X_train[1].reshape(dimensions), cmap=plt.get_cmap('gray'))
plt.subplot(143)
plt.imshow(X_train[2].reshape(dimensions), cmap=plt.get_cmap('gray'))
plt.subplot(144)
plt.imshow(X_train[3].reshape(dimensions), cmap=plt.get_cmap('gray'))
# show the plot
plt.show()<jupyter_output><empty_output><jupyter_text>The neural network bit: Grab the model (imported as 'NeuralNet') and initialise it with the shape of our data<jupyter_code># build the model
model = NeuralNet(y_test.shape[1])<jupyter_output><empty_output><jupyter_text>Compile and run the model<jupyter_code># Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Save logs in this folder under the current git hash
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
logdir="logs/" + sha
# Fit the model
fit = model.fit(
        X_train, 
        y_train, 
        validation_data=(X_test, y_test), 
        epochs=10, 
        batch_size=200,
        callbacks=[TensorBoard(log_dir=logdir)]
 )<jupyter_output>WARNING:tensorflow:From C:\Users\timvmou\AppData\Local\Continuum\anaconda3\envs\keras\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From C:\Users\timvmou\AppData\Local\Continuum\anaconda3\envs\keras\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From C:\Users\timvmou\AppData\Local\Continuum\anaconda3\envs\keras\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.ca[...]<jupyter_text>Hohum, see here the scores of the network:<jupyter_code># Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Large CNN Error: %.2f%%" % (100-scores[1]*100))<jupyter_output>Large CNN Error: 0.85%<jupyter_text>Proof it's working:<jupyter_code># plot random image
random_number = random.randrange(0, X_test.shape[0])
dimensions = list(X_test[random_number].shape)[1:]
plt.imshow(X_test[random_number].reshape(dimensions), cmap=plt.get_cmap('gray'))
# show the plot
plt.show()
prediction = model.predict(X_test[random_number].reshape(1,1,28,28))
result = plt.bar(list(range(10)), prediction[0])
result.set_label(list(range(10)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11891018240591088, 'fraction_numerical': 0.022627568690833526, 'mean_word_length': 6.819494584837545, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '252459', 'license': 'MIT', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1527, 'n_words': 335, 'path': 'digit_recognition.ipynb', 'repo_name': 'TimVanMourik/keras-giraffetools-digit-recognition', 'size': '4331'}