[FIXED] ModuleNotFoundError: No module named 'tensorflow.examples' (i am just tried to load mnist)

Issue

I have tried lots of times by taking many ways but it doesn’t work anyway.

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_dataput_data

Solution

If you want to load MNIST dataset, you can try this:

import tensorflow as tf
import matplotlib.pyplot as plt

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

fig, axes = plt.subplots(2,5,figsize=(15,6))
for idx, axe in enumerate(axes.flatten()):
    axe.axis('off')
    axe.set_title(f'label : {y_train[idx]}')
    axe.imshow(x_train[idx])
plt.show()

Or you can use tensorflow_datasets like below:

import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
dataset = tfds.load('mnist', download=True, as_supervised=True, split = 'train').batch(10)
image, label = next(iter(dataset))
fig, axes = plt.subplots(2,5,figsize=(15,6))
for idx, axe in enumerate(axes.flatten()):
    axe.axis('off')
    axe.set_title(f'label : {label[idx]}')
    axe.imshow(image[idx][...,0])
plt.show()

Output:

enter image description here

Answered By – I'mahdi

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published