<jupyter_start><jupyter_text>----------Everything above this is for Google Coloab---------- Fashion-MNIST is a dataset of Zalando's article images—consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes.  It shares the same image size and structure of training and testing splits. -  Try to build a classifier for the Fashion-MNIST dataset that achieves over 93.5% accuracy on the test set. It is possible to get over 94%. I got 94.1%.-  Use convnet for the purpose. Focus on the following    -  Learn how to use padding in convnet.    -  Learn how to use Batch Normalization and Dropout to fight overfitting.    -  Learn how to use nadam optimizer.-  Part of function build_model() is given. Add more layers to achieve our goal. That is the only part we need to change.-  Use test data set only once.-  The grade is calculated as max(0, min((test accuracy - 0.92)/(0.935 - 0.92), 1))*100<jupyter_code>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
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
import mnist_reader
X_train, y_train = mnist_reader.load_mnist('/content/my_drive/Fashion/convnet/', kind='train')
X_test, y_test = mnist_reader.load_mnist('/content/my_drive/Fashion/convnet/', kind='t10k')
X_train.shape
y_train.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>LabelsEach training and test example is assigned to one of the following labels:Label	Description- 0	T-shirt/top- 1	Trouser- 2	Pullover- 3	Dress- 4	Coat- 5	Sandal- 6	Shirt- 7	Sneaker- 8	Bag- 9	Ankle boot<jupyter_code>def plot_digit(data):
    image = data.reshape(28, 28)
    plt.imshow(image, cmap = matplotlib.cm.binary, interpolation="nearest")
    plt.axis("off")
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

some_digit = X_train[36001]
some_digit_image = some_digit.reshape(28, 28)
plt.imshow(some_digit_image, cmap = matplotlib.cm.binary, interpolation="nearest")
plt.axis("off");
plot_digit(X_train[40000])
def plot_digits(instances, images_per_row=10, **options):
    size = 28
    images_per_row = min(len(instances), images_per_row)
    images = [instance.reshape(size,size) for instance in instances]
    n_rows = (len(instances) - 1) // images_per_row + 1
    row_images = []
    n_empty = n_rows * images_per_row - len(instances)
    images.append(np.zeros((size, size * n_empty)))
    for row in range(n_rows):
        rimages = images[row * images_per_row : (row + 1) * images_per_row]
        row_images.append(np.concatenate(rimages, axis=1))
    image = np.concatenate(row_images, axis=0)
    plt.imshow(image, cmap = matplotlib.cm.binary, **options)
    plt.axis("off")
plt.figure(figsize=(9,9))
X_0 = X_train[(y_train == 0)]
example_images = X_0[:100]
plot_digits(example_images, images_per_row=10)
plt.figure(figsize=(9,9))
X_1 = X_train[(y_train == 1)]
example_images = X_1[:100]
plot_digits(example_images, images_per_row=10)
plt.figure(figsize=(9,9))
X_2 = X_train[(y_train == 2)]
example_images = X_2[:100]
plot_digits(example_images, images_per_row=10)
from keras import layers
from keras import models

def build_model():
    model = models.Sequential()
    model.add(layers.Conv2D(32, (3, 3), padding="same", activation='relu', input_shape=(28, 28, 1)))
    model.add(layers.BatchNormalization())
    model.add(layers.Conv2D(32, (3, 3), padding="same", activation='relu'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Dropout(0.25))

    # add layers here 
    #model.add(layers.Conv2D(32, (3, 3), padding="same", activation='relu', input_shape=(28, 28, 1)))
    model.add(layers.Conv2D(64, (3, 3), padding="same", activation='relu'))
    model.add(layers.BatchNormalization())
    model.add(layers.Conv2D(64, (3, 3), padding="same", activation='relu'))
    model.add(layers.BatchNormalization())
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Dropout(0.25))
    
    model.add(layers.Flatten())

    # add layers here
    model.add(layers.Dense(128, activation='relu'))
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(10, activation='softmax'))
    return model
model = build_model()
model.summary()
from keras.utils import to_categorical

X_train = X_train.reshape((60000, 28, 28, 1))
X_train = X_train.astype('float32') / 255
y_train = to_categorical(y_train)

X_test = X_test.reshape((10000, 28, 28, 1))
X_test = X_test.astype('float32') / 255
y_test = to_categorical(y_test)

X_val = X_train[:10000]
y_val = y_train[:10000]

X_train_partial = X_train[10000:]
y_train_partial = y_train[10000:]
X_train_partial.shape, y_train_partial.shape, X_test.shape, y_test.shape
model.compile(optimizer='nadam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(X_train_partial, y_train_partial, epochs=40, batch_size=64, validation_data=(X_val, y_val))
history.history.keys()
%matplotlib inline
import matplotlib.pyplot as plt

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)

# "bo" is for "blue dot"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.clf()   # clear figure
acc_values = history.history['accuracy']
val_acc_values = history.history['val_accuracy']

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
model = build_model()
model.compile(optimizer='nadam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=40, batch_size=64)
model.evaluate(X_test, y_test)
acc = model.evaluate(X_test, y_test)
grade = max(0, min((acc[1] - 0.92)/(0.935 - 0.92), 1))*100
print(grade)
print("The grade is ", grade)<jupyter_output>The grade is  100
{'fraction_non_alphanumeric': 0.1384687353812568, 'fraction_numerical': 0.03882738188055512, 'mean_word_length': 4.884403669724771, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '450071', 'license': 'MIT', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2250, 'n_words': 553, 'path': 'Fashion MNIST/Convnet/Fashion_MNIST_convnet_starter_done.ipynb', 'repo_name': 'aj96oneill/ML-Beginner-Projects', 'size': '6413'}