<jupyter_start><jupyter_text><jupyter_code># Import tools
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt


# Load data and normalize to make images values have amaximun of 1
(trainData, trainLabels),(testData, testLabels) = mnist.load_data();
trainData = trainData.astype('float32')/255
testData = testData.astype('float32')/255

trainData = np.expand_dims(trainData, 3);
testData = np.expand_dims(testData, 3);

trainLabels = keras.utils.to_categorical(trainLabels,10);
testLabels = keras.utils.to_categorical(testLabels,10);

print('Train data size: ', trainData.shape)
print('Test data size: ', testData.shape)
# if you are rerunning this, clear the old model
keras.backend.clear_session()

# Set the network 3 conv2d layers with maxpooling over the size of the filter of the previous layer
# also batchnormalization is used before every layer, which imporves speed
inputs = keras.layers.Input(shape=(trainData.shape[1:]))
x = keras.layers.BatchNormalization()(inputs)
x = keras.layers.Conv2D(100,3, activation='relu', padding='valid',input_shape=inputs.shape[1:])(x)
x = keras.layers.MaxPooling2D((3,3),(1,1))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(50,3, activation='relu')(x)
x = keras.layers.MaxPooling2D((3,3),(1,1))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(25,3, activation='relu')(x)
x = keras.layers.MaxPooling2D((3,3),(1,1))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(100,activation='relu')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dense(50,activation='relu')(x)
x = keras.layers.BatchNormalization()(x)
outputs = keras.layers.Dense(10,activation='softmax')(x)

model = keras.Model(inputs=inputs,outputs=outputs)
model.summary()


# Compile, use Adadelta to reduce the effect of decreasing earning rate
model.compile(
    loss=keras.losses.CategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adadelta(learning_rate=1),
    metrics=["accuracy"],
)

# train the model and plot the loss history, consider to increase the epochs if the loss history sugest it
history = model.fit(trainData,trainLabels,batch_size=1000, epochs=30, validation_split=0.2)
plt.plot(history.history['loss'])


#Test the model
model.evaluate(testData,testLabels)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13756177924217464, 'fraction_numerical': 0.026771004942339374, 'mean_word_length': 6.886363636363637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '103370', 'license': 'MIT', 'n_tokens_mistral': 889, 'n_tokens_neox': 848, 'n_words': 183, 'path': 'deepConvNet.ipynb', 'repo_name': 'jfdelgad/convolutionalnetworks_mnist', 'size': '2428'}