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

Question:

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
Asked By: Rasel

||

Answers:

In in tensorflow 2, you don’t need turorial package, use:

tf.keras.datasets.mnist.load_data(
    path='mnist.npz'
)

You can read more : here

Answered By: saleh sargolzaee

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

It seems that tensorflow has creatd seperated repo for datasets now..just import below :

import tensorflow_datasets as datasets
mnist = datasets.load(name=’mnist’)

..This might require bit of installing of other deependncies like below: if you run jupyeter from your machine.But on Colab it will import it in a jiffy since colab EC2/docker instance asigned to you will have these preinstalled.

Below were the dependcaies I neede to isntall since I run jupyter from Ananconda.

  1. pip install tensorflow-datasets
  2. conda install -c conda-forge ipywidgets
  3. pip install ipywidgets
  4. pip install IProgress
  5. jupyter nbextension enable –py widgetsnbextension
  6. pip install ipywidgets widgetsnbextension pandas-profiling
  7. conda install -c conda-forge nodejs=16.6.1
Answered By: Damascus