如何解决-从tensorflow.examples.tutorials. mist import input_data错



我正在尝试导入运行两层模型所需的所有库。然而,当我添加以下

from tensorflow.examples.tutorials.mnist import input_data

它在下面显示黄色的卷线,就好像是一个错误。我相信它显示花线的原因是因为my:/venv和/或系统无法访问上述导入。

以下是完整的代码:

#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)

当我运行上面的代码时,结果是一个错误,如下所示:

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'

询问是否有人可以建议解决这个问题。

谢谢

您的系统上似乎没有安装tensorflow.examples模块。与单独安装模块相反,在您的情况下,一个简单的修复方法是像这样获取数据集:

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

下面是示例的完整工作代码:

#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)

相关内容

  • 没有找到相关文章