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