Home > OS >  How to solve - from tensorflow.examples.tutorials.mnist import input_data Error
How to solve - from tensorflow.examples.tutorials.mnist import input_data Error

Time:05-11

I'm trying import all the libraries needed to run a two-layer model. However when I add following

from tensorflow.examples.tutorials.mnist import input_data

It shows yellow curly lines below the same as if its an error. I believe the reason it shows the curly lines is because my :/venv and/or the system cannot access the said import.

Following is the complete code.

#Import multiple libraries

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image

# import fashion MNIST
fashion_mnist = input_data.read_data_sets('input_data', one_hot=True)
(train_images, train_labels), (test_images, test_labels) \
    = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Touser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankel boot']

train_images = train_images / 255.0

test_images = test_images / 255.0

model = tf.keras.Sequential()

model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.train.AdamOptimizer(), loss='sparse_catogerical_crossentropy', metrics=['accuracy'])

model.fit(train_images, test_images, epoch=5)

# test with 10,000 images
test_loss, test_acc = model.evaluate(train_images, test_labels)

print('10,000 test image accuracy:', test_acc)

When I run the code above it results to an error which is as follows.

Traceback (most recent call last):
  File "c:/Users/coderex/Documents/Py3.0/AIO/myenv/fmtensorflow.py", line 9, in <module>
    from tensorflow.examples.tutorials.mnist import input_data
ModuleNotFoundError: No module named 'tensorflow.examples'

Requesting if someone can suggest a solution to this problem.

Thank you

CodePudding user response:

It seems that the tensorflow.examples module is not installed on your system. As opposed to installing the module separately, an easy fix in your case is to obtain the dataset like so:

import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data()

Here is the complete working code for your example:

#Import multiple libraries

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns
import tensorflow as tf
from tensorflow.python.framework import ops
from PIL import Image

# import fashion MNIST
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Touser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankel boot']

train_images = train_images / 255.0

test_images = test_images / 255.0

model = tf.keras.Sequential()

model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer=tf.optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5)

# test with 10,000 images
test_loss, test_acc = model.evaluate(train_images, test_labels)

print('10,000 test image accuracy:', test_acc)
  • Related