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脚本生成Android SALT扰码的方法
Sep 18 Python
python监控网站运行异常并发送邮件的方法
Mar 13 Python
Python 获得命令行参数的方法(推荐)
Jan 24 Python
Python实现查找字符串数组最长公共前缀示例
Mar 27 Python
详解python实现小波变换的一个简单例子
Jul 18 Python
PyTorch中Tensor的拼接与拆分的实现
Aug 18 Python
python制作朋友圈九宫格图片
Nov 03 Python
Python计算公交发车时间的完整代码
Feb 12 Python
python实现udp聊天窗口
Mar 31 Python
python实现二分查找算法
Sep 18 Python
Python数据可视化之绘制柱状图和条形图
May 25 Python
Python编程super应用场景及示例解析
Oct 05 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
在PHP3中实现SESSION的功能(二)
2006/10/09 PHP
建站常用13种PHP开源CMS比较
2009/08/23 PHP
php的crc32函数使用时需要注意的问题(不然就是坑)
2015/04/21 PHP
php实现基于PDO的预处理示例
2017/03/28 PHP
PHP用户注册邮件激活账户的实现代码
2017/05/31 PHP
JS中剪贴板兼容性、判断复制成功或失败
2021/03/09 Javascript
如何用JavaScript动态呼叫函数(两种方式)
2013/05/03 Javascript
jQuery获得document和window对象宽度和高度的方法
2015/03/25 Javascript
jQuery选择器源码解读(七):elementMatcher函数
2015/03/31 Javascript
jQuery+CSS实现的网页二级下滑菜单效果
2015/08/25 Javascript
JS实现登录页面记住密码和enter键登录方法推荐
2016/05/10 Javascript
深入理解$.each和$(selector).each
2016/05/15 Javascript
angular中的http拦截器Interceptors的实现
2017/02/21 Javascript
纯JS实现轮播图
2017/02/22 Javascript
JS点击图片弹出文件选择框并覆盖原图功能的实现代码
2017/08/25 Javascript
apicloud拉起小程序并传递参数的方法示例
2018/11/21 Javascript
JS实现的新闻列表自动滚动效果示例
2019/01/30 Javascript
微信小程序使用蓝牙小插件
2019/09/23 Javascript
微信头像地址失效踩坑记附带解决方案
2019/09/23 Javascript
jquery检测上传文件大小示例
2020/04/26 jQuery
[04:16]DOTA2英雄梦之声_第09期_斧王
2014/06/21 DOTA
Python实现保证只能运行一个脚本实例
2015/06/24 Python
python 执行shell命令并将结果保存的实例
2018/05/11 Python
python高阶爬虫实战分析
2018/07/29 Python
python实现汉诺塔算法
2021/03/01 Python
python爬虫爬取微博评论案例详解
2019/03/27 Python
python实现kmp算法的实例代码
2019/04/03 Python
python 定时器每天就执行一次的实现代码
2019/08/14 Python
python 如何将office文件转换为PDF
2020/09/22 Python
公务员的自我鉴定
2013/10/26 职场文书
可贵的沉默教学反思
2014/02/06 职场文书
艺术设计专业个人求职信
2014/04/10 职场文书
论语读书笔记
2015/06/26 职场文书
董事长开业致辞
2015/07/29 职场文书
社会心理学学习心得体会
2016/01/22 职场文书
Node.js实现断点续传
2021/06/23 Javascript