Tensorflow vs PyTorch: convolution doesn't work
I'm trying to test if Tensorflow convolution output matches PyTorch convolution output with the same weights.

Here's my code in which I copy the weights from Tensorflow to Torch, convolve and compare outputs:

import tensorflow as tf
import numpy as np
import math
from math import floor, ceil
import os
import math
import datetime
from scipy import misc
from PIL import Image
import model
import torch
from torch import nn
import common
import torch.nn.functional as F


sess = tf.Session()
np.random.seed(1)
tf.set_random_seed(1)

#parameters
kernel_size = 3
input_feat = 4
output_feat = 4

#inputs
npo = np.random.random((1,5,5, input_feat))
x = tf.convert_to_tensor(npo, tf.float32)
x2 = torch.tensor(np.transpose(npo, [0, 3, 1, 2])).double()

#the same weights
weights = np.random.random((kernel_size,kernel_size,input_feat,output_feat))
weights_torch = np.transpose(weights, [3, 2, 1, 0])

#convolving with tensorflow
w = tf.Variable(weights, name="testconv_W", dtype=tf.float32)
res = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding="VALID")

sess.run(tf.global_variables_initializer())


#convolving with torch
torchres = F.conv2d(x2, torch.tensor(weights_torch), padding=0, bias=torch.zeros((output_feat)).double())

#comparing the results
print(np.mean(np.transpose(sess.run(res), [0, 3, 1, 2])) - torch.mean(torchres).detach().numpy())


It outputs

0.15440369065716908


Why? Why is there such a big difference? Is the Tensorflow conv2d implementation incorrect? Why doesn't it match PyTorch? Am I doing something wrong? On kernel size 1 everything works fine.
Please help.

You might try x2 = torch.tensor(np.transpose(npo, [0, 3, 2, 1])).double()
instead of x2 = torch.tensor(np.transpose(npo, [0, 3, 1, 2])).double()
