Keras Fashion-MNIST

This notebook runs on a Python environment that contains Keras, Tensorflow and Numpy. It describes a small machine learning challenge with a suggested solution to the challenge.

Your mission is to come with a better solution to the challenge and to share it with your colleagues, customers or friends either privately or publicly.

Currently, the notebook is in read-only mode. In order to change the content of a cell, all you need to do is to click on the Remix button on the right of the top bar. Once you are satisfied with your solution, share it by clicking on the Publish button that will appears once you remix the notebook.

1. Setup

There is almost nothing to setup as the "Keras Installation" environment already contains the required python packages.

The only thing we need to do is to import the required dependencies in a Python code cell:

import keras, tensorflow as tf, numpy as np, matplotlib.pyplot as plt

2. The Challenge

The challenge is to build a model for Fashion MNIST, 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. Fashion-MNIST dataset is accessible from keras.datasets.

from keras.datasets import fashion_mnist

np.random.seed(1)
tf.set_random_seed(1)

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

type(train_images)
<class 'numpy.ndarray'>

Let's visualize some items.

train_images = train_images / 255.0
test_images = test_images / 255.0

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

fig = plt.figure(figsize=(10,10))
for i in range(25):
  plt.subplot(5,5,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  plt.imshow(train_images[i], cmap=plt.cm.binary)
  plt.xlabel(class_names[train_labels[i]])
fig

3. The Model

We start with a very simple neural network and compile the model, using the Adam optimizer and sparse categorical cross entropy:

model = keras.Sequential([
  keras.layers.Flatten(input_shape=(28, 28)),
  keras.layers.Dense(3, activation=tf.nn.relu), 
  keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Let's train our model with 5 epochs:

model.fit(train_images, train_labels, epochs=4)
<keras.callba...x7f83e7e949e8>

4. Model Evaluation

Let's check how our model performs:

test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

Accuracy of 0.7231 on the test set is a good start for our naive model!

Can you come up with a model of your own that performs better?

You could try using convolutional layers instead of the dense layers or simply add more layers or more neurons into the existing layers.

All you need to do is to click on the "Remix" button on the right of the top bar. Now the notebook becomes your own and you can author it. Once you are satisfied with your model, share it with privately with your colleagues or publicly. It is a simple as clicking on the "Publish" button.

Happy notebooking!