Pytorch – torch.nn.Conv2d参数详解与使用
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Pytorch – torch.nn.Conv2d参数详解与使用
原文链接:https://www.stubbornhuang.com/2436/
发布于:2022年12月07日 13:10:14
修改于:2022年12月07日 13:23:17

1 torch.nn.Conv2d
torch.nn.Conv2d
主要对输入Tensor应用2D卷积。
比如输入(N,C_{in},H,W)维度的Tensor,则输出(N,C_{out},H,W)的Tensor,这两者的关系可以描述为
其中,\star为2D cross-correlation操作,N为batch size,C为channels,H为高,W为宽。
1.1 torch.nn.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)
参数
- in_channels(int):输入的特征维度
- out_channels(int):输出的特征维度
- kernel_size(int or tuple):卷积核大小
- stride(int or tuple):卷积的步幅,默认值为1
- padding(int or tuple):添加到输入两侧的零填充数量,默认值为0
- dilation(int or tuple):内核元素之间的间距,默认值为1
- groups(int):从输入通道到输出通道的阻塞连接数
- bias(bool):默认值为True,如果为True,则向输出添加可学习的偏差
- padding_mode(str):可选值为"zeros"、"reflect"、"replicate"、“circular”,默认值为"zeros"
输入与输出维度
一般,输入与输出tensor具有以下维度:
- input:(N,C_{in},H_{in},W_{in})或者(C_{in},H_{in},W_{in})
- output:(N,C_{out},H_{out},W_{out})或者(C_{out},H_{out},W_{out})
其中,
1.2 torch.nn.Conv2d的简单使用
假设有batch_size为10,in_channels特征维度为256,宽高都为224的输入tensor,使用卷积核大小为3,卷积步幅为1的二维卷积层对输入tensor进行卷积,
对应的pytorch代码如下:
import torch
if __name__ == '__main__':
batch_size = 10
in_channels = 256
h = 224
w = 224
out_channels = 512
input = torch.randn(size=(batch_size, in_channels, h, w))
conv2d = torch.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3)
out = conv2d(input)
print(out.shape)
输出
torch.Size([10, 512, 222, 222])
当前分类随机文章推荐
- Pytorch - reshape和view的用法和区别 阅读211次,点赞0次
- Pytorch - 用Pytorch实现ResNet 阅读348次,点赞0次
- Pytorch - torch.nn.Conv1d参数详解与使用 阅读1691次,点赞0次
- Pytorch - 内置的CTC损失函数torch.nn.CTCLoss参数详解与使用示例 阅读967次,点赞1次
- Pytorch - 梯度累积/梯度累加trick,在显存有限的情况下使用更大batch_size训练模型 阅读246次,点赞0次
- Pytorch - 模型保存与加载以及如何在已保存的模型的基础上继续训练模型 阅读431次,点赞0次
- Pytorch - torch.unsqueeze和torch.squeeze函数 阅读233次,点赞0次
- Pytorch - 使用torch.matmul()替换torch.einsum('nctw,cd->ndtw',(a,b))算子模式 阅读1759次,点赞0次
- Pytorch - .to()和.cuda()的区别 阅读611次,点赞0次
- Pytorch - torch.cat参数详解与使用 阅读1166次,点赞1次
全站随机文章推荐
- 资源分享 - Jim Blinn‘s Corner - Dirty Pixels 英文高清PDF下载 阅读1919次,点赞1次
- 资源分享 - Polygon Mesh Processing英文高清PDF下载 阅读6981次,点赞1次
- 资源分享 - Computer Graphics, C Version , Second Edition 英文高清PDF下载 阅读951次,点赞0次
- 工具网站推荐 - PhotoShop、Adobe Premiere、Autodesk AutoCAD、Adobe After Effects等生产力软件下载 阅读460次,点赞0次
- TensorRT - Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors 阅读73次,点赞0次
- C++ - websocket++库的可使用的所有事件总结 阅读85次,点赞0次
- 资源分享 - Digital Lighting & Rendering , Third Edition 英文高清PDF下载 阅读1280次,点赞0次
- 资源分享 - Physically Based Rendering From Theory To Implementation (Second Edition)英文高清PDF下载 阅读2613次,点赞2次
- 资源分享 - Computational Geometry - Algorithms and Applications, Second Edition 英文高清PDF下载 阅读1638次,点赞0次
- C++ - sleep睡眠函数总结 阅读925次,点赞0次
评论
167