首页 > 解决方案 > 模块“tensorflow.keras.datasets.mnist”没有属性“x_train”

问题描述

在下载 MNIST 数据集并将其加载到各自的变量中后,我尝试打印 x_tain、x_test、y_train、y_test 的值。然后它向我展示了这个错误。

import tensorflow as tf
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print("No of labels in training set {}".format(mnist.y_train.labels.shape))
print("No of images in test set {}".format(mnist.x_test.images.shape))
print("No of labels in test set {}".format(mnist.y_test.labels.shape))
print("No of images in training set {}".format(mnist.x_train.images.shape))

执行后显示如下错误 module 'tensorflow.keras.datasets.mnist' has no attribute 'x_train'

标签: tensorflowmachine-learningdeep-learningmnist

解决方案


tf.keras.datasets.mnist除了 .module 之外,确实没有任何其他成员load_data。因此,在加载值之前到处添加模块名称mnist是没有意义的。您加载了您的数据,(x_train, y_train), (x_test, y_test) 并且您可以使用它们。不需要mnist.y_train,只需使用y_train


推荐阅读