【pytorch】nn.conv1d的使用

官方文档在这里

conv1d具体不做介绍了,本篇只做pytorch的API使用介绍.

torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=‘zeros’, device=None, dtype=None)

计算公式

输入张量的Shape一般为 ( N , C i n , L ) (N,C_{in}, L) (N,Cin,L),其中N为batch_size,一般也可用B代替;
C i n C_{in} Cin输入张量倒数第二维,表示Channel的数量;
L是输入信号序列的长度;

输出张量的shape一般为 ( N , C o u t , L o u t ) (N,C_{out},L_{out}) (N,Cout,Lout), 下一节会介绍怎么计算来的。

公式截图如下,
公式
星号表示卷积运算,torch的运算这里使用的是cross-correlation,即互相关运算

运算过程

假设输入的序列为 [1,5,7,3,2,1,6,9], 卷积核为[2,4,6,1,3]

那么对应nn.conv1d初始化就是 in_channel=1, out_channel=1, stride=1, padding=0

计算过程中,每计算一次(kernel_size),移动一步(stride),
这个过程就是上图公式中的 w e i g h t ( C o u t , k ) ∗ i n p u t ( N i , k ) weight(C_{out},k) * input(N_i,k) weight(Cout,k)input(Ni,k),其中k是out_channel的,
如果out_channel个数大于1,则这样的过程会有多次。
运算过程

入参

  • in_channels
    Number of channels in the input image
    输入的Channel数,对应的是输入数据的倒数第二维
  • out_channels
    Number of channels produced by the convolution
    输出的Channel数,对应输出数据的倒数第二维度
  • kernel_size
    Size of the convolving kernel
    即卷积核长度
    它可以是一个数字也可以是一个tuple(但是conv1d下,tuple是否有意义?
  • stride
    Stride of the convolution. Default: 1
    卷积核步长
  • padding
    Padding added to both sides of the input. Default: 0
  • padding_mode
    ‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’
  • dilation
    Spacing between kernel elements. Default: 1
  • groups
    Number of blocked connections from input channels to output channels. Default: 1
  • bias
    If True, adds a learnable bias to the output. Default: True
    是否需要bias

输出结果的shape计算

计算output的shape

代码举例

1

net = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=4, stride=1, bias=False)
x = torch.linspace(1,10,10).view(1,1,10)

y = net(x)
print(y.shape)

计算结果

torch.Size([1, 8, 7])

2. 修改stride

net = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=4, stride=2, bias=False)
x = torch.linspace(1,10,10).view(1,1,10)

y = net(x)
print(y.shape)

计算结果

torch.Size([1, 8, 4])

3 添加padding

net = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=4, stride=1, padding=1, padding_mode='zeros',bias=False)
x = torch.linspace(1,10,10).view(1,1,10)

y = net(x)
print(y.shape)

计算结果

torch.Size([1, 8, 5])

4 修改dilation

net = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=4, stride=2, dilation=2,bias=False)
x = torch.linspace(1,10,10).view(1,1,10)

y = net(x)
print(y.shape)

计算结果

torch.Size([1, 8, 2])
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值