Draft: 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:
2. The Challenge
Fashion MNIST.
import keras, tensorflow as tf, numpy as np, matplotlib.pyplot as plt 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() 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
Define
model = keras.Sequential([ keras.layers.Flatten(input_shape=(28, 28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ])
Compile
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Now, we train our model:
model.fit(train_images, train_labels, epochs=5)
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)
Not bad! But can you do better?
5. Visualization
predictions = model.predict(test_images)
def plot_image(i, predictions_array, true_label, img): predictions_array = predictions_array[i] true_label = true_label[i] img = img[i] plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(img, cmap=plt.cm.binary) predicted_label = np.argmax(predictions_array) if predicted_label == true_label: color = 'blue' else: color = 'red' plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label], 100*np.max(predictions_array), class_names[true_label]), color=color)
def plot_value_array(i, predictions_array, true_label): predictions_array, true_label = predictions_array[i], true_label[i] plt.grid(False) plt.yticks([]) plt.xticks([]) plot = plt.bar(range(10), predictions_array, color="#777777", align="center") plt.ylim([0, 1]) predicted_label = np.argmax(predictions_array) plot[predicted_label].set_color('red') plot[true_label].set_color('blue')
i = 0 f1 = plt.figure(figsize=(10,5)) plt.subplot(1,2,1) plot_image(i, predictions, test_labels, test_images) plt.subplot(1,2,2) plot_value_array(i, predictions, test_labels) f1
num_rows = 5 num_cols = 3 num_images = num_rows*num_cols fig = plt.figure(figsize=(2*2*num_cols, 2*num_rows)) for i in range(num_images): plt.subplot(num_rows, 2*num_cols, 2*i+1) plot_image(i, predictions, test_labels, test_images) plt.subplot(num_rows, 2*num_cols, 2*i+2) plot_value_array(i, predictions, test_labels) fig
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!