在tf.nn中“SAME”和“VALID”填充之间的区别是什么?tensorflow的Max_pool ?

在我看来,'VALID'意味着当我们做max pool时,边缘外不会有零填充。

根据深度学习卷积算法指南,它说池操作符中不会有填充,即只使用tensorflow的“VALID”。 但什么是'SAME'填充的最大池张量流量?


当前回答

填充是一种增加输入数据大小的操作。在一维数据中,你只需要在数组前加上一个常数,在2-dim中,你用这些常数包围矩阵。在n-dim中,用常数包围n-dim超立方体。在大多数情况下,这个常数是零,它被称为零填充。

下面是一个应用于2-d张量的p=1的零填充的例子:


你可以为你的内核使用任意填充,但是有些填充值比其他填充值使用得更频繁:

有效的填充。最简单的情况,意味着根本没有填充。让你的数据保持原样。 相同填充有时称为半填充。之所以称为SAME,是因为对于stride=1的卷积(或池化),它应该产生与输入相同大小的输出。之所以叫HALF是因为对于一个大小为k的核 FULL填充是最大填充,它不会导致对刚刚填充的元素进行卷积。对于一个大小为k的核,这个填充值等于k - 1。


要在TF中使用任意填充,可以使用TF .pad()

其他回答

我引用了官方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()

Padding on/off. Determines the effective size of your input. VALID: No padding. Convolution etc. ops are only performed at locations that are "valid", i.e. not too close to the borders of your tensor. With a kernel of 3x3 and image of 10x10, you would be performing convolution on the 8x8 area inside the borders. SAME: Padding is provided. Whenever your operation references a neighborhood (no matter how big), zero values are provided when that neighborhood extends outside the original tensor to allow that operation to work also on border values. With a kernel of 3x3 and image of 10x10, you would be performing convolution on the full 10x10 area.

快速的解释

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.

如果你喜欢ascii艺术:

"VALID" = without padding: inputs: 1 2 3 4 5 6 7 8 9 10 11 (12 13) |________________| dropped |_________________| "SAME" = with zero padding: pad| |pad inputs: 0 |1 2 3 4 5 6 7 8 9 10 11 12 13|0 0 |________________| |_________________| |________________|

在这个例子中:

输入宽度= 13 滤镜宽度= 6 步幅= 5

注:

"VALID"只删除最右边的列(或最底部的行)。 “SAME”尝试均匀地左右填充,但如果要添加的列的数量是奇数,它会将额外的列添加到右侧,就像本例中的情况一样(垂直方向上的逻辑相同:底部可能有额外的一行零)。

编辑:

关于名字:

对于“SAME”填充,如果你使用的步幅为1,该层的输出将具有与其输入相同的空间维度。 使用“VALID”填充,就没有“编造”填充输入。该层只使用有效的输入数据。