<jupyter_start><jupyter_text>Setup First, let's import a few common modules, ensure MatplotLib plots figures inline and prepare a function to save the figures. We also check that Python 3.5 or later is installed (although Python 2.x may work, it is deprecated so we strongly recommend you use Python 3 instead), as well as Scikit-Learn ≥0.20 and TensorFlow ≥2.0.<jupyter_code># Python ≥3.5 is required
import sys
assert sys.version_info >= (3, 5)

# Scikit-Learn ≥0.20 is required
import sklearn
assert sklearn.__version__ >= "0.20"

try:
    # %tensorflow_version only exists in Colab.
    %tensorflow_version 2.x
except Exception:
    pass

# TensorFlow ≥2.0 is required
import tensorflow as tf
assert tf.__version__ >= "2.0"

# Common imports
import numpy as np
import os

# to make this notebook's output stable across runs
np.random.seed(42)

# To plot pretty figures
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)

# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "ann"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)

def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
    path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
    print("Saving figure", fig_id)
    if tight_layout:
        plt.tight_layout()
    plt.savefig(path, format=fig_extension, dpi=resolution)<jupyter_output><empty_output><jupyter_text>Building an Image Classifier First let's import TensorFlow and Keras.<jupyter_code>import tensorflow as tf
from tensorflow import keras
tf.__version__
keras.__version__<jupyter_output><empty_output><jupyter_text>Let's start by loading the fashion MNIST dataset. Keras has a number of functions to load popular datasets in `keras.datasets`. The dataset is already split for you between a training set and a test set, but it can be useful to split the training set further to have a validation set:<jupyter_code>fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()<jupyter_output><empty_output><jupyter_text>The training set contains 60,000 grayscale images, each 28x28 pixels:<jupyter_code>X_train_full.shape<jupyter_output><empty_output><jupyter_text>Each pixel intensity is represented as a byte (0 to 255):<jupyter_code>X_train_full.dtype<jupyter_output><empty_output><jupyter_text>Let's split the full training set into a validation set and a (smaller) training set. We also scale the pixel intensities down to the 0-1 range and convert them to floats, by dividing by 255.<jupyter_code>X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.<jupyter_output><empty_output><jupyter_text>You can plot an image using Matplotlib's `imshow()` function, with a `'binary'` color map:<jupyter_code>plt.imshow(X_train[0], cmap="binary")
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>The classes are:Label | Description----- | -------0 | T-shirt/top1 | Trouser2 | Pullover3 | Dress4 | Coat5 | Sandal6 | Shirt7 | Sneaker8 | Bag9 | Ankle bootThe labels are the class IDs (represented as uint8), from 0 to 9:<jupyter_code>y_train<jupyter_output><empty_output><jupyter_text>Here are the corresponding class names:<jupyter_code>class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
               "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]<jupyter_output><empty_output><jupyter_text>So the first image in the training set is a coat:<jupyter_code>class_names[y_train[0]]<jupyter_output><empty_output><jupyter_text>The validation set contains 5,000 images, and the test set contains 10,000 images:<jupyter_code>X_valid.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Let's take a look at a sample of the images in the dataset:<jupyter_code>n_rows = 4
n_cols = 10
plt.figure(figsize=(n_cols * 1.2, n_rows * 1.2))
for row in range(n_rows):
    for col in range(n_cols):
        index = n_cols * row + col
        plt.subplot(n_rows, n_cols, index + 1)
        plt.imshow(X_train[index], cmap="binary", interpolation="nearest")
        plt.axis('off')
        plt.title(class_names[y_train[index]], fontsize=12)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
save_fig('fashion_mnist_plot', tight_layout=False)
plt.show()
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))
keras.backend.clear_session()
np.random.seed(42)
tf.random.set_seed(42)
model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[28, 28]),
    keras.layers.Dense(300, activation="relu"),
    keras.layers.Dense(100, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])
model.layers
model.summary()
keras.utils.plot_model(model, "my_fashion_mnist_model.png", show_shapes=True)
hidden1 = model.layers[1]
hidden1.name
model.get_layer(hidden1.name) is hidden1
weights, biases = hidden1.get_weights()
weights
weights.shape
biases
biases.shape
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="sgd",
              metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>This is equivalent to: ```pythonmodel.compile(loss=keras.losses.sparse_categorical_crossentropy,              optimizer=keras.optimizers.SGD(),              metrics=[keras.metrics.sparse_categorical_accuracy])```<jupyter_code>history = model.fit(X_train, y_train, epochs=30,
                    validation_data=(X_valid, y_valid))
history.params
print(history.epoch)
history.history.keys()
import pandas as pd

pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
save_fig("keras_learning_curves_plot")
plt.show()
model.evaluate(X_test, y_test)
X_new = X_test[:3]
y_proba = model.predict(X_new)
y_proba.round(2)<jupyter_output><empty_output><jupyter_text>**Warning**: `model.predict_classes(X_new)` is deprecated. It is replaced with `np.argmax(model.predict(X_new), axis=-1)`.<jupyter_code>#y_pred = model.predict_classes(X_new) # deprecated
y_pred = np.argmax(model.predict(X_new), axis=-1)
y_pred
np.array(class_names)[y_pred]
y_new = y_test[:3]
y_new
plt.figure(figsize=(7.2, 2.4))
for index, image in enumerate(X_new):
    plt.subplot(1, 3, index + 1)
    plt.imshow(image, cmap="binary", interpolation="nearest")
    plt.axis('off')
    plt.title(class_names[y_test[index]], fontsize=12)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
save_fig('fashion_mnist_images_plot', tight_layout=False)
plt.show()<jupyter_output>Saving figure fashion_mnist_images_plot
{'fraction_non_alphanumeric': 0.12310606060606061, 'fraction_numerical': 0.025349650349650348, 'mean_word_length': 5.906438631790745, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 59, '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': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '569197', 'license': 'Apache-2.0', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2414, 'n_words': 552, 'path': 'eigene_tests/classification_with_keras.ipynb', 'repo_name': 'dbe-gmbh/handson-ml2', 'size': '6864'}