TL; DR Use the convolution from the torch.nn.fuctional.conv2d function toolbar, not torch.nn.conv2d and flip the filter around the vertical and horizontal axis.
torch.nn.conv2d is a convolutional layer for the network. Since the weights are studied, it doesnβt matter if it is implemented using cross-correlation, because the network will simply learn the mirror version of the kernel (thanks @etarion for this clarification).
torch.nn.fuctional.conv2d does a convolution with the inputs and weights provided as arguments, similar to the tensorflow function in your example. I wrote a simple test to determine if the tensor flow function actually performs cross-correlation, and you need to flip the filter for correct convolutional results.
import torch import torch.nn.functional as F import torch.autograd as autograd import numpy as np
Displays
Variable containing: (0 ,0 ,.,.) = 0 0 0 0 0 0 0 1 0 0 -1 0 0 1 0 0 -1 0 0 1 0 0 -1 0 0 0 0 0 0 0 [torch.FloatTensor of size 1x1x5x6]
This result is the result of cross-correlation. So we need to flip the filter
def flip_tensor(t): flipped = t.numpy().copy() for i in range(len(filters.size())): flipped = np.flip(flipped,i)
The new conclusion is the correct result for the convolution.
Variable containing: (0 ,0 ,.,.) = 0 0 0 0 0 0 0 -1 0 0 1 0 0 -1 0 0 1 0 0 -1 0 0 1 0 0 0 0 0 0 0 [torch.FloatTensor of size 1x1x5x6]
source share