pytorch官方链接:https://pytorch.org/docs/stable/generated/torch.nn.functional.conv2d.html?highlight=f%20conv2d#torch.nn.functional.conv2d
import torch
import torch.nn.functional as F
if __name__ == '__main__':
inputs = torch.tensor([[[[7, 8], [6, 4]]]])
filters = torch.tensor([[[[1, 2], [2, 3]]]])
print(inputs.shape)
print(filters.shape)
ans = F.conv2d(inputs, filters, padding=0, stride=1)
print(ans)
print("")
inputs = torch.tensor([[[[7, 8], [6, 4]], [[7, 8], [6, 4]]]])
filters = torch.tensor([[[[1, 2], [2, 3]], [[1, 2], [2, 3]]]])
print(inputs.shape)
print(filters.shape)
ans = F.conv2d(inputs, filters, padding=0, stride=1, groups=1)
print(ans)
print("")
inputs = torch.tensor([[[[7, 8], [6, 4]], [[7, 8], [6, 4]]]])
filters = torch.tensor([[[[1, 2], [2, 3]]], [[[2, 3], [4, 5]]]])
print(inputs.shape)
print(filters.shape)
ans = F.conv2d(inputs, filters, padding=0, stride=1, groups=2)
print(ans)
print("")
inputs = torch.tensor([[[[7, 8], [6, 4]], [[7, 8], [6, 4]]]])
filters = torch.tensor([[[[1, 2], [2, 3]]], [[[2, 3], [4, 5]]], [[[2, 4], [4, 6]]], [[[4, 6], [8, 10]]]])
print(inputs.shape)
print(filters.shape)
ans = F.conv2d(inputs, filters, padding=0, stride=1, groups=2)
print(ans)
print("")
inputs = torch.tensor([[[[7, 8], [6, 4]], [[7, 8], [6, 4]]], [[[7, 8], [6, 4]], [[7, 8], [6, 4]]]])
filters = torch.tensor([[[[1, 2], [2, 3]]], [[[2, 3], [4, 5]]]])
print(inputs.shape)
print(filters.shape)
ans = F.conv2d(inputs, filters, padding=0, stride=1, groups=2)
print(ans)
print("")
torch.Size([1, 1, 2, 2])
torch.Size([1, 1, 2, 2])
tensor([[[[47]]]])
torch.Size([1, 2, 2, 2])
torch.Size([1, 2, 2, 2])
tensor([[[[94]]]])
torch.Size([1, 2, 2, 2])
torch.Size([2, 1, 2, 2])
tensor([[[[47]],
[[82]]]])
torch.Size([1, 2, 2, 2])
torch.Size([4, 1, 2, 2])
tensor([[[[ 47]],
[[ 82]],
[[ 94]],
[[164]]]])
torch.Size([2, 2, 2, 2])
torch.Size([2, 1, 2, 2])
tensor([[[[47]],
[[82]]],
[[[47]],
[[82]]]])