简体   繁体   English

pytorch ReplicationPad2d的张量流等效

[英]tensorflow equivalent of pytorch ReplicationPad2d

I'm trying to figure out how to do the tensorflow equivalent of the following padding in pytorch: 我正在尝试找出如何在pytorch中执行以下填充的张量流等效项:

nn.ReplicationPad2d((1, 0, 1, 0))

I've tried the following, but this only seems to work if the input tensor is actually 2x2: 我已经尝试了以下方法,但是这仅在输入张量实际上是2x2时才起作用:

tf.pad(my_tensor, [[1, 0], [1, 0]], "SYMMETRIC")

The equivalent for Tensorflow is tf.pad(my_tensor,[[0,0],[0,0],[1,0],[1,0]],"SYMMETRIC") . Tensorflow的等效项是tf.pad(my_tensor,[[0,0],[0,0],[1,0],[1,0]],"SYMMETRIC") (This assumes that you are interested in operating on 4D tensors, with the first two dimensions being batch and channel). (这假定您有兴趣对4D张量进行操作,前两个维是批处理和通道)。

In Tensorflow, you need to explicitly give the padding for all of the four dimensions. 在Tensorflow中,您需要显式给出所有四个维度的填充。 If you don't want the batch and channel dimensions to be padded (in convolutional networks you typically do not need them padded), you need to explicitly ask for zero padding in both of these dimensions, on both sides of the tensor. 如果您不希望填充批处理和通道尺寸(在卷积网络中通常不需要填充),则需要在张量的两侧显式要求这两个尺寸的零填充。 This is why I added the [0,0],[0,0] before your [1,0],[1,0] . 这就是为什么我在[1,0],[1,0]之前添加[0,0],[0,0] [1,0],[1,0]

In Pytorch, an instance of nn.ReplicationPad2d is already assumed to be padding a 4D tensor, without padding the the first two dimensions. 在Pytorch,的一个实例nn.ReplicationPad2d已经假定为一个填充4D张量,没有填充所述第一两个维度。 That's why you initialize the instance by specifying the padding only in the two additional dimensions. 这就是为什么您仅通过在两个附加维度中指定填充来初始化实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM