如何解决为什么我们将 Mnist 训练图像重塑为 (60000,28,28,1) 而不是像这样直接使用它 (60,28,28)?
此代码用于使用 Mnist 数据集进行图像分类的训练模型。我不明白的是为什么我们将训练图像重塑为 (60000,28,1) 而不是像这样直接使用它 (60,28)。
num_classes = 10
input_shape = (28,1)
(x_train,y_train),(x_test,y_test) = keras.datasets.mnist.load_data()
#print(x_train[0])
x_train = x_train.astype("float32") / 255
#print(x_train[0])
x_test = x_test.astype("float32") / 255
print(x_train.shape)
print(x_test.shape)
x_train = np.expand_dims(x_train,-1)
x_test = np.expand_dims(x_test,-1)
print("x_train shape:",x_train.shape)
print("x_train shape:",x_test.shape)
print(x_train.shape[0],"train samples")
print(x_test.shape[0],"test samples")
print()
print(y_train)
y_train = keras.utils.to_categorical(y_train,num_classes)
y_test = keras.utils.to_categorical(y_test,num_classes)
print()
print(y_train)
解决方法
在机器学习中理解数据非常重要,就像这个案例。有 60000 张训练图像开始,10000 张图像用于测试。
每张图片大小为 28*28 像素;即 28 像素高和 28 像素宽,因此 (28,28,1),最后一部分的 1 是指定像素的颜色深度。 1 用于灰度图像(黑白图像)。
所以在这里使用 (60,1) 是不可能的。现在为什么我们使用 (60000,1) - 这是我们数据的矩阵形状,因为我们有 60000 张图像,其中 28*28 像素和每个像素在矩阵中都有一个值。
为了简化假设我们只有 1 张图像,那么它就像 (1,1) 并且可以很容易地以矩阵形式写成 28*28 矩阵。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。