在卷积神经网络中实施填充是一件十分重要的事情,如果没有填充,边缘区域的像素值基本不会受到卷积层的影响。
那么,如何写填充代码:
# if you want to pad the array "a" of shape (5,5,5,5,5)(5,5,5,5,5) with pad = 1 for the 2nd dimension,
# pad = 3 for the 4th dimension and pad = 0 for the rest, you would do
a = np.pad(a, ((0,0), (1,1), (0,0), (3,3), (0,0)), 'constant', constant_values = (..,..))
具体详细讲解,可见:http://www.bubuko.com/infodetail-2448104.html
https://blog.csdn.net/AbstractSky/article/details/76769202
比如说
a = [[1, 2], [3, 4],[5,6]]
a = np.pad(a, ((1, 3), (3, 1)), 'constant')
print(a)
表示的就是对于矩阵A,前面填充一行,后面填充三行(1,3)。左边填充三行,右边填充一行(3,1)。输出结果入下:
[[0 0 0 0 0 0]
[0 0 0 1 2 0]
[0 0 0 3 4 0]
[0 0 0 5 6 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]]
对于更高纬度的我们表示不出来,不过填充效果基本上是类似的
x=np.arange(8).reshape(2,2,2)
k = np.pad(x,pad_width=((2,3),(3,3),(2,2)),mode='constant',constant_values=((1,2),(2,3),(2,2)))
print(x.shape)
print(k.shape)
(2, 2, 2)
(7, 8, 6)
结果的解释,首先我们x.shape为(2, 2, 2)应该没有疑问,然后我们看pad_width,它里面每一个元组表示对应的维度左右两边填充的数量,第几个元组就代表第几维,constant_values也就是对应,每一个维度左右两边各填充的值,所以比如第一维度,开始是2,因为左右两边各填充2和3,加起来相当于扩展了5个,开始是2,所以填充以后就是7了,第二维度开始是2,增加了6就是8了,第三维度增加了4,就是6了,然后各个维度对应左右两边填充的值由后面的constant_values决定。