<jupyter_start><jupyter_text>Fashion-Mnist Dataset with Tensorflow<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 
import tensorflow as tf 
from tensorflow.keras.datasets import fashion_mnist
from tensorflow_datasets import load
(train_data, train_labels), (test_data, test_labels) = fashion_mnist.load_data()
# ds = load('fashion_mnist',data_dir=r'Documents\tensorflow-datasets', split='train', shuffle_files=True)
# # assert isinstance(ds, tf.data.Dataset)
# print(ds)
train_data.shape, train_labels.shape , test_data.shape
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# How many classes are there (this'll be our output shape)?
len(class_names)
plt.imshow(train_data[0])
plt.title(class_names[train_labels[0]])
# Plot an example image and its label
plt.imshow(train_data[17], cmap=plt.cm.binary) # change the colours to black & white
plt.title(class_names[train_labels[17]]);

# Plot multiple random images of fashion MNIST
import random
plt.figure(figsize=(7, 7))
for i in range(4):
  ax = plt.subplot(2, 2, i + 1)
  rand_index = random.choice(range(len(train_data)))
  plt.imshow(train_data[rand_index], cmap=plt.cm.binary)
  plt.title(class_names[train_labels[rand_index]])
  plt.axis(False)

# Set random seed
tf.random.set_seed(42)

# Create the model
model_1 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784, the Flatten layer does this for us)
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_1.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), # different loss function for multiclass classifcation
                 optimizer=tf.keras.optimizers.Adam(),
                 metrics=["accuracy"])

# Fit the model
non_norm_history = model_1.fit(train_data,
                                train_labels,
                                epochs=50,
                                validation_data=(test_data, test_labels)) # see how the model performs on the test set during training
pd.DataFrame(non_norm_history.history).plot()
# Normalizing
# Check the min and max values of the training data
train_data.min(), train_data.max()
# Divide train and test images by the maximum value (normalize it)
train_data = train_data / 255.0
test_data = test_data / 255.0

# Check the min and max values of the training data
train_data.min(), train_data.max()
# Set random seed
tf.random.set_seed(42)

# Create the model
model_12 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784)
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_12.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                 optimizer=tf.keras.optimizers.Adam(),
                 metrics=["accuracy"])

earlyStopping= tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=20,mode="min")

# Fit the model (to the normalized data)
norm_history = model_12.fit(train_data,
                            train_labels,
                            epochs=100,
                            validation_data=(test_data, test_labels),
                            callbacks=[earlyStopping])
pd.DataFrame(norm_history.history).plot()
# finding optimal learning rate 

# Set random seed
tf.random.set_seed(42)

# Create the model
model_13 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784)
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_13.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                 optimizer=tf.keras.optimizers.Adam(),
                 metrics=["accuracy"])

# Create the learning rate callback
lr_scheduler = tf.keras.callbacks.LearningRateScheduler(lambda epoch: 1e-3 * 10**(epoch/20))

# Fit the model
find_lr_history = model_13.fit(train_data,
                               train_labels,
                               epochs=50, # model already doing pretty good with current LR, probably don't need 100 epochs
                               validation_data=(test_data, test_labels),
                               callbacks=[lr_scheduler])


# Plot the learning rate decay curve

lrs = 1e-3 * (10**(np.arange(50)/20))
plt.semilogx(lrs, find_lr_history.history["loss"]) # want the x-axis to be log-scale
plt.xlabel("Learning rate")
plt.ylabel("Loss")
plt.title("Finding the ideal learning rate");

# 0.001 seems to be a good fit

# Set random seed
tf.random.set_seed(42)

# Create the model
model_14 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784)
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_14.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                 optimizer=tf.keras.optimizers.Adam(lr=0.001), # ideal learning rate (same as default)
                 metrics=["accuracy"])

earlyStopping= tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=20,mode="min")

# Fit the model
history = model_14.fit(train_data,
                       train_labels,
                       epochs=100,
                      callbacks=[earlyStopping],
                       validation_data=(test_data, test_labels))
pd.DataFrame(history.history).plot()
# Set random seed
tf.random.set_seed(42)

# Create the model
model_15 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784)
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(8, activation="relu"),
  tf.keras.layers.Dense(16, activation="relu"),
  tf.keras.layers.Dense(4, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_15.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                 optimizer=tf.keras.optimizers.Adam(lr=0.001), # ideal learning rate (same as default)
                 metrics=["accuracy"])

earlyStopping= tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=20,mode="min")

# Fit the model
history = model_15.fit(train_data,
                       train_labels,
                       epochs=300,
                      callbacks=[earlyStopping],
                       validation_data=(test_data, test_labels))
pd.DataFrame(history.history).plot()
# Set random seed
tf.random.set_seed(42)

# Create the model
model_16 = tf.keras.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)), # input layer (we had to reshape 28x28 to 784)
  tf.keras.layers.Dense(16, activation="relu"),
  tf.keras.layers.Dropout(0.15),
  tf.keras.layers.Dense(32, activation="relu"),
  tf.keras.layers.Dropout(0.15),
  tf.keras.layers.Dense(16, activation="relu"),
  tf.keras.layers.Dense(10, activation="softmax") # output shape is 10, activation is softmax
])

# Compile the model
model_16.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                 optimizer=tf.keras.optimizers.Adam(lr=0.001), # ideal learning rate (same as default)
                 metrics=["accuracy"])

earlyStopping= tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=20,mode="min")

# Fit the model
history = model_16.fit(train_data,
                       train_labels,
                       epochs=300,
                      callbacks=[earlyStopping],
                       validation_data=(test_data, test_labels))
pd.DataFrame(history.history).plot()
preds= np.argmax(model_16.predict(test_data), axis=1)
preds
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score

plt.figure(figsize=(15,10))
sns.heatmap(confusion_matrix(test_labels, preds), annot=True, fmt="d")
accuracy_score(test_labels, preds)
model_16.summary()
tf.keras.utils.plot_model(model_16, show_layer_names=True, show_shapes=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11933500341607835, 'fraction_numerical': 0.028239580961056707, 'mean_word_length': 3.247098646034816, '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': '431926', 'license': 'Apache-2.0', 'n_tokens_mistral': 3123, 'n_tokens_neox': 2837, 'n_words': 600, 'path': 'Classification/04__classification_FashionMnist_Tensorflow.ipynb', 'repo_name': 'teddcp2/Tensorflow-Deep-Learning-notes', 'size': '8782'}