nn.Conv2d卷积

1、二维卷积可以处理二维数据

 nn.Conv2d(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True))

 参数:
        in_channel: 输入数据的通道数,例RGB图片通道数为3;
        out_channel: 输出数据的通道数,这个根据模型调整;
        kennel_size: 卷积核大小,可以是int,或tuple;kennel_size=2,意味着卷积大小(2,2),         kennel_size=(2,3),意味着卷积大小(2,3)即非正方形卷积
        stride:步长,默认为1,与kennel_size类似,stride=2,意味着步长上下左右扫描皆为2, stride=(2,3),左右扫描步长为2,上下为3;
        padding: 零填充

2、例子

 输入数据X[10,16,30,32],其分别代表:10组数据,通道数为16,高度为30,宽为32

import torch
import torch.nn as nn

x = torch.randn(10, 16, 30, 32) # batch, channel , height , width
print(x.shape)
m = nn.Conv2d(16, 33, (3, 2), (2,1))  # in_channel, out_channel ,kennel_size,stride
print(m)
y = m(x)
print(y.shape)

 结果:

torch.Size([10, 16, 30, 32])
Conv2d(16, 33, kernel_size=(3, 2), stride=(2, 1))
torch.Size([10, 33, 14, 31])

3.卷积计算过程:
h/w = (h/w - kennel_size + 2padding) / stride + 1
x = ([10,16,30,32]),其中h=30,w=32,对于卷积核长分别是 h:3,w:2 ;对于步长分别是h:2,w:1;padding默认0;
h = (30 - 3 + 20)/ 2 +1 = 27/2 +1 = 13+1 =14
w =(32 - 2 + 2*0)/ 1 +1 = 30/1 +1 = 30+1 =31
batch = 10, out_channel = 33
故: y= ([10, 33, 14, 31])

k + (r-1) *(k-1) 

你可能感兴趣的:(Python,cnn,深度学习,pytorch)