【问题标题】:Convolution layer padding difference between pytorch and tensorflowpytorch和tensorflow之间的卷积层填充差异
【发布时间】:2020-11-04 00:58:51
【问题描述】:
torch.nn.Conv2d(7, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))

在上面的pytorch卷积代码行中,padding = (3,3) 参数发生了什么。我们如何在 tensorflow 中实现相同的填充。

tf.keras.layers.Conv2D(7, 64, stride = (2,2), padding="same padding as pytorch")

我们如何在 tensorflow 中实现这一点?

【问题讨论】:

    标签: pytorch conv-neural-network tensorflow2.0


    【解决方案1】:

    填充是在图像的每个维度上添加额外的字节。根据文档,

    padding 控制两边隐式零填充的数量 用于填充每个维度的点数。

    在 TensorFlow 中,您可以使用 tf.pad 来做同样的事情。比如做padding=(3, 3)类似于pytorch,你可以使用下面的代码-

    import tensorflow as tf
    
    image = tf.constant([[1, 2, 3], [4, 5, 6]])
    paddings = tf.constant([[3, 3,], [3, 3]])
    
    image = tf.pad(image, paddings, "CONSTANT")
    print(image)
    

    输出-

    tf.Tensor(
    [[0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0]
     [0 0 0 1 2 3 0 0 0]
     [0 0 0 4 5 6 0 0 0]
     [0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0]
     [0 0 0 0 0 0 0 0 0]], shape=(8, 9), dtype=int32)
    

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2020-12-10
      相关资源
      最近更新 更多