pytorch中的卷积和池化计算方式详解


Posted in Python onJanuary 03, 2020

TensorFlow里面的padding只有两个选项也就是valid和same

pytorch里面的padding么有这两个选项,它是数字0,1,2,3等等,默认是0

所以输出的h和w的计算方式也是稍微有一点点不同的:tf中的输出大小是和原来的大小成倍数关系,不能任意的输出大小;而nn输出大小可以通过padding进行改变

nn里面的卷积操作或者是池化操作的H和W部分都是一样的计算公式:H和W的计算

pytorch中的卷积和池化计算方式详解

class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False):
"""
Parameters: 
  kernel_size ? the size of the window to take a max over
  stride ? the stride of the window. 默认值是kernel_size
  padding ? implicit zero padding to be added on both side,默认值是0
  dilation ? a parameter that controls the stride of elements in the window,默认值是1
  return_indices ? if True, will return the max indices along with the outputs. Useful when Unpooling later
  ceil_mode ? when True, will use ceil instead of floor to compute the output shape,向上取整和向下取整,默认是向下取整
"""

不一样的地方在于:第一点,步长stride默认值,上面默认和设定的kernel_size一样,下面默认是1;第二点,输出通道的不一样,上面的输出通道和输入通道是一样的也就是没有改变特征图的数目,下面改变特征图的数目为out_channels

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
    pass
"""
Parameters: 
  in_channels (int) ? Number of channels in the input image
  out_channels (int) ? Number of channels produced by the convolution
  kernel_size (int or tuple) ? Size of the convolving kernel
  stride (int or tuple, optional) ? Stride of the convolution. Default: 1,默认是1
  padding (int or tuple, optional) ? Zero-padding added to both sides of the input. Default: 0
  dilation (int or tuple, optional) ? Spacing between kernel elements. Default: 1
  groups (int, optional) ? Number of blocked connections from input channels to output channels. Default: 1
  bias (bool, optional) ? If True, adds a learnable bias to the output. Default: True
"""

第三点不一样是卷积有一个参数groups,将特征图分开给不同的卷积进行操作然后再整合到一起,xception就是利用这一个。

"""
At groups=1, all inputs are convolved to all outputs.
At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.
At groups= in_channels, each input channel is convolved with its own set of filters (of size ⌊out_channelsin_channels⌋
).
"""

pytorch AvgPool2d函数

class torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, 
             ceil_mode=False, count_include_pad=True):
  pass
"""
kernel_size: the size of the window
stride: the stride of the window. Default value is :attr:`kernel_size`
padding: implicit zero padding to be added on both sides
ceil_mode: when True, will use `ceil` instead of `floor` to compute the output shape
count_include_pad: when True, will include the zero-padding in the averaging calculation
"""

shape的计算公式,在(h,w)位置处的输出值的计算。

pytorch中的卷积和池化计算方式详解

pytorch中的F.avg_pool1d()平均池化操作作用于一维,input 的维度是三维比如[2,2,7]。F.avg_pool1d()中核size是3,步长是2表示每三个数取平均,每隔两个数取一次.比如[1,3,3,4,5,6,7]安照3个数取均值,两步取一次,那么结果就是[ 2.3333 ,4 ,6 ],也就是核是一维的,也只作用于一个维度。按照池化操作计算公式input size为[2,2,7],kernel size为3,步长为2,则输出维度计算(7-3)/2+1=3所以输出维度是[2,2,3],这与输出结果是一致的。

pytorch中的F.avg_pool2d(),input 是维度是4维如[2,2,4,4],表示这里批量数是2也就是两张图像,这里通道数量是2,图像是size 是4*4的.核size是(2,2),步长是(2,2)表示被核覆盖的数取平均,横向纵向的步长都是2.那么核是二维的,所以取均值时也是覆盖二维取的。输出中第一个1.5的计算是:(1+2+1+2)/4=1.5.表示第一张图像左上角的四个像素点的均值。按照池化操作计算公式input size为[2,2,4,4],kernel size为2*2,步长为2,则输出维度计算(4-2)/2+1=2所以输出维度是[2,2,2,2],这与输出结果是一致的。

Conv3d函数

class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1,
           padding=0, dilation=1, groups=1, bias=True):
  pass
