iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pytorch中的torch.nn.Conv2d()函数图文详解
  • 616
分享到

pytorch中的torch.nn.Conv2d()函数图文详解

2024-04-02 19:04:59 616人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录一、官方文档介绍二、torch.nn.Conv2d()函数详解参数dilation——扩张卷积(也叫空洞卷积)参数groups——

一、官方文档介绍

官网

nn.Conv2d:对由多个输入平面组成的输入信号进行二维卷积

二、torch.nn.Conv2d()函数详解

参数详解

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

参数参数类型  
in_channelsintNumber of channels in the input image输入图像通道数
out_channelsintNumber of channels produced by the convolution卷积产生的通道数
kernel_size(int or tuple)Size of the convolving kernel卷积核尺寸,可以设为1个int型数或者一个(int, int)型的元组。例如(2,3)是高2宽3卷积核
stride(int or tuple, optional)Stride of the convolution. Default: 1卷积步长,默认为1。可以设为1个int型数或者一个(int, int)型的元组。
padding(int or tuple, optional)Zero-padding added to both sides of the input. Default: 0填充操作,控制padding_mode的数目。
padding_mode(string, optional)‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’padding模式,默认为Zero-padding 。
dilation(int or tuple, optional)Spacing between kernel elements. Default: 1扩张操作:控制kernel点(卷积核点)的间距,默认值:1。
groups(int, optional)Number of blocked connections from input channels to output channels. Default: 1group参数的作用是控制分组卷积,默认不分组,为1组。
bias(bool, optional)If True, adds a learnable bias to the output. Default: True为真,则在输出中添加一个可学习的偏差。默认:True。

参数dilation——扩张卷积(也叫空洞卷积)

dilation操作动图演示如下:

Dilated Convolution with a 3 x 3 kernel and dilation rate 2

扩张卷积核为3×3,扩张率为2

参数groups——分组卷积

Group Convolution顾名思义,则是对输入feature map进行分组,然后每组分别卷积。

三、代码实例

import torch

x = torch.randn(3,1,5,4)
print(x)

conv = torch.nn.Conv2d(1,4,(2,3))
res = conv(x)

print(res.shape)    # torch.Size([3, 4, 4, 2])

输入:x[ batch_size, channels, height_1, width_1 ]

  • batch_size,一个batch中样本的个数 3
  • channels,通道数,也就是当前层的深度 1
  • height_1, 图片的高 5
  • width_1, 图片的宽 4

卷积操作:Conv2d[ channels, output, height_2, width_2 ]

  • channels,通道数,和上面保持一致,也就是当前层的深度 1
  • output ,输出的深度 4【需要4个filter】
  • height_2,卷积核的高 2
  • width_2,卷积核的宽 3

输出:res[ batch_size,output, height_3, width_3 ]

  • batch_size,,一个batch中样例的个数,同上 3
  • output, 输出的深度 4
  • height_3, 卷积结果的高度 4
  • width_3,卷积结果的宽度 2

一个样本卷积示例:

总结 

到此这篇关于PyTorch中torch.nn.Conv2d()函数的文章就介绍到这了,更多相关pytorch torch.nn.Conv2d()函数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: pytorch中的torch.nn.Conv2d()函数图文详解

本文链接: https://www.lsjlt.com/news/140793.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作