<jupyter_start><jupyter_text>CNN Demo to distinguish between classes of apparels  Fashion MNIST dataset<jupyter_code>import tensorflow.keras
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras import regularizers
from tensorflow.keras.datasets import fashion_mnist

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
print("Shape of x_train: {}".format(x_train.shape))
print("Shape of y_train: {}".format(y_train.shape))
print()
print("Shape of x_test: {}".format(x_test.shape))
print("Shape of y_test: {}".format(y_test.shape))
#Display as text
from IPython.display import display
import pandas as pd

pd.set_option('display.max_columns', 10)
pd.set_option('display.max_rows', 5)

print("Shape for dataset: {}".format(x_train.shape))
print("Labels: {}".format(y_train))

# Single MNIST digit
single = x_train[0]
print("Shape for single: {}".format(single.shape))

pd.set_option('display.max_columns', 24)
pd.set_option('display.max_rows', 20)

pd.set_option('display.max_columns', 20)
pd.set_option('display.max_rows', 20)
pd.DataFrame(single.reshape(28,28))
# Display as image
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
digit = 90 # Change to choose new article
a = x_train[digit]
plt.imshow(a, cmap='gray', interpolation='nearest')
print("Image (#{}): Which is digit '{}'".format(digit,y_train[digit]))<jupyter_output>Image (#90): Which is digit '9'<jupyter_text>Check a few images<jupyter_code>import random

ROWS = 6
random_indices = random.sample(range(x_train.shape[0]), ROWS*ROWS)

sample_images = x_train[random_indices, :]

plt.clf()

fig, axes = plt.subplots(ROWS,ROWS, 
                         figsize=(ROWS,ROWS),
                         sharex=True, sharey=True) 

for i in range(ROWS*ROWS):
    subplot_row = i//ROWS 
    subplot_col = i%ROWS
    ax = axes[subplot_row, subplot_col]

    plottable_image = np.reshape(sample_images[i,:], (28,28))
    ax.imshow(plottable_image, cmap='gray_r')
    
    ax.set_xbound([0,28])

plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Training/Fitting CNN - Fashion<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D<jupyter_output><empty_output><jupyter_text>Reshape input dataWhen using the tensorflow backend, you must explicitly declare a dimension for the depth of the input image. E.g. a full-color image with all 3 RGB channels will have a depth of 3.Our MNIST images only have a depth of 1, but we must explicitly declare that. In other words, we want to transform our dataset from having shape (n, width, height) to (n, width, height, channel).<jupyter_code># input image dimensions
img_rows, img_cols = 28, 28
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32')/255  # this is just to normalize the data. 
x_test = x_test.astype('float32')/255
# convert class vectors to binary class matrices
num_classes=10
y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)
input_shape = (img_rows, img_cols, 1)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25)) # Dropout is a technique used to prevent a model from overfitting. 
# Dropout randomly sets the outgoing edges of hidden units to 0 at each update of the training phase.
model.add(Flatten()) # A flatten layer collapses the spatial dimensions of the input into the channel dimension.
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',
              metrics=['accuracy'])
import tensorflow as tf
import time

start_time = time.time()

model.fit(x_train, y_train,
          batch_size=128,
          epochs=5,
          verbose=2,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss: {}'.format(score[0]))
print('Test accuracy: {}'.format(score[1]))

elapsed_time = time.time() - start_time
print("Elapsed time: {}".format(elapsed_time))<jupyter_output>Epoch 1/5
469/469 - 67s - loss: 0.5249 - accuracy: 0.8148 - val_loss: 0.3402 - val_accuracy: 0.8838
Epoch 2/5
469/469 - 63s - loss: 0.3464 - accuracy: 0.8763 - val_loss: 0.2834 - val_accuracy: 0.8965
Epoch 3/5
469/469 - 66s - loss: 0.2935 - accuracy: 0.8939 - val_loss: 0.2600 - val_accuracy: 0.9052
Epoch 4/5
469/469 - 61s - loss: 0.2625 - accuracy: 0.9035 - val_loss: 0.2517 - val_accuracy: 0.9092
Epoch 5/5
469/469 - 64s - loss: 0.2365 - accuracy: 0.9126 - val_loss: 0.2315 - val_accuracy: 0.9169
Test loss: 0.23153315484523773
Test accuracy: 0.9168999791145325
Elapsed time: 324.5158619880676
{'fraction_non_alphanumeric': 0.1253416634127294, 'fraction_numerical': 0.05603279968762202, 'mean_word_length': 4.895281933256617, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '294340', 'license': 'MIT', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1809, 'n_words': 457, 'path': 'W7D7/CNN.ipynb', 'repo_name': 'sedv8808/sm_classes', 'size': '5122'}