<jupyter_start><jupyter_text>CNN on (Fashion) MNIST using TF 2.0<jupyter_code># Standard Python libraries
from __future__ import absolute_import, division, print_function, unicode_literals

import os
import time
import numpy as np
import glob
import matplotlib.pyplot as plt
import PIL

try:
    import imageio
except ModuleNotFoundError:
    %pip install -qq imageio
    import imageio
from IPython import display
import sklearn
from time import time

try:
    # %tensorflow_version only exists in Colab.
    %tensorflow_version 2.x
    IS_COLAB = True
except Exception:
    IS_COLAB = False

# TensorFlow ≥2.0 is required
try:
    import tensorflow as tf
except ModuleNotFoundError:
    %pip install -qq tensorflow
    import tensorflow as tf
    
from tensorflow import keras
assert tf.__version__ >= "2.0"

if not tf.config.list_physical_devices('GPU'):
    print("No GPU was detected. DNNs can be very slow without a GPU.")
    if IS_COLAB:
        print("Go to Runtime > Change runtime and select a GPU hardware accelerator.")
!git clone https://github.com/probml/pyprobml
try:
    import pyprobml.scripts.mnist_helpers_tf as helper
except ModuleNotFoundError:
    %pip install -qq pyprobml
    import pyprobml.scripts.mnist_helpers_tf as helper
train_images, train_labels, test_images, test_labels, class_names = helper.get_dataset(FASHION=False)
print(train_images.shape)
helper.plot_dataset(train_images, train_labels, class_names)
try:
    from tensorflow.keras import datasets, layers, models
except ModuleNotFoundError:
    %pip install -qq tensorflow
    from tensorflow.keras import datasets, layers, models

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(10, activation="softmax"))

model.summary()
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

for epoch in range(2):
    print(f"epoch {epoch}")
    time_start = time()
    model.fit(train_images, train_labels, epochs=1)
    print("time spent training {:0.3f}".format(time() - time_start))

    train_loss, train_acc = model.evaluate(train_images, train_labels)
    print("Train accuracy:", train_acc)
    test_loss, test_acc = model.evaluate(test_images, test_labels)
    print("Test accuracy:", test_acc)

    predictions = model.predict(test_images)
    if epoch == 0:
        ndx = helper.find_interesting_test_images(predictions, test_labels)
    helper.plot_interesting_test_results(test_images, test_labels, predictions, class_names, ndx)
    plt.suptitle(f"epoch {epoch}")<jupyter_output>epoch 0
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0469 - accuracy: 0.9852
time spent training 4.735
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0288 - accuracy: 0.9912
Train accuracy: 0.9912333488464355
313/313 [==============================] - 1s 2ms/step - loss: 0.0340 - accuracy: 0.9886
Test accuracy: 0.9886000156402588
(114,)
{'fraction_non_alphanumeric': 0.13428827215756492, 'fraction_numerical': 0.048343777976723366, 'mean_word_length': 5.072463768115942, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '437972', 'license': 'MIT', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1089, 'n_words': 287, 'path': 'notebooks/book1/14/cnn_mnist_tf.ipynb', 'repo_name': 'patel-zeel/pyprobml', 'size': '3351'}