<jupyter_start><jupyter_text>TensorFlow Keras example on MNIST References* [Simple MNIST convnet](https://keras.io/examples/vision/mnist_convnet/)  Setup<jupyter_code>import numpy as np
from tensorflow import keras<jupyter_output><empty_output><jupyter_text>Prepare the data  Model / data parameters<jupyter_code>num_classes = 10
input_shape = (28, 28, 1)<jupyter_output><empty_output><jupyter_text>the data, split between train and test sets<jupyter_code>(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()<jupyter_output><empty_output><jupyter_text>Scale images to the [0, 1] range<jupyter_code>x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255<jupyter_output><empty_output><jupyter_text>Make sure images have shape (28, 28, 1)<jupyter_code>x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print("x_train shape:", x_train.shape)
print(x_train.shape[0], "train samples")
print(x_test.shape[0], "test samples")<jupyter_output><empty_output><jupyter_text>convert class vectors to binary class matrices<jupyter_code>y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)<jupyter_output><empty_output><jupyter_text>Build the model<jupyter_code>layers = [
    keras.Input(shape=input_shape),
    keras.layers.Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), padding='valid', data_format=None,
        dilation_rate=(1, 1), groups=1, activation='relu'),
    keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None),
    keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    keras.layers.MaxPooling2D(pool_size=(2, 2)),
    keras.layers.Flatten(data_format=None),
    keras.layers.Dropout(rate=0.5, noise_shape=None, seed=None),
    keras.layers.Dense(units=num_classes, activation='softmax'),
]
model = keras.Sequential(layers)

model.summary()<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>batch_size = 128
epochs = 15
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'],
    loss_weights=None, weighted_metrics=None, run_eagerly=None, steps_per_execution=None)
model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs, verbose=1, callbacks=None, validation_split=0.1,
    validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None,
    validation_steps=None, validation_batch_size=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)<jupyter_output><empty_output><jupyter_text>Evaluate the trained model<jupyter_code>score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13574982231698648, 'fraction_numerical': 0.023809523809523808, 'mean_word_length': 9.053571428571429, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, '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': '180028', 'license': 'CC0-1.0', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1037, 'n_words': 158, 'path': '2. Comparison/Keras/KerasExampleOnMNIST.ipynb', 'repo_name': 'k14i/LearningPyTorchLightning', 'size': '2814'}