Why is a 1x1 pytorch convolution changing the data?
I am debugging an issue I have using torch::nn:Conv2d.  Here is a simple script which demonstrates the unexpected behavour
import torch

use_cuda = torch.cuda.is_available()
device = torch.device(&quot;cuda:0&quot; if use_cuda else &quot;cpu&quot;)

t = torch.ones([1,1,3,3]).to(device)

print(t)

kernel_size=[1,1]
t2 = torch.nn.Conv2d(in_channels=1,out_channels=1,kernel_size=kernel_size, stride=kernel_size, padding=0).to(device)(t)

print(t2)

Running this results in:
tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')
tensor([[[[-1.7190, -1.7190, -1.7190],
          [-1.7190, -1.7190, -1.7190],
          [-1.7190, -1.7190, -1.7190]]]], device='cuda:0',
       grad_fn=&lt;AddBackward0&gt;)

Why is t2 different to t1?  I would expect a 1x1 convolution to leave the input unchanged.

Thanks to Michael Szczesny's comment, I replace the conv2d with;
t2 = torch.nn.AvgPool2d(1, stride=1)(t)

And all is well:
tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')
tensor([[[[1., 1., 1.],
          [1., 1., 1.],
          [1., 1., 1.]]]], device='cuda:0')

Basically, I was using the wrong function.
