import numpy as np
import torch
def corr2d(X, K):
h, w = K.shape
Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
Y[i, j] = (X[i: i + h, j: j + w] * K).sum()
return Y
x = torch.ones([6, 8])
x[:, 2:6] = 0
print(x)
K = torch.tensor([[1, -1]])
crr = torch.nn.Conv2d(1, 1, kernel_size=(1, 2))
print(crr.weight.data)
crr.weight.data = (torch.tensor([[[[1.0, -1.0]]]]))
crr.bias.data.zero_()
print(crr(x.unsqueeze(dim=0).unsqueeze(dim=0)))