Last answered:

17 Jan 2024

Posted on:

27 Apr 2021

1

Importing MNIST dataset - Deeper Example

After running the first part of the code I get this error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-917aabef6fe5> in <module>
      1 import numpy as np
      2 import tensorflow as tf
----> 3 from tensorflow.examples.tutorials.mnist import input_data
      4 
      5 # TensorFLow includes a data provider for MNIST that we'll use.

ModuleNotFoundError: No module named 'tensorflow.examples'

I searched it online and found that in tensorflow documentation, it is recommended doing it in  this way:

builder = tfds.load(name="mnist")
builder.download_and_prepare()
mnist = builder.as_dataset(split='train', shuffle_files=True)

But then I get this error

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-7-2bcf21631d35> in <module>
      1 import numpy as np
      2 import tensorflow as tf
----> 3 import tensorflow_datasets as tfds
      4 
      5 #from tensorflow.examples.tutorials.mnist import input_data

ModuleNotFoundError: No module named 'tensorflow_datasets'

Any ideas ?

Thanks in advance.

2 answers ( 0 marked as helpful)
Posted on:

29 Jun 2021

0

Just replace the last two lines of the code. I searched Github and this solution worked for me:

mnist = tf.keras.datasets.mnist

Posted on:

17 Jan 2024

0

Here is the code I use


import tensorflow as tf
import tensorflow_datasets as tfds
from keras.datasets import mnist

import os

#get current directory for file
current_dir = os.getcwd()

(train_images,train_labels),(test_images,test_labels) = mnist.load_data(path=current_dir+"/mnist.npz")
#Create Tensorflow Dataset
mnist_train = tf.data.Dataset.from_tensor_slices((train_images, train_labels))
mnist_test = tf.data.Dataset.from_tensor_slices((test_images, test_labels))

Submit an answer