核大小 填充(通常设置为核减一) 步幅(通常情况步幅为1最好,计算量太大的话,步幅取2)
卷积核的边长一般取奇数:上下填充对称
resnet 经典网络
通道数(超参数)
输出通道数是卷积层的超参数
每个输入通道有独立的二维卷积核,所有通道结果相加得到一个输出通道结果
每个输出通道有独立的三维卷积核
1*1卷积层
多输入通道
实现多输入用到互相关运算
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.22.0
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] on win32
import torch
from d2l import torch as d2l
def corr2d_multi_in(X, K):
# 先遍历 “X” 和 “K” 的第0个维度(通道维度),再把它们加在一起
return sum(d2l.corr2d(x, k) for x, k in zip(X, K))
X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])
K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])
corr2d_multi_in(X, K)
Out[3]:
tensor([[ 56., 72.],
[104., 120.]])
多输出通道
实现一个计算多个通道的输出的互相关函数
def corr2d_multi_in_out(X, K):
# 迭代“K”的第0个维度,每次都对输入“X”执行互相关运算。
# 最后将所有结果都叠加在一起
return torch.stack([corr2d_multi_in(X, k) for k in K], 0)
"""torch.stack是堆叠操作"""
Out[5]: 'torch.stack是堆叠操作'
K = torch.stack((K, K + 1, K + 2), 0)
K.shape
Out[6]: torch.Size([3, 2, 2, 2])
corr2d_multi_in_out(X, K)
Out[7]:
tensor([[[ 56., 72.],
[104., 120.]],
1×1 卷积层
def corr2d_multi_in_out_1x1(X, K):
c_i, h, w = X.shape
c_o = K.shape[0]
X = X.reshape((c_i, h * w))
K = K.reshape((c_o, c_i))
# 全连接层中的矩阵乘法
Y = torch.matmul(K, X)
return Y.reshape((c_o, h, w))
X = torch.normal(0, 1, (3, 3, 3)) #输入通道是3
K = torch.normal(0, 1, (2, 3, 1, 1)) #输出通道是2
X
Out[10]:
tensor([[[-1.3284, 0.8308, 0.2190],
[-0.6506, -0.7553, -0.1650],
[-0.5727, -0.8305, -0.9563]],
[[ 0.4634, 0.1110, 0.6811],
[-0.9413, 2.1331, 0.0488],
[ 0.3226, -0.2758, -1.1776]],
[[-1.4714, -0.9758, -0.8739],
[ 0.7497, 1.0012, -0.3699],
[-0.3726, 1.4853, 0.3012]]])
K
Out[11]:
tensor([[[[ 0.4743]],
[[-1.1153]],
[[-1.1156]]],
[[[-0.5471]],
[[-0.5898]],
[[-1.6157]]]])
Y1 = corr2d_multi_in_out_1x1(X, K)
Y2 = corr2d_multi_in_out(X, K)
assert float(torch.abs(Y1 - Y2).sum()) < 1e-6