Conv2d
CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)
2D 卷积
o u t ( N i , C o u t j ) = b i a s ( C o u t j ) + ∑ k = 0 C i n − 1 W e i g h t ( C o u t j , k ) ∗ i n p u t ( N i , k ) out(N_i, C_{out_j})=bias(C_{out_j})+\sum_{k=0}^{C_{in}-1}Weight(C_{out_j},k)*input(N_i, k) out(Ni,Coutj)=bias(Coutj)+k=0∑Cin−1Weight(Coutj,k)∗input(Ni,k)
N
N
N: 批大小
C
C
C: 通道数
H
H
H: 输入高
W
W
W: 输入宽
- weight ([Tensor]) – 模块可学习的权重,形状为 ( out_chanels \text{out\_chanels} out_chanels, in_channels groups \frac{\text{in\_channels}}{\text{groups}} groupsin_channels, kernel_size[0] \text{kernel\_size[0]} kernel_size[0], kernel_size[1] \text{kernel\_size[1]} kernel_size[1]). 从 U ( − k , k ) \mathcal{U}(-\sqrt{k}, \sqrt{k}) U(−k,k) 中采样,其中 k = g r o u p s C in ∗ ∏ i = 0 1 kernel_size [ i ] k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]} k=Cin∗∏i=01kernel_size[i]groups
- bias ([Tensor] ) – 模块可学习的权重,形状为 (out_channels). 如果
bias
为True
, 从 U ( − k , k ) \mathcal{U}(-\sqrt{k}, \sqrt{k}) U(−k,k) 采样,其中 k = g r o u p s C in ∗ ∏ i = 0 1 kernel_size [ i ] k = \frac{groups}{C_\text{in} * \prod_{i=0}^{1}\text{kernel\_size}[i]} k=Cin∗∏i=01kernel_size[i]groups
- 参数
- in_channels ([int]) - 输入图像的通道数
- out_channels ([int]) - 输出通道数
- kernel_size ([int] or *[tuple]) - 卷积核尺寸
- stride ([int] or [tuple], optional ) - 步长,默认为1
- padding ([int] [tuple] or [str] optional ) - 扩边,默认为0
- padding_mode ([str] optional ) - zeros, reflect, replicate, 默认为 zeros.
- dilation ([int] [tuple] optional ) - 膨胀核,默认为1
- groups ([int] optional ) - 分组,默认为1
- bias ([bool] optional ) - 如果设置为 true, 添加学习的 bias
import torch
import torch.nn as nn
# With square kernels and equal stride
m1 = nn.Conv2d(16, 33, 3, stride=2)
# non-square kernels and unequal stride and with padding
m2 = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
# non-square kernels and unequal stride and with padding and dilation
m3 = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
input = torch.randn(20, 16, 50, 100)
output1 = m1(input) # torch.Size([20, 33, 24, 49])
output2 = m2(input) # torch.Size([20, 33, 28, 100])
output3 = m3(input) # torch.Size([20, 33, 26, 100])
【参考】
Conv2d — PyTorch 1.13 documentation