简体   繁体   中英

tensorflow equivalent of pytorch ReplicationPad2d

I'm trying to figure out how to do the tensorflow equivalent of the following padding in 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:

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") . (This assumes that you are interested in operating on 4D tensors, with the first two dimensions being batch and channel).

In Tensorflow, you need to explicitly give the padding for all of the four dimensions. 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] .

In Pytorch, an instance of nn.ReplicationPad2d is already assumed to be padding a 4D tensor, without padding the the first two dimensions. That's why you initialize the instance by specifying the padding only in the two additional dimensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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