pytorch中的卷积和池化计算方式详解

 更新时间:2020年01月03日 10:06:37   作者:一只tobey  
今天小编就为大家分享一篇pytorch中的卷积和池化计算方式详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

TensorFlow里面的padding只有两个选项也就是valid和same

pytorch里面的padding么有这两个选项,它是数字0,1,2,3等等,默认是0

所以输出的h和w的计算方式也是稍微有一点点不同的:tf中的输出大小是和原来的大小成倍数关系,不能任意的输出大小;而nn输出大小可以通过padding进行改变

nn里面的卷积操作或者是池化操作的H和W部分都是一样的计算公式:H和W的计算

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中的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中的卷积和池化计算方式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解在Python中以绝对路径或者相对路径导入文件的方法

    详解在Python中以绝对路径或者相对路径导入文件的方法

    这篇文章主要介绍了详解在Python中以绝对路径或者相对路径导入文件的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • 使用python中的openpyxl操作excel详解

    使用python中的openpyxl操作excel详解

    这篇文章主要介绍了使用python中的openpyxl操作excel详解,openpyxl 模块是一个读写Excel文档的Python库,本文就来讲解如何使用openpyxl操作excel,需要的朋友可以参考下
    2023-07-07
  • python实现简易版计算器

    python实现简易版计算器

    这篇文章主要为大家详细介绍了python实现简易版计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • python中Pyqt5使用Qlabel标签进行视频播放

    python中Pyqt5使用Qlabel标签进行视频播放

    这篇文章主要介绍了python中Pyqt5使用Qlabel实现标签进行视频播放,QLabel是界面中的标签类,继承自QFrame类,提供文本和图像的显示,是一种展示控件,下文相关内容介绍需要的小伙伴可以参考一下
    2022-04-04
  • 用python爬取历史天气数据的方法示例

    用python爬取历史天气数据的方法示例

    这篇文章主要介绍了用python爬取历史天气数据的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Python使用matplotlib实现的图像读取、切割裁剪功能示例

    Python使用matplotlib实现的图像读取、切割裁剪功能示例

    这篇文章主要介绍了Python使用matplotlib实现的图像读取、切割裁剪功能,结合实例形式分析了Python基于matplotlib操作图片的加载、读取、坐标控制及裁剪相关操作技巧,需要的朋友可以参考下
    2018-04-04
  • Python学习笔记之解析json的方法分析

    Python学习笔记之解析json的方法分析

    这篇文章主要介绍了Python解析json的方法,结合实例形式分析了常见的Python解析与转换json格式数据相关操作技巧,需要的朋友可以参考下
    2017-04-04
  • Python Requests库及用法详解

    Python Requests库及用法详解

    Requests库作为Python中最受欢迎的HTTP库之一,为开发人员提供了简单而强大的方式来发送HTTP请求和处理响应,本文将带领您深入探索Python Requests库的世界,我们将从基础知识开始,逐步深入,覆盖各种高级用法和技巧,感兴趣的朋友一起看看吧
    2024-06-06
  • Python数据分析之绘制m1-m2数据

    Python数据分析之绘制m1-m2数据

    这篇文章主要介绍了Python数据分析之绘制m1-m2数据,文章基于python的相关资料展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • Django实现从数据库中获取到的数据转换为dict

    Django实现从数据库中获取到的数据转换为dict

    这篇文章主要介绍了Django实现从数据库中获取到的数据转换为dict,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03

最新评论