In [1]:
from __future__ import absolute_import, division, print_function

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)


2.0.0-alpha0

In [2]:
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()


Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 6us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 47s 2us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 10s 2us/step

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

In [16]:
#explore the data
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()



In [17]:
train_images = train_images / 255.0
test_images = test_images / 255.0

In [18]:
plt.figure(figsize=(10,10))
for i in range(20):
    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]])
plt.show()



In [34]:
class MyModel(tf.keras.Model):
    def __init__(self):
        super(MyModel,self).__init__()
        pass
    


def build_model():
    
    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(28,28)),
        keras.layers.Dense(128,activation = 'relu'),
        keras.layers.Dense(10,activation = 'softmax')
    ])
    optimizer = keras.optimizers.Adam
    model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
    
    return model

In [25]:
model = build_model()

In [29]:
history = model.fit(train_images,train_labels,epochs=10, validation_data=[test_images,test_labels])


Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 4s 59us/sample - loss: 0.1396 - accuracy: 0.9481 - val_loss: 0.4028 - val_accuracy: 0.8866
Epoch 2/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.1360 - accuracy: 0.9491 - val_loss: 0.4250 - val_accuracy: 0.8854
Epoch 3/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.1324 - accuracy: 0.9499 - val_loss: 0.4453 - val_accuracy: 0.8875
Epoch 4/10
60000/60000 [==============================] - 4s 69us/sample - loss: 0.1307 - accuracy: 0.9505 - val_loss: 0.4311 - val_accuracy: 0.8859
Epoch 5/10
60000/60000 [==============================] - 4s 73us/sample - loss: 0.1281 - accuracy: 0.9513 - val_loss: 0.4369 - val_accuracy: 0.8830
Epoch 6/10
60000/60000 [==============================] - 4s 68us/sample - loss: 0.1253 - accuracy: 0.9529 - val_loss: 0.4291 - val_accuracy: 0.8850
Epoch 7/10
60000/60000 [==============================] - 4s 59us/sample - loss: 0.1231 - accuracy: 0.9541 - val_loss: 0.4488 - val_accuracy: 0.8847
Epoch 8/10
60000/60000 [==============================] - 4s 61us/sample - loss: 0.1216 - accuracy: 0.9548 - val_loss: 0.4614 - val_accuracy: 0.8821
Epoch 9/10
60000/60000 [==============================] - 4s 72us/sample - loss: 0.1176 - accuracy: 0.9559 - val_loss: 0.4667 - val_accuracy: 0.8814
Epoch 10/10
60000/60000 [==============================] - 4s 65us/sample - loss: 0.1167 - accuracy: 0.9570 - val_loss: 0.4593 - val_accuracy: 0.8854

In [31]:
results = model.evaluate(test_images,test_labels)
print(results)


10000/10000 [==============================] - 0s 26us/sample - loss: 0.4593 - accuracy: 0.8854
[0.45925962738990783, 0.8854]

In [33]:
history_dicts = history.history
print(history_dicts.keys())


dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])