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 相关文章推荐
用C++封装MySQL的API的教程
May 06 Python
python实现在sqlite动态创建表的方法
May 08 Python
Python处理字符串之isspace()方法的使用
May 19 Python
Python文件操作,open读写文件,追加文本内容实例
Dec 14 Python
TensorFlow实现Batch Normalization
Mar 08 Python
Python DataFrame.groupby()聚合函数,分组级运算
Sep 18 Python
对python中xlsx,csv以及json文件的相互转化方法详解
Dec 25 Python
python中使用 xlwt 操作excel的常见方法与问题
Jan 13 Python
Python使用while循环花式打印乘法表
Jan 28 Python
python数据预处理 :样本分布不均的解决(过采样和欠采样)
Feb 29 Python
Python进程间通信multiprocess代码实例
Mar 18 Python
Python 如何操作 SQLite 数据库
Aug 17 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安装php_rar扩展实现rar文件读取和解压的方法
2016/11/17 PHP
php使用 readfile() 函数设置文件大小大小的方法
2017/08/11 PHP
jQuery弹性滑动导航菜单实现思路及代码
2013/05/02 Javascript
jQuery实现自动调整字体大小的方法
2015/06/15 Javascript
js实现点击切换TAB标签实例
2015/08/21 Javascript
js传值后台中文出现乱码的解决方法
2016/06/30 Javascript
基于JS如何实现给字符加千分符(65,541,694,158)
2016/08/03 Javascript
JavaScript如何实现图片懒加载(lazyload) 提高用户体验(增强版)
2016/11/30 Javascript
Javascript同时声明一连串(多个)变量的方法
2017/01/23 Javascript
js中setTimeout的妙用--防止循环超时
2017/03/06 Javascript
[js高手之路]寄生组合式继承的优势详解
2017/08/28 Javascript
JS跳转手机站url的若干注意事项
2017/10/18 Javascript
浅谈Node.js CVE-2017-14849 漏洞分析(详细步骤)
2017/11/10 Javascript
Vue.js组件间通信方式总结【推荐】
2018/11/23 Javascript
angular4自定义组件非input元素实现ngModel双向数据绑定的方法
2018/12/28 Javascript
vue基础之模板和过滤器用法实例分析
2019/03/12 Javascript
详解Vuex下Store的模块化拆分实践
2019/07/31 Javascript
vue 全局环境切换问题
2019/10/27 Javascript
JavaScript内置对象之Array的使用小结
2020/05/12 Javascript
原生JS实现无缝轮播图片
2020/06/24 Javascript
微信小程序基于高德地图API实现天气组件(动态效果)
2020/10/22 Javascript
vuex的数据渲染与修改浅析
2020/11/26 Vue.js
[57:29]Alliance vs KG 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/17 DOTA
Python中的赋值、浅拷贝、深拷贝介绍
2015/03/09 Python
python实现装饰器、描述符
2018/02/28 Python
Python 爬取携程所有机票的实例代码
2018/06/11 Python
使用Python刷淘宝喵币(低阶入门版)
2019/10/30 Python
Python连接SQLite数据库并进行增册改查操作方法详解
2020/02/18 Python
HTML5调用手机发短信和打电话功能
2020/04/29 HTML / CSS
英国电器零售商:PRC Direct
2018/06/21 全球购物
Ego Shoes官网:英国时髦鞋类品牌
2020/10/19 全球购物
2014年中秋寄语
2014/08/11 职场文书
新郎结婚保证书
2015/02/26 职场文书
本科毕业论文致谢怎么写
2015/05/14 职场文书
公司庆典主持词
2015/07/04 职场文书
《纸船和风筝》教学反思
2016/02/18 职场文书