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 相关文章推荐
Python中列表和元组的使用方法和区别详解
Dec 30 Python
多版本Python共存的配置方法
May 22 Python
Python tkinter事件高级用法实例
Jan 31 Python
Python中defaultdict与lambda表达式用法实例小结
Apr 09 Python
python和shell获取文本内容的方法
Jun 05 Python
python实现简单登陆系统
Oct 18 Python
利用Python如何实现一个小说网站雏形
Nov 23 Python
python json.loads兼容单引号数据的方法
Dec 19 Python
Python日志无延迟实时写入的示例
Jul 11 Python
python except异常处理之后不退出,解决异常继续执行的实现
Apr 25 Python
Django后端分离 使用element-ui文件上传方式
Jul 12 Python
python代码能做成软件吗
Jul 24 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获取数组中某元素的位置及array_keys函数应用
2013/01/29 PHP
部署PHP项目应该注意的几点事项分享
2013/12/20 PHP
php常用hash加密函数
2014/11/22 PHP
ZF框架实现发送邮件的方法
2015/12/03 PHP
PHP下载文件的函数实例代码
2016/05/18 PHP
PHP封装的MSSql操作类完整实例
2016/05/26 PHP
PHP连接MYSQL数据库的3种常用方法
2017/02/27 PHP
PHP实现长轮询消息实时推送功能代码实例讲解
2021/02/26 PHP
Avengerls vs Newbee BO3 第一场2.18
2021/03/10 DOTA
javascript 进度条 实现代码
2009/07/30 Javascript
WEB高性能开发之疯狂的HTML压缩
2010/06/19 Javascript
轻量级 JS ToolTip提示效果
2010/07/20 Javascript
jquery mobile事件多次绑定示例代码
2013/09/13 Javascript
jquery 漂亮的删除确认和提交无刷新删除示例
2013/11/13 Javascript
JS简单循环遍历json数组的方法
2016/04/22 Javascript
AngularJS 中的Promise --- $q服务详解
2016/09/14 Javascript
Angular1.x复杂指令实例详解
2017/03/01 Javascript
详解用node-images 打造简易图片服务器
2017/05/08 Javascript
Bootstrap table中toolbar新增条件查询及refresh参数使用方法
2018/05/18 Javascript
浅谈vue中组件绑定事件时是否加.native
2019/11/09 Javascript
Python自动化测试工具Splinter简介和使用实例
2014/05/13 Python
在Linux上安装Python的Flask框架和创建第一个app实例的教程
2015/03/30 Python
Python 运行.py文件和交互式运行代码的区别详解
2019/07/02 Python
Python符号计算之实现函数极限的方法
2019/07/15 Python
HTML5之SVG 2D入门6—视窗坐标系与用户坐标系及变换概述
2013/01/30 HTML / CSS
英国高级百货公司:Harvey Nichols
2017/01/29 全球购物
沙特阿拉伯网上购物:Sayidaty Mall
2018/05/06 全球购物
Notino匈牙利:购买香水和化妆品
2019/04/12 全球购物
Michael Kors香港官网:美国奢侈品品牌
2019/12/26 全球购物
深圳-东方伟业笔试部分
2015/02/11 面试题
领导干部培训感言
2014/01/23 职场文书
小学生暑假感言
2014/02/06 职场文书
党员群众路线对照检查材料思想汇报
2014/09/17 职场文书
聚众斗殴罪辩护词
2015/05/21 职场文书
MYSQL如何查看进程和kill进程
2022/03/13 MySQL
使用pd.merge表连接出现多余行的问题解决
2022/06/16 Python