import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
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 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz 26427392/26421880 [==============================] - 1s 0us/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 [==============================] - 0s 0us/step
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
f, ax = plt.subplots(1,2)
ax[0].imshow(train_images[40])
ax[1].imshow(train_images[50])
print(train_labels[40],train_labels[50])
print(class_names[6],class_names[6])
6 3 Shirt Shirt
train_images = train_images / 255.0
test_images = test_images / 255.0
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
Train on 60000 samples Epoch 1/10 60000/60000 [==============================] - 4s 68us/sample - loss: 0.5175 - acc: 0.8194 Epoch 2/10 60000/60000 [==============================] - 4s 62us/sample - loss: 0.3921 - acc: 0.8615 Epoch 3/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.3565 - acc: 0.8711 Epoch 4/10 60000/60000 [==============================] - 4s 60us/sample - loss: 0.3330 - acc: 0.8784 Epoch 5/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.3146 - acc: 0.8836 Epoch 6/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.3026 - acc: 0.8891 Epoch 7/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.2893 - acc: 0.8948 Epoch 8/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.2778 - acc: 0.8982 Epoch 9/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.2695 - acc: 0.9009 Epoch 10/10 60000/60000 [==============================] - 4s 61us/sample - loss: 0.2632 - acc: 0.9036
<tensorflow.python.keras.callbacks.History at 0x7f4ffa687400>
prediction=model.predict(test_images[1].reshape(1, 28, 28))
print("Probabilities of image in each class are",prediction)
print("Highest probability in place:", prediction.argmax())
print("Image is classified as a: ",class_names[prediction.argmax()])
Probabilities of image in each class are [[4.7269925e-05 6.7162512e-11 9.9416757e-01 7.9031757e-08 2.6153151e-03 7.7022203e-14 3.1697203e-03 3.2766182e-15 1.2217416e-08 4.0623840e-15]] Highest probability in place: 2 Image is classified as a: Pullover
print("Actual label number is:",test_labels[1])
print("Image is a: ",class_names[test_labels[1]])
plt.imshow(test_images[1])
Actual label number is: 2 Image is a: Pullover
<matplotlib.image.AxesImage at 0x7f4ff5013cc0>
test_acc = model.evaluate(test_images, test_labels)
print('Test loss, accuracy:', test_acc)
10000/10000 [==============================] - 0s 34us/sample - loss: 0.3430 - acc: 0.8819 Test loss, accuracy: [0.34303882870674135, 0.8819]
prediction=model.predict(test_images[12].reshape(1, 28, 28))
print("Probabilities of image in each class are",prediction)
print("Highest probability in place:", prediction.argmax())
print("Image is classified as a: ",class_names[prediction.argmax()])
print("Actual label number is:",test_labels[12])
print("Image is a: ",class_names[test_labels[12]])
plt.imshow(test_images[12])
Probabilities of image in each class are [[2.9833541e-07 3.4984214e-07 5.2938790e-06 2.2196046e-07 3.0843472e-05 1.4087908e-01 5.4214041e-08 2.2918980e-01 6.2989372e-01 2.9229400e-07]] Highest probability in place: 8 Image is classified as a: Bag Actual label number is: 7 Image is a: Sneaker
<matplotlib.image.AxesImage at 0x7f4ffbfe4470>
prediction=model.predict(test_images[17].reshape(1, 28, 28))
print("Probabilities of image in each class are",prediction)
print("Highest probability in place:", prediction.argmax())
print("Image is classified as a: ",class_names[prediction.argmax()])
print("Actual label number is:",test_labels[17])
print("Image is a: ",class_names[test_labels[17]])
plt.imshow(test_images[17])
Probabilities of image in each class are [[2.6976159e-03 2.6915019e-09 9.8906553e-01 4.8810068e-07 3.9343471e-03 4.9612314e-10 4.2868727e-03 5.2282022e-11 1.5191695e-05 1.4628523e-10]] Highest probability in place: 2 Image is classified as a: Pullover Actual label number is: 4 Image is a: Coat
<matplotlib.image.AxesImage at 0x7f4ff1fb0f28>