1.解决问题,我就偏要用keras的
MNIST 数据集在外网,所以咱直接挂个梯子就好了,我是一直没打开所以失败,梯子打开了几秒就成功了
import tensorflow as tf
#加载测试mnist数据集——使用keras库
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train/255, x_test/255
2.避开问题手动下载也行
来,直接点:https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
下完了之后路径导进来
# 从本地路径加载MNIST数据集
local_mnist_path = 'E:/Python/jupyter_PRJ/Deep Learning/data/mnist.npz'
with np.load(local_mnist_path, allow_pickle=True) as data:
x_train, y_train = data['x_train'], data['y_train']
x_test, y_test = data['x_test'], data['y_test']
# 对数据进行归一化处理
x_train, x_test = x_train / 255.0, x_test / 255.0
3.看下导入成功了没
import matplotlib.pyplot as plt
# 查看数据集是否load成功
for i in range(5):
plt.subplot(1, 5, i + 1)
plt.imshow(x_train[i].reshape(28, 28), cmap='gray') # 重塑图像为28x28大小
plt.title("Label: {}".format(y_train[i]))
plt.axis('off') # 不显示坐标轴
plt.show()