"""
in_channels (int): Number of channels in the input image
out_channels (int): Number of channels produced by the convolution
kernel_size (int or tuple): Size of the convolving kernel
stride (int or tuple, optional): Stride of the convolution. Default: 1
padding (int or tuple, optional): Zero-padding added to all three sides of the input. Default: 0
dilation (int or tuple, optional): Spacing between kernel elements. Default: 1
groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
Shape:
    - Input: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`
    - Output: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`
"""
  C_out = out_channels

pytorch中的卷积和池化计算方式详解

以上这篇pytorch中的卷积和池化计算方式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详解Python3中的Sequence type的使用
Aug 01 Python
Django视图之ORM数据库查询操作API的实例
Oct 27 Python
Python爬虫实例爬取网站搞笑段子
Nov 08 Python
TF-IDF与余弦相似性的应用(一) 自动提取关键词
Dec 21 Python
python实现从文件中读取数据并绘制成 x y 轴图形的方法
Oct 14 Python
Python魔法方法功能与用法简介
Apr 04 Python
python调用并链接MATLAB脚本详解
Jul 05 Python
Python selenium抓取虎牙短视频代码实例
Mar 02 Python
Python configparser模块配置文件过程解析
Mar 03 Python
利用Python脚本批量生成SQL语句
Mar 04 Python
有趣的Python图片制作之如何用QQ好友头像拼接出里昂
Apr 22 Python
Python爬虫之Selenium警告框(弹窗)处理
Dec 04 Python
Python While循环语句实例演示及原理解析
Jan 03 #Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 #Python
Python综合应用名片管理系统案例详解
Jan 03 #Python
Python tkinter常用操作代码实例
Jan 03 #Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 #Python
nginx搭建基于python的web环境的实现步骤
Jan 03 #Python
Python如何使用字符打印照片
Jan 03 #Python
You might like
PHP之uniqid()函数用法
2014/11/03 PHP
基于php的微信公众平台开发入门实例
2015/04/15 PHP
基于OpenCart 开发支付宝,财付通,微信支付参数错误问题
2015/10/01 PHP
Laravel框架用户登陆身份验证实现方法详解
2017/09/14 PHP
javascript YUI 读码日记之 YAHOO.util.Dom - Part.4
2008/03/22 Javascript
关于JavaScript的一些看法
2009/05/27 Javascript
Jquery事件的连接使用示例
2013/06/18 Javascript
JavaScript中把数字转换为字符串的程序代码
2013/06/19 Javascript
javascript相关事件的几个概念
2015/05/21 Javascript
jquery常用函数与方法汇总
2015/09/01 Javascript
学习JavaScript设计模式之策略模式
2016/01/12 Javascript
socket.io学习教程之深入学习篇(三)
2017/04/29 Javascript
解决vue单页使用keep-alive页面返回不刷新的问题
2018/03/13 Javascript
浅析vue-router原理
2018/10/19 Javascript
Vue Render函数创建DOM节点代码实例
2020/07/08 Javascript
解决vuex刷新数据消失问题
2020/11/12 Javascript
vue+Element-ui实现登录注册表单
2020/11/17 Javascript
JS数组索引检测中的数据类型问题详解
2021/01/11 Javascript
[48:24]完美世界DOTA2联赛循环赛LBZS vs Forest 第一场 10月30日
2020/10/31 DOTA
python获取一组数据里最大值max函数用法实例
2015/05/26 Python
详解在Python程序中自定义异常的方法
2015/10/16 Python
python+opencv像素的加减和加权操作的实现
2019/07/14 Python
Python的bit_length函数来二进制的位数方法
2019/08/27 Python
用Python实现校园通知更新提醒功能
2019/11/23 Python
澳大利亚领先的睡衣品牌:Peter Alexander
2016/08/16 全球购物
澳大利亚相机之家:Camera House
2017/11/30 全球购物
丝芙兰意大利官方网站:Sephora.it
2019/12/13 全球购物
后勤采购员岗位职责
2013/12/19 职场文书
幼儿园教师个人反思
2014/01/30 职场文书
2014年团委工作总结
2014/11/13 职场文书
2014年应急工作总结
2014/12/11 职场文书
幼师中班个人总结
2015/02/12 职场文书
宝葫芦的秘密观后感
2015/06/11 职场文书
班干部学习委员竞选稿
2015/11/20 职场文书
《7的乘法口诀》教学反思
2016/02/18 职场文书
java如何实现获取客户端ip地址的示例代码
2022/04/07 Java/Android