本文是基于Pytorch框架下的API :Conv2d()。该函数使用在二维输入,另外还有Conv1d()、Conv3d(),其输入分别是一维和三维。下面将介绍Conv2d()的参数。
一、参数介绍
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: _size_2_t,
stride: _size_2_t = 1,
padding: _size_2_t = 0,
dilation: _size_2_t = 1,
groups: int = 1,
bias: bool = True,
padding_mode: str = 'zeros' # TODO: refine this type
):
二、通过调整参数来感受这些参数
1、结果1
import torch
import torch.nn as nn
# 输入是一个N=20,C=16,H=50,W=100的向量
m = nn.Conv2d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output.size())
torch.Size([20, 33, 24, 49])
2、结果2
import torch
import torch.nn as nn
m = nn.Conv2d(16, 33, 3, stride=(1, 2))
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output.size())
-上一步中stride=2表示的是stride=(2,2), 这里添加了stride=(1, 2),表示向右步长为1,向下步长为2,输出结果如下:
torch.Size([20, 33, 48, 49])
3、结果3
import torch
import torch.nn as nn
m = nn.Conv2d(16, 33, (3, 5), stride=2, padding=(4, 2))
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output.size())
torch.Size([20, 33, 28, 50])
三、总结
Conv2d()是卷积神经网络的操作函数,了解函数中的参数是用好CNN的关键。