weight权重,就是就是卷积核
先看所需要的参数
发现input不符合要求,可使用pytorch提供的变换
import torch
import torch.nn.functional as F
input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]])
kernal = torch.tensor([[1, 2, 0],
[0, 1, 0],
[2, 1, 0]])
input = torch.reshape(input, (1, 1, 5, 5))
# input – input tensor of shape (\text{minibatch} , \text{in\_channels} , iH , iW)(minibatch,in_channels,iH,iW)
kernal = torch.reshape(kernal, (1, 1, 3, 3))
print(input.shape)
print(kernal.shape)
output = F.conv2d(input, kernal, stride=1)
print(output)
D:\anaconda\python.exe C:/Users/ASUS/Desktop/tudui/nn_conv.py
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[10, 9, 11],
[16, 13, 15],
[12, 9, 3]]]])
Process finished with exit code 0