28

When I import tensorflow

import tensorflow as tf

I don't get an error. However, I do get the error below. I'm using spyder if that helps.

As per other questions, I ensured up to date (v1.8) tensorflow using both conda and then pip installs. This didn't resolve the issue. Please assist.

import tensorflow.examples.tutorials.mnist.input_data as input_data

ModuleNotFoundError: No module named 'tensorflow.examples'
alwayscurious
  • 1,155
  • 1
  • 8
  • 18

12 Answers12

25

I think you should use like bellow on tensorflow 2

import tensorflow_datasets
mnist = tensorflow_datasets.load('mnist')
Ibrahim COBANI
  • 251
  • 3
  • 2
10

Use the following, it will download the data. It is from the tensorflow documentation

import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
Fares Sayah
  • 121
  • 1
  • 5
9

To load the mnist dataset in Tensorflow 2.0:

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

Here is the reference: TensorFlow 2 quickstart for beginners

Another method(also works for locally saved dataset):

DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'

path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
  train_examples = data['x_train']
  train_labels = data['y_train']
  test_examples = data['x_test']
  test_labels = data['y_test']

Here is the reference: Load NumPy data

joshijai2
  • 157
  • 1
  • 11
6

Sometimes the TensorFlow examples are not pre-downloaded, so you might need to run the below command to install the examples from Github using the below code.

!pip install -q git+https://github.com/tensorflow/examples.git
Akshay Rana
  • 301
  • 3
  • 6
3

Sometimes on downloading the TF, the example directory might not be available. You could rectify it by linking the 'example' directory from the GitHub repo into the tensorflow python wheel folder. That way you don't need to change the code.

If this doesn't work, try to replace import tensorflow.examples.tutorials.mnist.input_data as input_data as import input_data as mentioned in the link: TensorFlow MNIST example not running with fully_connected_feed.py

Hope this helps!!!

ReInvent_IO
  • 477
  • 3
  • 13
  • Thanks. After deleting various installations and reinstalling tensorflow, dask and numpy eventually it corrected itself. I'm not sure what actually sorted it out. – alwayscurious May 14 '18 at 20:48
1

Different approach OS: Windows

copy from:

https://github.com/tensorflow/examples/tree/master/tensorflow_examples

to

[python folder]\Lib\site-packages\tensorflow_examples

use

import tensorflow_examples

example

from tensorflow_examples.models import pix2pix

but for datasets use:

pip install tensorflow_datasets
Mr_JD_Free
  • 11
  • 2
1

I solved this issue by adding **tutorial** directory into tensorflow_core, usually this issue pops up when lacking of this file

  1. ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples check this directory to see if you have tutorials file. lack of tutorial file
  2. If you do not have, then go to https://github.com/tensorflow/tensorflow download the zip file, and extract all (or open it). download tutorial file
  3. find tutorials file from tensorflow-master\tensorflow\examples\, and copy it to ..\anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\examples.
  4. Issue resolved. run
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
im = mnist.train.images[1]
im = im.reshape(-1, 28)
plt.imshow(im)

Jaden
  • 192
  • 4
  • 15
0

I solved this issue on Mac, simply copy the official examples to tensorflow_core/examples directory.

  1. pull the tensorflow code

    git clone https://github.com/tensorflow/tensorflow

  2. copy the examples to the system python3 directory

    cp -a tensorflow/examples/ /usr/local/lib/python3.7/site-packages/tensorflow_core/examples/

Community
  • 1
  • 1
Sunding Wei
  • 1,803
  • 17
  • 12
0

You need to download the data sets to use it.

Command:

pip install tensorflow-datasets

Code part:

mnist_train = tfds.load(name="mnist", split="train")

You are done now. Happy coding! :)

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
Ninja Master
  • 126
  • 3
0

You just need to download the lost files and copy them to the tensorflow-core/ examples.

for me on windows 10 is : C:\Users\Amirreza\AppData\Local\Programs\Python\Python37\Lib\site-packages\tensorflow_core\examples

0

This folder has been deleted at September/2020. See their repository.

I used the commands:

git clone https://github.com/tensorflow/tensorflow.git
tensorflow> git checkout c31acd156c
0

I solved this issue by installing Keras according to this answer from different question:

I didn't have Keras installed and had to add it manualy

ImportError: No module named 'keras'

Adam Kuzański
  • 509
  • 4
  • 12