mnist.read_data_sets() Is Deprecated

This section provides a tutorial example on how to use tensorflow.keras.datasets.mnist module instead of the tensorflow.examples.tutorials.mnist module to avoid the mnist.read_data_sets() deprecation warning messages.

If you are using TensorFlow 1.14.0, you will notice that the tensorflow.examples.tutorials.mnist marked as deprecated. If you continue to run the mnist_dataset.py script, you will get the following warning messages:

herong$ python3 mnist_dataset.py

WARNING:tensorflow:From mnist_dataset.py:7: read_data_sets 
  (from tensorflow.contrib.learn.python.learn.datasets.mnist) 
  is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py 
  from tensorflow/models.

WARNING:tensorflow:From 
  /.../tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: 
  maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) 
  is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.

...

The warning message is clear that the tensorflow.examples.tutorials.mnist module is deprecated. But instructions for updating is useless: "Please write your own downloading logic." Why deprecating a module without a good replacement?

Searching the Internet, I found 2 options to deal with the warning messages:

1. Turn off warning messages - Using tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) to stop TensorFlow from printing those warning messages.

Here is the revised version of my MNIST test script, mnist_dataset_no_warning.py:

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

import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

# 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))

2. Use tensorflow.keras.datasets.mnist module - The tensorflow.keras.datasets.mnist module offers the load_data() method to download MNIST datasets from the Internet.

Here is the revised version of my MNIST test script, mnist_dataset_keras.py:

#- mnist_dataset_keras.py
#- Copyright (c) 2019 HerongYang.com. All Rights Reserved.
#
import tensorflow.keras.datasets.mnist as mnist
import numpy as np

# load the MNIST dataset from Internet
# as numpy.ndarray of shapes x:(*, 28, 28) and y:(*,)
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# normalize feature values
x_train, x_test = x_train/255.0, x_test/255.0

# fetch next 10 samples from the training set.  
max = x_train.shape[0];
idx = np.random.randint(max, size=10)
xs = x_train.take(idx, 0)
ys = y_train.take(idx, 0)

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

# convert grey scale to black and white
bw = np.rint(x).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))

Run the revised version. I see no issues.

herong$ python3 mnist_dataset_keras.py

The input image in ascii
[[                            ]
 [                            ]
 [                            ]
 [                            ]
 [                            ]
 [                            ]
 [                            ]
 [        1111    1 1111      ]
 [        111111111111111     ]
 [       1111111111111111     ]
 [      1111111111111111      ]
 [      1111111    11111      ]
 [     11111       11111      ]
 [     1111       11111       ]
 [     1111      11111        ]
 [     111       11111        ]
 [              11111         ]
 [             11111          ]
 [             11111          ]
 [            11111           ]
 [            11111           ]
 [           111111           ]
 [           111111           ]
 [           111111           ]
 [          11111             ]
 [          11111             ]
 [           11               ]
 [                            ]]

The target label: 7

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