在tf.nn中“SAME”和“VALID”填充之间的区别是什么?tensorflow的Max_pool ?
在我看来,'VALID'意味着当我们做max pool时,边缘外不会有零填充。
根据深度学习卷积算法指南,它说池操作符中不会有填充,即只使用tensorflow的“VALID”。 但什么是'SAME'填充的最大池张量流量?
在tf.nn中“SAME”和“VALID”填充之间的区别是什么?tensorflow的Max_pool ?
在我看来,'VALID'意味着当我们做max pool时,边缘外不会有零填充。
根据深度学习卷积算法指南,它说池操作符中不会有填充,即只使用tensorflow的“VALID”。 但什么是'SAME'填充的最大池张量流量?
当前回答
我引用了官方tensorflow文档https://www.tensorflow.org/api_guides/python/nn#Convolution中的答案 对于'SAME'填充,输出高度和宽度的计算如下:
out_height = ceil(float(in_height) / float(strides[1]))
out_width = ceil(float(in_width) / float(strides[2]))
顶部和左侧的填充被计算为:
pad_along_height = max((out_height - 1) * strides[1] +
filter_height - in_height, 0)
pad_along_width = max((out_width - 1) * strides[2] +
filter_width - in_width, 0)
pad_top = pad_along_height // 2
pad_bottom = pad_along_height - pad_top
pad_left = pad_along_width // 2
pad_right = pad_along_width - pad_left
对于'VALID'填充,输出高度和宽度的计算如下:
out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))
填充值总是0。
其他回答
填充是一种增加输入数据大小的操作。在一维数据中,你只需要在数组前加上一个常数,在2-dim中,你用这些常数包围矩阵。在n-dim中,用常数包围n-dim超立方体。在大多数情况下,这个常数是零,它被称为零填充。
下面是一个应用于2-d张量的p=1的零填充的例子:
你可以为你的内核使用任意填充,但是有些填充值比其他填充值使用得更频繁:
有效的填充。最简单的情况,意味着根本没有填充。让你的数据保持原样。 相同填充有时称为半填充。之所以称为SAME,是因为对于stride=1的卷积(或池化),它应该产生与输入相同大小的输出。之所以叫HALF是因为对于一个大小为k的核 FULL填充是最大填充,它不会导致对刚刚填充的元素进行卷积。对于一个大小为k的核,这个填充值等于k - 1。
要在TF中使用任意填充,可以使用TF .pad()
我举个例子来说明:
X:输入形状[2,3]的图像,1通道 valid_pad: max pool with 2x2 kernel, stride 2和VALID padding。 same_pad: max pool with 2x2 kernel, stride 2和SAME padding(这是经典的方法)
输出形状为:
Valid_pad:这里没有填充,所以输出形状是[1,1] Same_pad:在这里,我们将图像填充到形状[2,4](使用-inf,然后应用Max pool),因此输出形状是[1,2]
x = tf.constant([[1., 2., 3.],
[4., 5., 6.]])
x = tf.reshape(x, [1, 2, 3, 1]) # give a shape accepted by tf.nn.max_pool
valid_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='VALID')
same_pad = tf.nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')
valid_pad.get_shape() == [1, 1, 1, 1] # valid_pad is [5.]
same_pad.get_shape() == [1, 1, 2, 1] # same_pad is [5., 6.]
为了补充YvesgereY的回答,我发现这个可视化非常有用:
填充'valid'是第一个数字。滤镜窗口停留在图像内部。
填充'same'是第三个数字。输出是相同的大小。
在这篇文章里找到的
可视化致谢:vdumoulin@GitHub
快速的解释
VALID:不要应用任何填充,也就是说,假设所有的维度都是有效的,这样输入的图像就会被你指定的过滤器和stride完全覆盖。
SAME:应用填充到输入(如果需要),以便输入图像被过滤器和步幅完全覆盖。对于stride 1,这将确保输出图像大小与输入相同。
笔记
This applies to conv layers as well as max pool layers in same way The term "valid" is bit of a misnomer because things don't become "invalid" if you drop part of the image. Sometime you might even want that. This should have probably be called NO_PADDING instead. The term "same" is a misnomer too because it only makes sense for stride of 1 when output dimension is same as input dimension. For stride of 2, output dimensions will be half, for example. This should have probably be called AUTO_PADDING instead. In SAME (i.e. auto-pad mode), Tensorflow will try to spread padding evenly on both left and right. In VALID (i.e. no padding mode), Tensorflow will drop right and/or bottom cells if your filter and stride doesn't full cover input image.
总之,“有效”填充意味着没有填充。卷积层的输出大小根据输入大小和内核大小而缩小。
相反,“相同”填充意味着使用填充。当stride设置为1时,卷积层的输出大小保持为输入大小,在计算卷积时在输入数据周围附加一定数量的“0-border”。
希望这个直观的描述能有所帮助。