Tensorflow 查找填充卷积层输入的零的数量

Tensorflow 查找填充卷积层输入的零的数量,tensorflow,padding,convolution,autoencoder,Tensorflow,Padding,Convolution,Autoencoder,我正在使用它们在tensorflow中构建卷积自动编码器。我知道我需要用零填充输入图像,以便从解码器获得与原始输入相等的输出。 作者给出了一个简单的例子,一个正方形内核和相等的步长值(垂直和水平)。我需要为我的输入推广这个填充函数,但是我无法得到张量的正确形状。到目前为止,我的职能是: def _pad(self, input_x, filter_height, filter_width): """ pads input_x with the right amount of ze

我正在使用它们在tensorflow中构建卷积自动编码器。我知道我需要用零填充输入图像,以便从解码器获得与原始输入相等的输出。 作者给出了一个简单的例子,一个正方形内核和相等的步长值(垂直和水平)。我需要为我的输入推广这个填充函数,但是我无法得到张量的正确形状。到目前为止,我的职能是:

def _pad(self, input_x, filter_height, filter_width):
    """
    pads input_x with the right amount of zeros.
    Args:
        input_x: 4-D tensor, [batch_side, widht, height, depth]
        filter_side: used to dynamically determine the padding amount
    Returns:
        input_x padded
    """
    # calculate the padding amount for each side
    top_bottom_padding = filter_height - 1
    left_right_padding = filter_width - 1

    # pad the input on top, bottom, left, right, with amount zeros
    return tf.pad(input_x,
                  [[0, 0], [top_bottom_padding, top_bottom_padding], [left_right_padding, left_right_padding], [0, 0]])
这给了我

Shape of input:  (10, 161, 1800, 1)
Shape of padded input: (10, 187, 1826, 1)
Shape of encoder output:  (10, 187, 913, 15)
Shape of decoder output:  (10, 187, 457, 15)
为了


知道我做错了什么吗

您使用的函数不考虑步幅。实际上,它只是在初始输入时递减1。对于1D情况,知道输入大小i、内核大小k、跨步s和填充p,可以计算卷积的输出大小,如下所示:

这里| |运算符表示天花板操作。了解一维情况的数学,一旦看到每个dim是独立的,n-dim情况就很容易了。因此,只需分别滑动每个维度


查看公式,知道您的
o
应该等于
i
,您可以计算适当的填充

num_outputs=15, kernel_size=14, stride=[1,2]