How can I change Tensorflow's 'SAME' padding in Pytorch?

I have Tensorflow code as follow:

kernel_x = tf.reshape(tf.constant([[0, 0], [-1, 1]], tf.float32), [2, 2, 1, 1])
gradient_orig = tf.nn.conv2d(input_tensor, kernel_x, strides=[1, 1, 1, 1], padding='SAME')

I’ll change this code for Pytorch but I have some trouble.

import torch
import torch.nn.functional as F

kernel_x = torch.tensor([[0, 0], [-1, 1]], dtype=torch.float32).view(1, 1, 2, 2)
gradient_orig = F.conv2d(input_tensor, kernel_x, stride=1, padding= ??? )

how can I do the same operation with Tensorflow’s ‘SAME’ padding in this setting?

Did you try to pass padding="same"?

I tried, but I think I got the different results.

import tensorflow as tf

kernel = tf.reshape(tf.constant([[0, 0], [-1, 1]], tf.float32), [2, 2, 1, 1])
input_tensor = tf.reshape(tf.constant([[1,1],[1, 2]], tf.float32), [2, 2, 1, 1])
gradient_orig = tf.nn.conv2d(input_tensor, kernel, strides=[1, 1, 1, 1], padding='SAME')
print(gradient_orig)
tf.Tensor(
[[[[-1.]]

  [[ 0.]]]


 [[[-2.]]

  [[ 0.]]]], shape=(2, 2, 1, 1), dtype=float32)
import torch
import torch.nn.functional as F

kernel_x = torch.tensor([[0, 0], [-1, 1]], dtype=torch.float32).view(1, 1, 2, 2)
input_tensor = torch.tensor([[1,1],[1,2]], dtype=torch.float32).view(1,1,2,2)
gradient_orig = F.conv2d(input_tensor, kernel_x, stride=1, padding="same")
print(gradient_orig)
tensor([[[[ 1., -2.],
          [ 0.,  0.]]]])

I implement both simple code for the test, but results seem different.
and I got Warning message :
“UserWarning: Using padding=‘same’ with even kernel lengths and odd dilation may require a zero-padded copy of the input be created (Triggered internally at …\aten\src\ATen\native\Convolution.cpp:647.)”

I’m sorry that I don’t know about tensorflow’s tensor shape… maybe the difficulty came from this problem

I think you might be mixing shapes, as e.g. in TF it seems your input has 2 samples.
From the TF/Keras docs:

Input shape
4+D tensor with shape: batch_shape + (channels, rows, cols) if data_format='channels_first' or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format='channels_last'.

In both memory formats the first dimension should represent the batch dimension, which is set to 2 in your example while it’s 1 in PyTorch.