Conv1d卷积、Conv2d卷积、Conv3d卷积详解

卷积操作中的dilation的理解:

https://blog.csdn.net/weixin_42363544/article/details/123920699
空洞卷积,膨胀卷积

在Pytorch中,dilation = 1等同于没有dilation的标准卷积。
当dilation为1时,卷积核中的每个元素都紧挨着;
而当dilation为2时,卷积核中的每个元素之间都会间隔一个元素。
通过调整dilation的大小,可以控制卷积操作提取特征的范围和感受野的大小,从而影响网络的性能和特征提取能力。

Conv1d卷积

Conv1d卷积的Pytorch官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html

import torch.nn as nn
import torch
m = nn.Conv1d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50)
print(input.shape)
output = m(input)
print(output.shape)

输出结果:
在这里插入图片描述
首先,让我们来理解一下这个1D卷积操作的参数和输入。

卷积层 nn.Conv1d(16, 33, 3, stride=2) 的参数解释如下:

输入通道数:16
输出通道数:33
卷积核大小:3
步长(stride):2
输入张量 input 的形状是 (20, 16, 50),这表示有20个样本,每个样本有16个通道,每个通道的长度是50。

卷积操作后,输出张量的形状可以通过以下方式计算:

输出长度 = (输入长度 - 卷积核大小) // 步长 + 1
在这里,输入长度是50,卷积核大小是3,步长是2,所以输出长度 = (50 - 3) // 2 + 1 = 24
因此,输出张量的形状将是 (样本数, 输出通道数, 输出长度),即 (20, 33, 24)。

在这里插入图片描述
结合上图的公式,dilation默认是1

Conv2d卷积

Pytorch官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html

import torch.nn as nn
import torch
# With square kernels and equal stride
m = nn.Conv2d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 50, 100)
print("1:")
print(input.shape)
output = m(input)
print(output.shape)

# non-square kernels and unequal stride and with padding
m = nn.Conv2d(16, 33, (3, 5), stride=(2, 1), padding=(4, 2))
input = torch.randn(20, 16, 50, 100)
print("2:")
print(input.shape)
output = m(input)
print(output.shape)

# 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)
print("3:")
print(input.shape)
output = m(input)
print(output.shape)

在这里插入图片描述

输出结果:
在这里插入图片描述

计算过程如下:
在这里插入图片描述

在这里插入图片描述

Conv3d卷积

官方文档:
https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html


import torch.nn as nn
import torch

# With square kernels and equal stride
m = nn.Conv3d(16, 33, 3, stride=2)
input = torch.randn(20, 16, 10, 50, 100)
print("1:")
print(input.shape)
output = m(input)
print(output.shape)


# non-square kernels and unequal stride and with padding
m = nn.Conv3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(4, 2, 0))
input = torch.randn(20, 16, 10, 50, 100)
print("2:")
print(input.shape)
output = m(input)
print(output.shape)

在这里插入图片描述

输出结果:
在这里插入图片描述

计算过程:
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LinlyZhai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值