torch.nn学习
文章目录
1. 卷积层
1.1 Conv2d
# 原型
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)
二维卷积,输入的tensor
的尺寸为(B,C,H,W)
参数主要有:
- in_channels(int):输入通道数
- out_channels(int):输出通道数
- kernel_size(int or tuple):卷积核大小
- stride(int or tuple,optional):卷积核移动的步长,默认为1
- padding(int or tuple or str,optional):给输入添加0的边距,默认为0
- padding_mode(string, optional):可选参数有zeros、reflect、replicate和circular,默认是zeros
- dilation(int or tuple, optional):卷积核膨胀(可以理解为卷积核每个像素之间的间隔),默认值为1
- groups(int, optional):通道分组数,in_channels和out_channels的公约数,默认值为1
- bias(bool, optional):是否需要可学习的偏差值,默认为True
参数kernel_size
, stride
, padding
,dilation
的取值可以是整数或者元组:
- 整数:在高度和宽度维度应用相同的值。
- 包含两个整数的元组:第一个int应用于高度维度,第二个int应用于宽度维度。
卷积可视化链接:https://github.com/vdumoulin/conv_arithmetic/blob/master/README.md
>>> # With square kernels and equal stride
>>> m = nn.Conv2d(16, 33, 3, stride=2)
>>> # non-square kernels and unequal stride and with padding
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
>>> # non-square kernels and unequal stride and with padding and dilation
>>> m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2), dilation=(3, 1))
>>> input = torch.randn(20, 16, 50, 100)
>>> output = m(input)
2. 池化层
2.1 MaxPool2d
# 原型
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
最大池化,也称下采样,输入的tensor的尺寸为(B,C,H,W)。
主要参数有:
- kernel_size:池化窗口的大小。
- stride:窗口移动的步长,默认和池化窗口大小一样。
- padding:填充0的边距大小。
- dilation:池化窗口膨胀,可以理解为窗口各像素之间相隔的距离。
- return_indices:如果为True,返回最大索引和输出值。
- ceil_mode:如果为True,将使用ceil模式计算输出形状;否则使用floor模式计算。
>>> # pool of square window of size=3, stride=2
>>> m = nn.MaxPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.MaxPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
2.2 MaxUnpool2d
# 原型
torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)
上采样,可以将多个输入大小映射到相同的输出大小。
主要参数有:
- kernel_size:池化窗口的大小。
- stride:窗口移动的步长,默认和池化窗口大小一样。
- padding:填充0的边距大小。
输入有:
- input:需要上采用的输入张量。
- indices:MaxPool2d返回的最大索引下标数组。
- output_size (optional):输出张量的形状。
>>> pool = nn.MaxPool2d(2, stride=2, return_indices=True)
>>> unpool = nn.MaxUnpool2d(2, stride=2)
>>> input = torch.tensor([[[[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.],
[13., 14., 15., 16.]]]])
>>> output, indices = pool(input)
>>> unpool(output, indices)
tensor([[[[ 0., 0., 0., 0.],
[ 0., 6., 0., 8.],
[ 0., 0., 0., 0.],
[ 0., 14., 0., 16.]]]])
>>> # Now using output_size to resolve an ambiguous size for the inverse
>>> input = torch.torch.tensor([[[[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.],
[11., 12., 13., 14., 15.],
[16., 17., 18., 19., 20.]]]])
>>> output, indices = pool(input)
>>> # This call will not work without specifying output_size
>>> unpool(output, indices, output_size=input.size())
tensor([[[[ 0., 0., 0., 0., 0.],
[ 0., 7., 0., 9., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 17., 0., 19., 0.]]]])
2.3 AvgPool2d
# 原型
torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)
平均池化。
主要参数有:
- kernel_size:池化窗口的大小。
- stride:窗口移动的步长,默认和池化窗口大小一样。
- padding:填充0的边距大小。
- ceil_mode:如果为True,将使用ceil模式计算输出形状;否则使用floor模式计算。
- count_include_pad:如果为True,在平均计算中会包含0填充边距。
- divisor_override:如果指定,则为除数,否则使用池化窗口的大小作为除数。
>>> # pool of square window of size=3, stride=2
>>> m = nn.AvgPool2d(3, stride=2)
>>> # pool of non-square window
>>> m = nn.AvgPool2d((3, 2), stride=(2, 1))
>>> input = torch.randn(20, 16, 50, 32)
>>> output = m(input)
3. 代码实践
3.1 Inception Module
代码实现:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Inception(nn.Module):
def __init__(self, in_channles):
super(Inception, self).__init__()
self.cov1x1_1 = nn.Conv2d(in_channles, 16, kernel_size=1)
self.cov1x1_2 = nn.Conv2d(in_channles, 24, kernel_size=1)
self.cov3x3_1 = nn.Conv2d(16, 24, kernel_size=3)
self.cov3x3_2 = nn.Conv2d(24, 24, kernel_size=3)
self.cov5x5 = nn.Conv2d(16, 24, kernel_size=5)
def forward(self, x):
# 第1个分支
branch1 = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
branch1 = self.cov1x1_2(x)
# 第2个分支
branch2 = self.cov1x1_1(x)
# 第3个分支
branch3 = self.cov1x1_1(x)
branch3 = self.cov5x5(branch3)
# 第4个分支
branch4 = self.cov1x1_1(x)
branch4 = self.cov3x3_1(branch4)
branch4 = self.cov3x3_2(branch4)
branchs = [branch1, branch2, branch3, branch4]
return torch.cat(branchs, dim=1)
3.2 Residual Block
代码实现:
import torch
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
def __init__(self, channles):
super(ResidualBlock, self).__init__()
self.conv = nn.Conv2d(channles, channles, kernel_size=3, padding=1)
def forward(self, x):
block = F.relu(self.conv(x))
block = self.conv(block)
return F.relu(block + x)