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的urllib模块显示下载进度示例
Jan 17 Python
Python3实现从文件中读取指定行的方法
May 22 Python
Python3实现简单可学习的手写体识别(实例讲解)
Oct 21 Python
Python numpy实现数组合并实例(vstack,hstack)
Jan 09 Python
python实现K最近邻算法
Jan 29 Python
使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”
Mar 23 Python
PyTorch上实现卷积神经网络CNN的方法
Apr 28 Python
Python3实现的字典、列表和json对象互转功能示例
May 22 Python
python实现根据文件关键字进行切分为多个文件的示例
Dec 10 Python
Python设计模式之模板方法模式实例详解
Jan 17 Python
用vue.js组件模拟v-model指令实例方法
Jul 05 Python
Python简易计算器制作方法代码详解
Oct 31 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导出oracle库的php代码
2009/04/20 PHP
php下使用strpos需要注意 === 运算符
2010/07/17 PHP
探讨fckeditor在Php中的配置详解
2013/06/08 PHP
Laravel解决nesting level错误和隐藏index.php的问题
2019/10/12 PHP
可以显示单图片,多图片ajax请求的ThickBox3.1类下载
2007/12/23 Javascript
解决extjs在firefox中关闭窗口再打开后iframe中js函数访问不到的问题
2008/11/06 Javascript
javascript面向对象编程(一) 实例代码
2010/06/25 Javascript
jQuery EasyUI API 中文文档 - MenuButton菜单按钮使用介绍
2011/10/06 Javascript
js判断是否按下了Shift键的方法
2015/01/27 Javascript
jquery中radio checked问题
2015/03/16 Javascript
BootStrap selectpicker
2016/06/20 Javascript
JS实现的表格行上下移动操作示例
2016/08/03 Javascript
移动端js图片查看器
2016/11/17 Javascript
JavaScript中的this陷阱的最全收集并整理(没有之一)
2017/02/21 Javascript
BootStrap 表单控件之单选按钮水平排列
2017/05/23 Javascript
js编写选项卡效果
2017/05/23 Javascript
JS实现HTML页面中动态显示当前时间完整示例
2018/07/30 Javascript
vue移动端弹框组件的实例
2018/09/25 Javascript
详解在React项目中安装并使用Less(用法总结)
2019/03/18 Javascript
Python捕捉和模拟鼠标事件的方法
2015/06/03 Python
python中引用与复制用法实例分析
2015/06/04 Python
深入浅出分析Python装饰器用法
2017/07/28 Python
Python测试人员需要掌握的知识
2018/02/08 Python
python操作kafka实践的示例代码
2019/06/19 Python
如何设置PyCharm中的Python代码模版(推荐)
2020/11/20 Python
CSS3制作炫酷的自定义发光文字
2016/03/28 HTML / CSS
教你使用Canvas处理图片的方法
2017/11/28 HTML / CSS
女士时装鞋:Chinese Laundry
2018/08/29 全球购物
365 Tickets英国:全球景点门票
2019/07/06 全球购物
俄罗斯最大的隐形眼镜销售网站:Ochkov.Net
2021/02/07 全球购物
男方父母证婚词
2014/01/12 职场文书
营销部内勤岗位职责
2014/04/30 职场文书
另类冲刺标语
2014/06/24 职场文书
请客吃饭开场白
2015/06/01 职场文书
《假如》教学反思
2016/02/17 职场文书
Java后端 Dubbo retries 超时重试机制的解决方案
2022/04/14 Java/Android