Deep learning in E-Commerce
Most of the E-commerce companies moved from inventory-led model to market place model in recent times. Mainly because in inventory led model one simply don’t harness the potential of internet as platform. Where as in Market place model you empowers someone who can source an item and you let someone buy who is looking for an item. In market place model there is a buyer on one side and seller on one side and the company in middle simply manage the payment and fulfillment and catalogue along with other aspects.
The market place model give opportunity to a large number of sellers to sell their products on the e-commerce website by a simple registeration process and sharing the product information like images, description etc.
So with increase in number of sellers on platform the catalogue size on these market place model e-commerce companies increases and it becomes much more difficult to maintain relevant and accurate information. I think AI can be really helpful in this.
I have worked in e-commerce companies like Snapdeal shopclues etc. Shopclues started as pure marketplace companies unlike other e-commerce companies like snapdeal, flipkart, etc. Now these companies have marketplace model where a large number of sellers want their products to be listed on website. However sometimes these sellers provide images only and no description about the product. In such case catalogue team verify these images and write description about the product manually one by one which is very time consuming and not scalable. I think with the help of AI we can classify product images and extract attribute details from the product images and also beautiful description about the product.
For ex- garment type :- Shirt/Tshirt
Color :- red/blue
Pattern :- solid/Stripes
Sleevelength :- half/full
In this blog we will use the Fashion MNIST database which contains 70,000 black and white images from 10 different categories. Images feature individual low-resolution garments (28 by 28 pixels). We’ll use 60,000 images to train the network and 10,000 images to gauge how well the network has learned to classify images.
#loading libraries
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
You can access the Fashion MNIST database directly from TensorFlow by simply importing the data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
Loading the database returns four NumPy arrays:
- The
train_images
andtrain_labels
arrays are the training set - the data the model uses to learn - The model is tested against the test set, the
test_images
, andtest_labels
arrays
The images are 28x28 Numpy arrays, where the values of each pixel range from 0 to 255. Labels are an integer vector, ranging from 0 to 9.
Each image is mapped to a single label. Since class names are not included in the database, we save here for later use in the graphics of the images:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
Explore the data
Let’s explore the format of the dataset before training the model. The following shows there are 60,000 images in the training set, with each image represented as 28 x 28 pixels:
train_images.shape
## (60000, 28, 28)
Each label is an integer between 0 and 9:
train_labels
## array([9, 0, 0, ..., 3, 0, 5], dtype=uint8)
There are 10,000 images in the test set. Again, each image is represented as 28 x 28 pixels:
test_images.shape
## (10000, 28, 28)
Data Preprocessing
Data must be preprocessed before training the network. If you look at the first image of the training data, you will see that the value of each pixel will be in the range 0 to 255:
plt.figure()
plt.imshow(train_images[1])
plt.colorbar()
## <matplotlib.colorbar.Colorbar object at 0x113517be0>
plt.grid(False)
plt.show()
We scaled these values to be in the range 0 to 1 before entering the neural model. To do this, we divide the values by 255. It is important that training data and test data are processed in the same way:
train_images = train_images / 255.0
test_images = test_images / 255.0
Show the first 25 images of the training data and the class it belongs to under each image. Make sure the data is in the correct format and we are ready to build and train the network.
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]])
plt.show()
Build the model
Building the neural network requires configuring the model layers, and then compiling the model.
Configure the layers
The basic block of a neural network is the layer. Layers extract representations of the data that feeds them. And hopefully, these representations have more meaning to the problem at hand.
Most deep learning consists of chaining simple layers together. Most layers, like tf.keras.layers.Dense
, have parameters that are learned during training.
# Configure the layers
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)
])
## WARNING: Logging before flag parsing goes to stderr.
## W0803 18:15:55.938343 4394472896 deprecation.py:506] From /Users/vidyasagarbhargava/anaconda/lib/python3.5/site-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
## Instructions for updating:
## Call initializer instance with the dtype argument instead of passing it to the constructor
The first layer of this network tf.keras.layers.Flatten
transforms the image format of a 2D vector (28 by 28 pixels) into a 1D vector of 28 * 28 = 784 pixels.This layer has no parameters to learn; just reformat the data.
Once the pixels are aligned, the mesh consists of a two-layer sequence tf.keras.layers.Dense
. These are categorized as densely-connected , or fully-connected . The first layer Densehas 128 nodes (or neurons). The second (and last) is a 10-node softmax layer - this returns a vector with 10 probability values that sum to 1. Each node contains the value that indicates the probability that the current image belongs to one of 10 classes.
Compile the model
Before the model is ready to train, it needs some more setup. These are added in the compile step :
Cost Function
- This measures how well the model ranks during training. We want to minimize this function to fit the model in the right direction.Optimizer
- This is used to update the model based on data and cost function.Metrics
- These are used to monitor training and test steps. The next example uses accuracy , the fraction of images that are correctly classified.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Train the model
Training the neural network model requires the following steps:
- Feed the model with training data, in this case the vectors
train_images
train_labels
.
- The model learns to associate images and labels.
- We ask the model to make a prediction on the test data - in this example, the vector
test_images
. We check the predictions with the labels vectortest_labels
.
To start training, we call the method model.fit
- the model will be the approximate one to describe the training data:
model.fit(train_images, train_labels, epochs=5)
## Epoch 1/5
##
32/60000 [..............................] - ETA: 2:43 - loss: 2.4791 - acc: 0.0312
896/60000 [..............................] - ETA: 9s - loss: 1.4486 - acc: 0.5067
1760/60000 [..............................] - ETA: 6s - loss: 1.1777 - acc: 0.5972
2592/60000 [>.............................] - ETA: 5s - loss: 1.0412 - acc: 0.6431
3456/60000 [>.............................] - ETA: 4s - loss: 0.9518 - acc: 0.6701
4352/60000 [=>............................] - ETA: 4s - loss: 0.8945 - acc: 0.6914
5280/60000 [=>............................] - ETA: 4s - loss: 0.8512 - acc: 0.7095
6176/60000 [==>...........................] - ETA: 3s - loss: 0.8134 - acc: 0.7210
7008/60000 [==>...........................] - ETA: 3s - loss: 0.7882 - acc: 0.7302
7648/60000 [==>...........................] - ETA: 3s - loss: 0.7678 - acc: 0.7371
8224/60000 [===>..........................] - ETA: 3s - loss: 0.7536 - acc: 0.7411
8736/60000 [===>..........................] - ETA: 3s - loss: 0.7424 - acc: 0.7457
9280/60000 [===>..........................] - ETA: 3s - loss: 0.7329 - acc: 0.7497
9920/60000 [===>..........................] - ETA: 3s - loss: 0.7229 - acc: 0.7521
10816/60000 [====>.........................] - ETA: 3s - loss: 0.7092 - acc: 0.7563
11712/60000 [====>.........................] - ETA: 3s - loss: 0.6959 - acc: 0.7606
12608/60000 [=====>........................] - ETA: 3s - loss: 0.6839 - acc: 0.7641
13536/60000 [=====>........................] - ETA: 3s - loss: 0.6732 - acc: 0.7671
14400/60000 [======>.......................] - ETA: 3s - loss: 0.6637 - acc: 0.7694
15296/60000 [======>.......................] - ETA: 3s - loss: 0.6551 - acc: 0.7731
16160/60000 [=======>......................] - ETA: 3s - loss: 0.6459 - acc: 0.7759
16992/60000 [=======>......................] - ETA: 2s - loss: 0.6402 - acc: 0.7764
17856/60000 [=======>......................] - ETA: 2s - loss: 0.6332 - acc: 0.7787
18752/60000 [========>.....................] - ETA: 2s - loss: 0.6263 - acc: 0.7806
19680/60000 [========>.....................] - ETA: 2s - loss: 0.6200 - acc: 0.7827
20608/60000 [=========>....................] - ETA: 2s - loss: 0.6139 - acc: 0.7848
21504/60000 [=========>....................] - ETA: 2s - loss: 0.6098 - acc: 0.7870
22368/60000 [==========>...................] - ETA: 2s - loss: 0.6039 - acc: 0.7890
23296/60000 [==========>...................] - ETA: 2s - loss: 0.6004 - acc: 0.7903
24192/60000 [===========>..................] - ETA: 2s - loss: 0.5948 - acc: 0.7923
25056/60000 [===========>..................] - ETA: 2s - loss: 0.5906 - acc: 0.7935
25920/60000 [===========>..................] - ETA: 2s - loss: 0.5870 - acc: 0.7945
26752/60000 [============>.................] - ETA: 2s - loss: 0.5833 - acc: 0.7956
27648/60000 [============>.................] - ETA: 2s - loss: 0.5781 - acc: 0.7974
28576/60000 [=============>................] - ETA: 2s - loss: 0.5723 - acc: 0.7997
29504/60000 [=============>................] - ETA: 1s - loss: 0.5673 - acc: 0.8015
30400/60000 [==============>...............] - ETA: 1s - loss: 0.5640 - acc: 0.8027
31264/60000 [==============>...............] - ETA: 1s - loss: 0.5608 - acc: 0.8037
32192/60000 [===============>..............] - ETA: 1s - loss: 0.5570 - acc: 0.8048
33088/60000 [===============>..............] - ETA: 1s - loss: 0.5547 - acc: 0.8056
33952/60000 [===============>..............] - ETA: 1s - loss: 0.5523 - acc: 0.8062
34816/60000 [================>.............] - ETA: 1s - loss: 0.5496 - acc: 0.8071
35712/60000 [================>.............] - ETA: 1s - loss: 0.5470 - acc: 0.8080
36608/60000 [=================>............] - ETA: 1s - loss: 0.5443 - acc: 0.8089
37504/60000 [=================>............] - ETA: 1s - loss: 0.5426 - acc: 0.8097
38368/60000 [==================>...........] - ETA: 1s - loss: 0.5404 - acc: 0.8104
39264/60000 [==================>...........] - ETA: 1s - loss: 0.5378 - acc: 0.8113
40192/60000 [===================>..........] - ETA: 1s - loss: 0.5353 - acc: 0.8120
41088/60000 [===================>..........] - ETA: 1s - loss: 0.5333 - acc: 0.8126
41952/60000 [===================>..........] - ETA: 1s - loss: 0.5313 - acc: 0.8134
42848/60000 [====================>.........] - ETA: 1s - loss: 0.5298 - acc: 0.8138
43744/60000 [====================>.........] - ETA: 1s - loss: 0.5276 - acc: 0.8143
44672/60000 [=====================>........] - ETA: 0s - loss: 0.5273 - acc: 0.8145
45568/60000 [=====================>........] - ETA: 0s - loss: 0.5248 - acc: 0.8155
46496/60000 [======================>.......] - ETA: 0s - loss: 0.5226 - acc: 0.8164
47360/60000 [======================>.......] - ETA: 0s - loss: 0.5209 - acc: 0.8169
48256/60000 [=======================>......] - ETA: 0s - loss: 0.5204 - acc: 0.8171
49184/60000 [=======================>......] - ETA: 0s - loss: 0.5186 - acc: 0.8175
50048/60000 [========================>.....] - ETA: 0s - loss: 0.5166 - acc: 0.8183
50944/60000 [========================>.....] - ETA: 0s - loss: 0.5154 - acc: 0.8184
51808/60000 [========================>.....] - ETA: 0s - loss: 0.5136 - acc: 0.8190
52704/60000 [=========================>....] - ETA: 0s - loss: 0.5116 - acc: 0.8197
53632/60000 [=========================>....] - ETA: 0s - loss: 0.5104 - acc: 0.8202
54560/60000 [==========================>...] - ETA: 0s - loss: 0.5082 - acc: 0.8211
55296/60000 [==========================>...] - ETA: 0s - loss: 0.5070 - acc: 0.8216
56192/60000 [===========================>..] - ETA: 0s - loss: 0.5069 - acc: 0.8217
57088/60000 [===========================>..] - ETA: 0s - loss: 0.5053 - acc: 0.8223
57984/60000 [===========================>..] - ETA: 0s - loss: 0.5032 - acc: 0.8227
58880/60000 [============================>.] - ETA: 0s - loss: 0.5019 - acc: 0.8230
59808/60000 [============================>.] - ETA: 0s - loss: 0.5006 - acc: 0.8235
60000/60000 [==============================] - 4s 60us/sample - loss: 0.5003 - acc: 0.8235
## Epoch 2/5
##
32/60000 [..............................] - ETA: 7s - loss: 0.3907 - acc: 0.8438
928/60000 [..............................] - ETA: 3s - loss: 0.3890 - acc: 0.8782
1856/60000 [..............................] - ETA: 3s - loss: 0.3909 - acc: 0.8669
2752/60000 [>.............................] - ETA: 3s - loss: 0.4016 - acc: 0.8608
3648/60000 [>.............................] - ETA: 3s - loss: 0.4034 - acc: 0.8596
4576/60000 [=>............................] - ETA: 3s - loss: 0.4045 - acc: 0.8573
5440/60000 [=>............................] - ETA: 3s - loss: 0.4015 - acc: 0.8585
6368/60000 [==>...........................] - ETA: 3s - loss: 0.3977 - acc: 0.8598
7200/60000 [==>...........................] - ETA: 3s - loss: 0.3927 - acc: 0.8601
8096/60000 [===>..........................] - ETA: 2s - loss: 0.3962 - acc: 0.8597
8960/60000 [===>..........................] - ETA: 2s - loss: 0.3954 - acc: 0.8595
9856/60000 [===>..........................] - ETA: 2s - loss: 0.3946 - acc: 0.8595
10784/60000 [====>.........................] - ETA: 2s - loss: 0.3956 - acc: 0.8583
11712/60000 [====>.........................] - ETA: 2s - loss: 0.3950 - acc: 0.8582
12640/60000 [=====>........................] - ETA: 2s - loss: 0.3910 - acc: 0.8595
13568/60000 [=====>........................] - ETA: 2s - loss: 0.3878 - acc: 0.8607
14496/60000 [======>.......................] - ETA: 2s - loss: 0.3860 - acc: 0.8608
15392/60000 [======>.......................] - ETA: 2s - loss: 0.3847 - acc: 0.8619
16288/60000 [=======>......................] - ETA: 2s - loss: 0.3863 - acc: 0.8622
17184/60000 [=======>......................] - ETA: 2s - loss: 0.3895 - acc: 0.8602
18080/60000 [========>.....................] - ETA: 2s - loss: 0.3886 - acc: 0.8602
18944/60000 [========>.....................] - ETA: 2s - loss: 0.3856 - acc: 0.8613
19872/60000 [========>.....................] - ETA: 2s - loss: 0.3853 - acc: 0.8621
20704/60000 [=========>....................] - ETA: 2s - loss: 0.3855 - acc: 0.8619
21600/60000 [=========>....................] - ETA: 2s - loss: 0.3852 - acc: 0.8614
22496/60000 [==========>...................] - ETA: 2s - loss: 0.3872 - acc: 0.8606
23424/60000 [==========>...................] - ETA: 2s - loss: 0.3847 - acc: 0.8614
24320/60000 [===========>..................] - ETA: 2s - loss: 0.3856 - acc: 0.8614
25216/60000 [===========>..................] - ETA: 1s - loss: 0.3848 - acc: 0.8615
26112/60000 [============>.................] - ETA: 1s - loss: 0.3852 - acc: 0.8612
26976/60000 [============>.................] - ETA: 1s - loss: 0.3845 - acc: 0.8617
27872/60000 [============>.................] - ETA: 1s - loss: 0.3837 - acc: 0.8619
28800/60000 [=============>................] - ETA: 1s - loss: 0.3834 - acc: 0.8616
29696/60000 [=============>................] - ETA: 1s - loss: 0.3842 - acc: 0.8615
30592/60000 [==============>...............] - ETA: 1s - loss: 0.3832 - acc: 0.8619
31520/60000 [==============>...............] - ETA: 1s - loss: 0.3831 - acc: 0.8617
32416/60000 [===============>..............] - ETA: 1s - loss: 0.3832 - acc: 0.8615
33280/60000 [===============>..............] - ETA: 1s - loss: 0.3836 - acc: 0.8615
34176/60000 [================>.............] - ETA: 1s - loss: 0.3840 - acc: 0.8614
35104/60000 [================>.............] - ETA: 1s - loss: 0.3842 - acc: 0.8613
36000/60000 [=================>............] - ETA: 1s - loss: 0.3834 - acc: 0.8615
36928/60000 [=================>............] - ETA: 1s - loss: 0.3846 - acc: 0.8610
37856/60000 [=================>............] - ETA: 1s - loss: 0.3826 - acc: 0.8617
38784/60000 [==================>...........] - ETA: 1s - loss: 0.3819 - acc: 0.8614
39712/60000 [==================>...........] - ETA: 1s - loss: 0.3816 - acc: 0.8614
40640/60000 [===================>..........] - ETA: 1s - loss: 0.3813 - acc: 0.8614
41536/60000 [===================>..........] - ETA: 1s - loss: 0.3817 - acc: 0.8615
42432/60000 [====================>.........] - ETA: 0s - loss: 0.3814 - acc: 0.8617
43328/60000 [====================>.........] - ETA: 0s - loss: 0.3809 - acc: 0.8619
44224/60000 [=====================>........] - ETA: 0s - loss: 0.3803 - acc: 0.8622
45120/60000 [=====================>........] - ETA: 0s - loss: 0.3792 - acc: 0.8628
46016/60000 [======================>.......] - ETA: 0s - loss: 0.3786 - acc: 0.8629
46944/60000 [======================>.......] - ETA: 0s - loss: 0.3790 - acc: 0.8628
47872/60000 [======================>.......] - ETA: 0s - loss: 0.3786 - acc: 0.8629
48800/60000 [=======================>......] - ETA: 0s - loss: 0.3780 - acc: 0.8630
49664/60000 [=======================>......] - ETA: 0s - loss: 0.3775 - acc: 0.8631
50592/60000 [========================>.....] - ETA: 0s - loss: 0.3772 - acc: 0.8632
51488/60000 [========================>.....] - ETA: 0s - loss: 0.3762 - acc: 0.8634
52416/60000 [=========================>....] - ETA: 0s - loss: 0.3761 - acc: 0.8635
53312/60000 [=========================>....] - ETA: 0s - loss: 0.3761 - acc: 0.8637
54240/60000 [==========================>...] - ETA: 0s - loss: 0.3764 - acc: 0.8635
55168/60000 [==========================>...] - ETA: 0s - loss: 0.3754 - acc: 0.8637
56096/60000 [===========================>..] - ETA: 0s - loss: 0.3753 - acc: 0.8637
56960/60000 [===========================>..] - ETA: 0s - loss: 0.3753 - acc: 0.8637
57888/60000 [===========================>..] - ETA: 0s - loss: 0.3749 - acc: 0.8639
58816/60000 [============================>.] - ETA: 0s - loss: 0.3749 - acc: 0.8638
59744/60000 [============================>.] - ETA: 0s - loss: 0.3745 - acc: 0.8639
60000/60000 [==============================] - 3s 56us/sample - loss: 0.3743 - acc: 0.8640
## Epoch 3/5
##
32/60000 [..............................] - ETA: 7s - loss: 0.3422 - acc: 0.8125
928/60000 [..............................] - ETA: 3s - loss: 0.3538 - acc: 0.8653
1824/60000 [..............................] - ETA: 3s - loss: 0.3269 - acc: 0.8783
2688/60000 [>.............................] - ETA: 3s - loss: 0.3207 - acc: 0.8836
3616/60000 [>.............................] - ETA: 3s - loss: 0.3337 - acc: 0.8778
4512/60000 [=>............................] - ETA: 3s - loss: 0.3431 - acc: 0.8723
5408/60000 [=>............................] - ETA: 3s - loss: 0.3334 - acc: 0.8772
6304/60000 [==>...........................] - ETA: 3s - loss: 0.3385 - acc: 0.8767
7232/60000 [==>...........................] - ETA: 3s - loss: 0.3388 - acc: 0.8767
8160/60000 [===>..........................] - ETA: 2s - loss: 0.3332 - acc: 0.8781
9056/60000 [===>..........................] - ETA: 2s - loss: 0.3342 - acc: 0.8778
9856/60000 [===>..........................] - ETA: 2s - loss: 0.3328 - acc: 0.8782
10752/60000 [====>.........................] - ETA: 2s - loss: 0.3341 - acc: 0.8770
11648/60000 [====>.........................] - ETA: 2s - loss: 0.3344 - acc: 0.8771
12512/60000 [=====>........................] - ETA: 2s - loss: 0.3334 - acc: 0.8772
13440/60000 [=====>........................] - ETA: 2s - loss: 0.3345 - acc: 0.8771
14368/60000 [======>.......................] - ETA: 2s - loss: 0.3346 - acc: 0.8767
15296/60000 [======>.......................] - ETA: 2s - loss: 0.3348 - acc: 0.8770
16160/60000 [=======>......................] - ETA: 2s - loss: 0.3349 - acc: 0.8770
17088/60000 [=======>......................] - ETA: 2s - loss: 0.3357 - acc: 0.8766
17952/60000 [=======>......................] - ETA: 2s - loss: 0.3370 - acc: 0.8761
18816/60000 [========>.....................] - ETA: 2s - loss: 0.3381 - acc: 0.8759
19712/60000 [========>.....................] - ETA: 2s - loss: 0.3374 - acc: 0.8762
20608/60000 [=========>....................] - ETA: 2s - loss: 0.3401 - acc: 0.8751
21536/60000 [=========>....................] - ETA: 2s - loss: 0.3420 - acc: 0.8752
22464/60000 [==========>...................] - ETA: 2s - loss: 0.3418 - acc: 0.8753
23264/60000 [==========>...................] - ETA: 2s - loss: 0.3415 - acc: 0.8755
24128/60000 [===========>..................] - ETA: 2s - loss: 0.3412 - acc: 0.8758
25056/60000 [===========>..................] - ETA: 2s - loss: 0.3409 - acc: 0.8758
25984/60000 [===========>..................] - ETA: 1s - loss: 0.3408 - acc: 0.8755
26912/60000 [============>.................] - ETA: 1s - loss: 0.3398 - acc: 0.8759
27840/60000 [============>.................] - ETA: 1s - loss: 0.3398 - acc: 0.8759
28736/60000 [=============>................] - ETA: 1s - loss: 0.3404 - acc: 0.8756
29632/60000 [=============>................] - ETA: 1s - loss: 0.3409 - acc: 0.8754
30528/60000 [==============>...............] - ETA: 1s - loss: 0.3415 - acc: 0.8751
31456/60000 [==============>...............] - ETA: 1s - loss: 0.3423 - acc: 0.8748
32320/60000 [===============>..............] - ETA: 1s - loss: 0.3414 - acc: 0.8751
33248/60000 [===============>..............] - ETA: 1s - loss: 0.3412 - acc: 0.8753
34080/60000 [================>.............] - ETA: 1s - loss: 0.3422 - acc: 0.8749
34944/60000 [================>.............] - ETA: 1s - loss: 0.3423 - acc: 0.8754
35840/60000 [================>.............] - ETA: 1s - loss: 0.3431 - acc: 0.8749
36768/60000 [=================>............] - ETA: 1s - loss: 0.3426 - acc: 0.8751
37664/60000 [=================>............] - ETA: 1s - loss: 0.3431 - acc: 0.8747
38592/60000 [==================>...........] - ETA: 1s - loss: 0.3415 - acc: 0.8751
39520/60000 [==================>...........] - ETA: 1s - loss: 0.3411 - acc: 0.8754
40448/60000 [===================>..........] - ETA: 1s - loss: 0.3407 - acc: 0.8757
41376/60000 [===================>..........] - ETA: 1s - loss: 0.3397 - acc: 0.8762
42304/60000 [====================>.........] - ETA: 1s - loss: 0.3398 - acc: 0.8763
43200/60000 [====================>.........] - ETA: 0s - loss: 0.3396 - acc: 0.8765
44096/60000 [=====================>........] - ETA: 0s - loss: 0.3393 - acc: 0.8765
44992/60000 [=====================>........] - ETA: 0s - loss: 0.3391 - acc: 0.8766
45920/60000 [=====================>........] - ETA: 0s - loss: 0.3378 - acc: 0.8770
46784/60000 [======================>.......] - ETA: 0s - loss: 0.3376 - acc: 0.8769
47680/60000 [======================>.......] - ETA: 0s - loss: 0.3378 - acc: 0.8766
48544/60000 [=======================>......] - ETA: 0s - loss: 0.3380 - acc: 0.8767
49472/60000 [=======================>......] - ETA: 0s - loss: 0.3372 - acc: 0.8766
50400/60000 [========================>.....] - ETA: 0s - loss: 0.3380 - acc: 0.8765
51328/60000 [========================>.....] - ETA: 0s - loss: 0.3386 - acc: 0.8762
52256/60000 [=========================>....] - ETA: 0s - loss: 0.3375 - acc: 0.8767
53152/60000 [=========================>....] - ETA: 0s - loss: 0.3380 - acc: 0.8765
54048/60000 [==========================>...] - ETA: 0s - loss: 0.3376 - acc: 0.8766
54912/60000 [==========================>...] - ETA: 0s - loss: 0.3376 - acc: 0.8766
55776/60000 [==========================>...] - ETA: 0s - loss: 0.3374 - acc: 0.8767
56672/60000 [===========================>..] - ETA: 0s - loss: 0.3377 - acc: 0.8766
57536/60000 [===========================>..] - ETA: 0s - loss: 0.3378 - acc: 0.8764
58464/60000 [============================>.] - ETA: 0s - loss: 0.3381 - acc: 0.8763
59328/60000 [============================>.] - ETA: 0s - loss: 0.3380 - acc: 0.8764
60000/60000 [==============================] - 3s 57us/sample - loss: 0.3383 - acc: 0.8762
## Epoch 4/5
##
32/60000 [..............................] - ETA: 7s - loss: 0.7143 - acc: 0.7812
928/60000 [..............................] - ETA: 3s - loss: 0.3300 - acc: 0.8772
1824/60000 [..............................] - ETA: 3s - loss: 0.3224 - acc: 0.8838
2720/60000 [>.............................] - ETA: 3s - loss: 0.3082 - acc: 0.8886
3616/60000 [>.............................] - ETA: 3s - loss: 0.3160 - acc: 0.8830
4512/60000 [=>............................] - ETA: 3s - loss: 0.3177 - acc: 0.8812
5440/60000 [=>............................] - ETA: 3s - loss: 0.3045 - acc: 0.8869
6368/60000 [==>...........................] - ETA: 3s - loss: 0.3086 - acc: 0.8860
7200/60000 [==>...........................] - ETA: 3s - loss: 0.3072 - acc: 0.8863
8096/60000 [===>..........................] - ETA: 2s - loss: 0.3115 - acc: 0.8851
8992/60000 [===>..........................] - ETA: 2s - loss: 0.3178 - acc: 0.8830
9888/60000 [===>..........................] - ETA: 2s - loss: 0.3191 - acc: 0.8812
10816/60000 [====>.........................] - ETA: 2s - loss: 0.3188 - acc: 0.8816
11744/60000 [====>.........................] - ETA: 2s - loss: 0.3208 - acc: 0.8805
12640/60000 [=====>........................] - ETA: 2s - loss: 0.3196 - acc: 0.8804
13536/60000 [=====>........................] - ETA: 2s - loss: 0.3185 - acc: 0.8800
14464/60000 [======>.......................] - ETA: 2s - loss: 0.3204 - acc: 0.8798
15392/60000 [======>.......................] - ETA: 2s - loss: 0.3202 - acc: 0.8805
16256/60000 [=======>......................] - ETA: 2s - loss: 0.3191 - acc: 0.8813
17184/60000 [=======>......................] - ETA: 2s - loss: 0.3178 - acc: 0.8816
18080/60000 [========>.....................] - ETA: 2s - loss: 0.3189 - acc: 0.8813
19008/60000 [========>.....................] - ETA: 2s - loss: 0.3169 - acc: 0.8820
19936/60000 [========>.....................] - ETA: 2s - loss: 0.3162 - acc: 0.8831
20832/60000 [=========>....................] - ETA: 2s - loss: 0.3154 - acc: 0.8833
21728/60000 [=========>....................] - ETA: 2s - loss: 0.3156 - acc: 0.8833
22624/60000 [==========>...................] - ETA: 2s - loss: 0.3173 - acc: 0.8830
23520/60000 [==========>...................] - ETA: 2s - loss: 0.3161 - acc: 0.8831
24448/60000 [===========>..................] - ETA: 2s - loss: 0.3155 - acc: 0.8832
25376/60000 [===========>..................] - ETA: 1s - loss: 0.3172 - acc: 0.8821
26208/60000 [============>.................] - ETA: 1s - loss: 0.3160 - acc: 0.8826
27104/60000 [============>.................] - ETA: 1s - loss: 0.3167 - acc: 0.8826
27968/60000 [============>.................] - ETA: 1s - loss: 0.3167 - acc: 0.8826
28896/60000 [=============>................] - ETA: 1s - loss: 0.3159 - acc: 0.8829
29824/60000 [=============>................] - ETA: 1s - loss: 0.3156 - acc: 0.8831
30720/60000 [==============>...............] - ETA: 1s - loss: 0.3169 - acc: 0.8826
31584/60000 [==============>...............] - ETA: 1s - loss: 0.3163 - acc: 0.8826
32512/60000 [===============>..............] - ETA: 1s - loss: 0.3165 - acc: 0.8824
33440/60000 [===============>..............] - ETA: 1s - loss: 0.3159 - acc: 0.8826
34304/60000 [================>.............] - ETA: 1s - loss: 0.3154 - acc: 0.8827
35232/60000 [================>.............] - ETA: 1s - loss: 0.3154 - acc: 0.8829
36128/60000 [=================>............] - ETA: 1s - loss: 0.3158 - acc: 0.8827
37024/60000 [=================>............] - ETA: 1s - loss: 0.3160 - acc: 0.8826
37888/60000 [=================>............] - ETA: 1s - loss: 0.3157 - acc: 0.8824
38784/60000 [==================>...........] - ETA: 1s - loss: 0.3150 - acc: 0.8829
39680/60000 [==================>...........] - ETA: 1s - loss: 0.3158 - acc: 0.8825
40608/60000 [===================>..........] - ETA: 1s - loss: 0.3148 - acc: 0.8831
41536/60000 [===================>..........] - ETA: 1s - loss: 0.3152 - acc: 0.8829
42464/60000 [====================>.........] - ETA: 0s - loss: 0.3148 - acc: 0.8830
43392/60000 [====================>.........] - ETA: 0s - loss: 0.3156 - acc: 0.8828
44320/60000 [=====================>........] - ETA: 0s - loss: 0.3154 - acc: 0.8828
45216/60000 [=====================>........] - ETA: 0s - loss: 0.3152 - acc: 0.8830
46112/60000 [======================>.......] - ETA: 0s - loss: 0.3149 - acc: 0.8831
47008/60000 [======================>.......] - ETA: 0s - loss: 0.3146 - acc: 0.8832
47904/60000 [======================>.......] - ETA: 0s - loss: 0.3151 - acc: 0.8830
48800/60000 [=======================>......] - ETA: 0s - loss: 0.3147 - acc: 0.8831
49696/60000 [=======================>......] - ETA: 0s - loss: 0.3148 - acc: 0.8830
50592/60000 [========================>.....] - ETA: 0s - loss: 0.3151 - acc: 0.8828
51520/60000 [========================>.....] - ETA: 0s - loss: 0.3152 - acc: 0.8827
52416/60000 [=========================>....] - ETA: 0s - loss: 0.3152 - acc: 0.8829
53280/60000 [=========================>....] - ETA: 0s - loss: 0.3155 - acc: 0.8828
54176/60000 [==========================>...] - ETA: 0s - loss: 0.3159 - acc: 0.8827
55104/60000 [==========================>...] - ETA: 0s - loss: 0.3169 - acc: 0.8822
56000/60000 [===========================>..] - ETA: 0s - loss: 0.3160 - acc: 0.8826
56896/60000 [===========================>..] - ETA: 0s - loss: 0.3158 - acc: 0.8826
57760/60000 [===========================>..] - ETA: 0s - loss: 0.3159 - acc: 0.8826
58656/60000 [============================>.] - ETA: 0s - loss: 0.3155 - acc: 0.8827
59552/60000 [============================>.] - ETA: 0s - loss: 0.3154 - acc: 0.8828
60000/60000 [==============================] - 3s 56us/sample - loss: 0.3154 - acc: 0.8827
## Epoch 5/5
##
32/60000 [..............................] - ETA: 7s - loss: 0.2902 - acc: 0.8750
960/60000 [..............................] - ETA: 3s - loss: 0.2677 - acc: 0.8969
1888/60000 [..............................] - ETA: 3s - loss: 0.2744 - acc: 0.8972
2816/60000 [>.............................] - ETA: 3s - loss: 0.2727 - acc: 0.8967
3744/60000 [>.............................] - ETA: 3s - loss: 0.2717 - acc: 0.8982
4672/60000 [=>............................] - ETA: 3s - loss: 0.2736 - acc: 0.8996
5568/60000 [=>............................] - ETA: 3s - loss: 0.2758 - acc: 0.8982
6432/60000 [==>...........................] - ETA: 3s - loss: 0.2841 - acc: 0.8965
7328/60000 [==>...........................] - ETA: 2s - loss: 0.2869 - acc: 0.8968
8256/60000 [===>..........................] - ETA: 2s - loss: 0.2863 - acc: 0.8967
9152/60000 [===>..........................] - ETA: 2s - loss: 0.2892 - acc: 0.8957
10048/60000 [====>.........................] - ETA: 2s - loss: 0.2889 - acc: 0.8957
10976/60000 [====>.........................] - ETA: 2s - loss: 0.2918 - acc: 0.8945
11872/60000 [====>.........................] - ETA: 2s - loss: 0.2946 - acc: 0.8929
12800/60000 [=====>........................] - ETA: 2s - loss: 0.2905 - acc: 0.8946
13664/60000 [=====>........................] - ETA: 2s - loss: 0.2890 - acc: 0.8945
14592/60000 [======>.......................] - ETA: 2s - loss: 0.2882 - acc: 0.8943
15488/60000 [======>.......................] - ETA: 2s - loss: 0.2873 - acc: 0.8948
16416/60000 [=======>......................] - ETA: 2s - loss: 0.2874 - acc: 0.8949
17344/60000 [=======>......................] - ETA: 2s - loss: 0.2866 - acc: 0.8951
18240/60000 [========>.....................] - ETA: 2s - loss: 0.2868 - acc: 0.8951
19136/60000 [========>.....................] - ETA: 2s - loss: 0.2882 - acc: 0.8949
20064/60000 [=========>....................] - ETA: 2s - loss: 0.2884 - acc: 0.8946
20960/60000 [=========>....................] - ETA: 2s - loss: 0.2876 - acc: 0.8948
21888/60000 [=========>....................] - ETA: 2s - loss: 0.2878 - acc: 0.8949
22784/60000 [==========>...................] - ETA: 2s - loss: 0.2897 - acc: 0.8944
23680/60000 [==========>...................] - ETA: 2s - loss: 0.2905 - acc: 0.8939
24608/60000 [===========>..................] - ETA: 1s - loss: 0.2909 - acc: 0.8941
25536/60000 [===========>..................] - ETA: 1s - loss: 0.2901 - acc: 0.8939
26464/60000 [============>.................] - ETA: 1s - loss: 0.2910 - acc: 0.8939
27392/60000 [============>.................] - ETA: 1s - loss: 0.2912 - acc: 0.8935
28320/60000 [=============>................] - ETA: 1s - loss: 0.2913 - acc: 0.8930
29184/60000 [=============>................] - ETA: 1s - loss: 0.2908 - acc: 0.8930
30080/60000 [==============>...............] - ETA: 1s - loss: 0.2909 - acc: 0.8930
31008/60000 [==============>...............] - ETA: 1s - loss: 0.2916 - acc: 0.8927
31904/60000 [==============>...............] - ETA: 1s - loss: 0.2911 - acc: 0.8926
32832/60000 [===============>..............] - ETA: 1s - loss: 0.2912 - acc: 0.8929
33728/60000 [===============>..............] - ETA: 1s - loss: 0.2924 - acc: 0.8923
34656/60000 [================>.............] - ETA: 1s - loss: 0.2929 - acc: 0.8921
35584/60000 [================>.............] - ETA: 1s - loss: 0.2928 - acc: 0.8918
36512/60000 [=================>............] - ETA: 1s - loss: 0.2929 - acc: 0.8918
37440/60000 [=================>............] - ETA: 1s - loss: 0.2929 - acc: 0.8918
38368/60000 [==================>...........] - ETA: 1s - loss: 0.2934 - acc: 0.8917
39264/60000 [==================>...........] - ETA: 1s - loss: 0.2954 - acc: 0.8907
40160/60000 [===================>..........] - ETA: 1s - loss: 0.2952 - acc: 0.8909
40992/60000 [===================>..........] - ETA: 1s - loss: 0.2957 - acc: 0.8907
41280/60000 [===================>..........] - ETA: 1s - loss: 0.2957 - acc: 0.8907
41696/60000 [===================>..........] - ETA: 1s - loss: 0.2958 - acc: 0.8906
42240/60000 [====================>.........] - ETA: 1s - loss: 0.2963 - acc: 0.8905
42784/60000 [====================>.........] - ETA: 1s - loss: 0.2964 - acc: 0.8906
43552/60000 [====================>.........] - ETA: 0s - loss: 0.2963 - acc: 0.8908
44320/60000 [=====================>........] - ETA: 0s - loss: 0.2964 - acc: 0.8908
45120/60000 [=====================>........] - ETA: 0s - loss: 0.2963 - acc: 0.8910
45952/60000 [=====================>........] - ETA: 0s - loss: 0.2957 - acc: 0.8912
46752/60000 [======================>.......] - ETA: 0s - loss: 0.2958 - acc: 0.8909
47552/60000 [======================>.......] - ETA: 0s - loss: 0.2954 - acc: 0.8909
48384/60000 [=======================>......] - ETA: 0s - loss: 0.2953 - acc: 0.8909
49280/60000 [=======================>......] - ETA: 0s - loss: 0.2962 - acc: 0.8905
50048/60000 [========================>.....] - ETA: 0s - loss: 0.2962 - acc: 0.8904
50976/60000 [========================>.....] - ETA: 0s - loss: 0.2962 - acc: 0.8904
51808/60000 [========================>.....] - ETA: 0s - loss: 0.2954 - acc: 0.8908
52640/60000 [=========================>....] - ETA: 0s - loss: 0.2949 - acc: 0.8910
53504/60000 [=========================>....] - ETA: 0s - loss: 0.2953 - acc: 0.8908
54400/60000 [==========================>...] - ETA: 0s - loss: 0.2947 - acc: 0.8910
55296/60000 [==========================>...] - ETA: 0s - loss: 0.2946 - acc: 0.8908
56128/60000 [===========================>..] - ETA: 0s - loss: 0.2955 - acc: 0.8906
56832/60000 [===========================>..] - ETA: 0s - loss: 0.2954 - acc: 0.8907
57536/60000 [===========================>..] - ETA: 0s - loss: 0.2947 - acc: 0.8910
58368/60000 [============================>.] - ETA: 0s - loss: 0.2951 - acc: 0.8907
58944/60000 [============================>.] - ETA: 0s - loss: 0.2951 - acc: 0.8906
59520/60000 [============================>.] - ETA: 0s - loss: 0.2944 - acc: 0.8910
60000/60000 [==============================] - 4s 61us/sample - loss: 0.2944 - acc: 0.8910
## <tensorflow.python.keras.callbacks.History object at 0x150bc2278>
As the model trains, the cost function and accuracy metrics are shown. This model achieves an accuracy of about 0.88 (or 88%) in training data.
Evaluate accuracy
Then compare how the model behaves in the test data:
test_loss, test_acc = model.evaluate(test_images, test_labels)
##
32/10000 [..............................] - ETA: 8s - loss: 0.3226 - acc: 0.9062
1280/10000 [==>...........................] - ETA: 0s - loss: 0.3418 - acc: 0.8773
2784/10000 [=======>......................] - ETA: 0s - loss: 0.3407 - acc: 0.8732
4256/10000 [===========>..................] - ETA: 0s - loss: 0.3583 - acc: 0.8689
5824/10000 [================>.............] - ETA: 0s - loss: 0.3633 - acc: 0.8698
7296/10000 [====================>.........] - ETA: 0s - loss: 0.3573 - acc: 0.8723
8768/10000 [=========================>....] - ETA: 0s - loss: 0.3508 - acc: 0.8752
10000/10000 [==============================] - 0s 37us/sample - loss: 0.3486 - acc: 0.8761
print('Test accuracy:', test_acc)
## Test accuracy: 0.8761
Apparently the accuracy of the test data is slightly lower than the accuracy of the training data. This gap between training and test accuracy is an example of overfitting . Which means when a machine learning model works worse on new data than it was trained on.
Make predictions
With the trained model, we can use it to make predictions about some images.
predictions = model.predict(test_images)
Here, the model predicted the label for each image in the training data. Let’s look at the first prediction:
predictions[0]
## array([5.9079719e-07, 2.9904876e-07, 4.3969308e-07, 8.7720204e-09,
## 2.9638118e-06, 8.2773548e-03, 4.1687690e-06, 1.0429406e-02,
## 4.2736665e-05, 9.8124206e-01], dtype=float32)
The forecast is a vector of 10 numbers. These describe the model’s “confidence” that this image belongs to each of the 10 different articles of clothing. We can see which label has the most confidence:
np.argmax(predictions[0])
## 9
So the model is more confident that the image is ankle boot , or class_names[9]. And we can check the test label to verify that it is correct:
test_labels[0]
## 9
We can create the graph to observe all 10 channels.
def plot_image(i, predictions_array, true_label, img):
predictions_array, true_label, img = predictions_array[i], true_label[i], 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.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
Let’s look at image 0, the predictions, and the forecast vector.
i = 0
plt.figure(figsize=(6,3))
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)
plt.show()
i = 12
plt.figure(figsize=(6,3))
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)
plt.show()
Let’s create the graph with multiple images and their predictions. The correct forecasts are in blue and the incorrect forecasts in red. The number indicates the percentage (in 100) for the predicted label. Note that it may be wrong even when the confidence level is high.
# Plot the first X test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
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)
plt.show()
Finally, use the trained model to make predictions about a single image.
img = test_images[0]
print(img.shape)
## (28, 28)
Templates tf.kerasare optimized for forecasting in a batch , or collection, of examples at once. So even though we are only using an image we have to add it to a list:
img = (np.expand_dims(img,0))
print(img.shape)
## (1, 28, 28)
Now make a prediction about the image:
predictions_single = model.predict(img)
print(predictions_single)
## [[5.9079719e-07 2.9904933e-07 4.3969266e-07 8.7720382e-09 2.9638118e-06
## 8.2773464e-03 4.1687767e-06 1.0429416e-02 4.2736745e-05 9.8124206e-01]]
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
np.argmax(predictions_single[0])
## 9
And as before, the model provides the label 9.