torch.nn 好像一个齿轮组,整体的运作
torch.nn.functional 就是里面的一个齿轮怎么转
stride=1,每次挪一个位置(横纵都是),stride可以是单个数字或一个元组(sH, sW),横向步径和纵向
如果stride=2,那步径是2,得到2*2的矩阵
[[],[],[],…]表示二维矩阵
padding在给定矩阵左右两侧进行填充,padding=1,在左、右、上、下分别加1列/1行,加的值为0,
reshape(batchsize, 通道,矩阵(几维就几个数字))
代码如下
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]])
kernel = torch.tensor([[1,2,1],
[0,1,0],
[2,1,0]])#卷积核
input = torch.reshape(input, (1,1,5,5))#reshape(batchsize, 通道,矩阵5*5)
kernel = torch.reshape(kernel, (1,1,3,3))
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1) #stride步径
print(output)
output2 = F.conv2d(input, kernel, stride=2)
print(output2)
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)