conv2d function in pytorch
To get equivalent results :
im_torch = im.reshape((1, 1, 3, 3))
filt_torch = filt.reshape((1, 1, 3, 3))

out = torch.nn.functional.conv2d(filt_torch, im_torch, stride=1, padding=0)
print(out)


import scipy.signal


out_scipy = scipy.signal.convolve2d(im.detach().numpy(), filt.detach().numpy()[::-1, ::-1], 'valid')
print(out_scipy)

There was a shaping issue (https://pytorch.org/docs/stable/nn.functional.html) and the application of the filter in torch differs a bit with how it is handled in scipy : https://discuss.pytorch.org/t/functional-conv2d-produces-different-results-vs-scipy-convolved2d/17762