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 相关文章推荐
django实现分页的方法
May 26 Python
Django1.7+python 2.78+pycharm配置mysql数据库
Oct 09 Python
Python使用django框架实现多人在线匿名聊天的小程序
Nov 29 Python
python使用pycharm环境调用opencv库
Feb 11 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
Apr 23 Python
Pyspider中给爬虫伪造随机请求头的实例
May 07 Python
pip命令无法使用的解决方法
Jun 12 Python
Python简单基础小程序的实例代码
Apr 28 Python
Python 安装第三方库 pip install 安装慢安装不上的解决办法
Jun 18 Python
简单了解Python生成器是什么
Jul 02 Python
TensorFlow 输出checkpoint 中的变量名与变量值方式
Feb 11 Python
详解Python中的Lock和Rlock
Jan 26 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+shell实现多线程的方法
2015/07/01 PHP
微信公众平台开发之配置与请求
2015/08/26 PHP
找到一点可怜的关于dojo资料,谢谢作者!
2006/12/06 Javascript
JavaScript中的Location地址对象
2008/01/16 Javascript
js AspxButton的客户端操作
2009/06/26 Javascript
网页打开自动最大化的js代码
2012/08/22 Javascript
div浮层,滚动条移动,位置保持不变的4种方法汇总
2013/12/11 Javascript
Bootstrap3.0建站教程(一)之bootstrap表单元素排版
2016/06/01 Javascript
浅谈js对象的创建和对6种继承模式的理解和遐想
2016/10/16 Javascript
jQuery实现手机上输入后隐藏键盘功能
2017/01/04 Javascript
vue组件中的数据传递方法
2018/05/14 Javascript
vue.js使用v-if实现显示与隐藏功能示例
2018/07/06 Javascript
详解Angular中通过$location获取地址栏的参数
2018/08/02 Javascript
基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能
2019/04/02 Javascript
jquery实现垂直无限轮播的方法分析
2019/07/16 jQuery
理解JavaScript中的Proxy 与 Reflection API
2020/09/21 Javascript
[01:56]生活中的妖精之七夕特别档
2016/08/09 DOTA
利用Python演示数型数据结构的教程
2015/04/03 Python
Python正则表达式使用范例分享
2016/12/04 Python
Windows 64位下python3安装nltk模块
2018/09/19 Python
python 获取一个值在某个区间的指定倍数的值方法
2018/11/12 Python
Python中Numpy ndarray的使用详解
2019/05/24 Python
解决Atom安装Hydrogen无法运行python3的问题
2019/08/28 Python
python中的RSA加密与解密实例解析
2019/11/18 Python
Python实现图片添加文字
2019/11/26 Python
Python嵌入C/C++进行开发详解
2020/06/09 Python
python uuid生成唯一id或str的最简单案例
2021/01/13 Python
英国人最爱的饰品网站:Accessorize
2016/08/22 全球购物
全球速卖通巴西站点:Aliexpress巴西
2016/08/24 全球购物
IFCHIC台湾:欧美国际设计师品牌
2019/05/18 全球购物
行政人事岗位职责
2014/03/17 职场文书
大学生工作求职信
2014/06/23 职场文书
抢劫罪辩护词
2015/05/21 职场文书
运动会广播稿300字
2015/08/19 职场文书
2016年精神文明建设先进个人事迹材料
2016/02/29 职场文书
Django操作cookie的实现
2021/05/26 Python