tensorflow.examples.tutorials.mnist Module

This section provides a tutorial example on how to load the MNIST database using the tensorflow.examples.tutorials.mnist module. MNIST contains a large number of images of handwritten digits.

TensorFlow comes with a tutorial module called tensorflow.examples.tutorials.mnist, which allows you to load and manipulate the MNIST (Modified National Institute of Standards and Technology) database. As mentioned earlier in this book, the MNIST database is the “hello world” database for machine learning. It contains a large number of 28x28 images of handwritten digits preprocessed and ready to be used to test machine learning models.

Let's load and play with the MNIST database using functions provided in the tensorflow.examples.tutorials.mnist module.

#- mnist_dataset.py
#- Copyright (c) 2019 HerongYang.com. All Rights Reserved.
#
import tensorflow.examples.tutorials.mnist.input_data as mnist

# load the MNIST dataset
ds = mnist.read_data_sets("MNIST_data/")

# fetch next 10 samples from the dataset as features and target
xs, ys = ds.train.next_batch(10)

# take the first sample
x = xs[0]
y = ys[0]

# convert features back to a 28x28 grey scale image
grey = x.reshape(28,28)

# convert grey scale to black and white
import numpy as np
bw = np.rint(grey).astype(int)

# convert grey scale to black and white
ascii = str(bw).replace(' ','').replace('0', ' ')

# show the ascii image and the target label.
print("The input image in ascii\n"+ascii)
print("The target label: "+str(y))

To run the script, you need to download the MNIST dataset in the sub-directory, MNIST_data. There are 4 files in the MNIST dataset:

herong$ ls -l MNIST_data

 1648877 Aug 11  2020 t10k-images-idx3-ubyte.gz
    4542 Aug 11  2020 t10k-labels-idx1-ubyte.gz
 9912422 Aug 11  2020 train-images-idx3-ubyte.gz
   28881 Aug 11  2020 train-labels-idx1-ubyte.gz

If you run the script, you should get a handwritten digit of 3. Or you may get a different number, because the MNIST dataset is loaded in random orders.

herong$ python3 mnist_dataset.py

The input image in ascii
[[                            ]
 [                            ]
 [                            ]
 [                            ]
 [         111 111111         ]
 [        11111111111111      ]
 [        11111111111111      ]
 [         1111111111111      ]
 [                  1111      ]
 [                111111      ]
 [              1111111       ]
 [           111111111        ]
 [          11111111111       ]
 [          111111111111      ]
 [      11         11111      ]
 [     1111         11111     ]
 [     1111          1111     ]
 [     1111          111      ]
 [     1111         1111      ]
 [     1111        11111      ]
 [      1111111111111111      ]
 [      111111111111111       ]
 [        11111111111         ]
 [          11111             ]
 [                            ]
 [                            ]
 [                            ]
 [                            ]]

The target label: 3

Table of Contents

 About This Book

 Deep Playground for Classical Neural Networks

 Building Neural Networks with Python

 Simple Example of Neural Networks

TensorFlow - Machine Learning Platform

 What Is TensorFlow

 "tensorflow" - TensorFlow Python Library

 "tensorflow" Interactive Test Web Page

 Tensor and Tensor Flow Graph

 Tensor Operation Properties

 TensorFlow Session Class and run() Function

 TensorFlow Variable Class and load() Function

 Linear Regression with TensorFlow

tensorflow.examples.tutorials.mnist Module

 mnist.read_data_sets() Is Deprecated

 Simple TensorFlow Model on MNIST Database

 Commonly Used TensorFlow functions

 PyTorch - Machine Learning Platform

 Gradio - ML Demo Platform

 CNN (Convolutional Neural Network)

 RNN (Recurrent Neural Network)

 GNN (Graph Neural Network)

 GAN (Generative Adversarial Network)

 Performance Evaluation Metrics

 References

 Full Version in PDF/EPUB