在 mnist 上填充 LeNet 架构

数据挖掘 深度学习 麻木的 极简主义
2022-02-23 13:13:00

LeNet 接受 32X32 图像。因此,要将 LeNet 用于 MNIST 数据集,我们必须将大小从 28X28 更改为 32X32。我遇到了这个实现。我对以下代码行的工作方式感到困惑。

np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')

上面一行代码板 28X28 像素图像变成 32X32 图像。任何人都可以帮助我了解它是如何完成的。

1个回答

基本上,它完全按照您指定的方式执行。使用的numpy 函数在每个维度中附加值。给定数据集的维度,每个轴上的“焊盘”数量由 指定((0,0),(2,2),(2,2),(0,0)),即:

10000 (samples) x 28 (image dimension 1) x 28 (image dim. 2) x 1 (grayscale value!)

现在让我们看看您的规范在这方面意味着什么:

(0,0)Pad 0(as in the amount) values before and after each row
(2,2)Pad 2 before and 2 after each value of dim. 1 of your image data: 28 values -> 32
(2,2)Pad 2 before and 2 after each value of dim. 2 of your image data: 28 values -> 32
(0,0)Pad, again, nothing in the grayscale value dimension

这意味着您最终将获得相应维度的 32x32 图像。现在,唯一剩下的就是:我们填充哪些值?答案很简单,您不指定任何constant_values,这意味着它将使用默认值constant_values(在上面的链接页面上指定)填充。即这个值为0。

总结一下,简单地想象你有一个 32x32 的图像,你的 28x28 在中间,在外面你有一个 0 的 2 值粗边框。