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 to load and manipulate the MNIST (Modified National Institute of Standards and Technology) database. We learned 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) 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))

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

 Simple TensorFlow Model on MNIST Database

 Commonly Used TensorFlow Funcitons

 PyTorch - Machine Learning Platform

 CNN (Convolutional Neural Network)

 RNN (Recurrent Neural Network)

 GNN (Graph Neural Network)

 References

 Full Version in PDF/EPUB