ModuleNotFoundError: No module named 'tensorflow.examples' (i am just tried now)

ModuleNotFoundError: No module named 'tensorflow.examples' (i am just tried now)

试了很多次,方法很多,还是不行

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_dataput_data

在tensorflow 2中,不需要turorial包,使用:

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

您可以阅读更多内容:here

如果你想加载 MNIST 数据集,你可以试试这个:

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

或者您可以像下面这样使用 tensorflow_datasets

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

输出: