pytorch torch.nn.AdaptiveAvgPool2d()自适应平均池化函数详解

yipeiwu_com6年前Python基础

如题:只需要给定输出特征图的大小就好,其中通道数前后不发生变化。具体如下:

AdaptiveAvgPool2d

CLASStorch.nn.AdaptiveAvgPool2d(output_size)[SOURCE]

Applies a 2D adaptive average pooling over an input signal composed of several input planes.

The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.

Parameters

output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a int, or None which means the size will be the same as that of the input.

Examples

>>> # target output size of 5x7
>>> m = nn.AdaptiveAvgPool2d((5,7))
>>> input = torch.randn(1, 64, 8, 9)
>>> output = m(input)
>>> # target output size of 7x7 (square)
>>> m = nn.AdaptiveAvgPool2d(7)
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> # target output size of 10x7
>>> m = nn.AdaptiveMaxPool2d((None, 7))
>>> input = torch.randn(1, 64, 10, 9)
>>> output = m(input)
>>> input = torch.randn(1, 3, 3, 3)
>>> input
tensor([[[[ 0.6574, 1.5219, -1.3590],
   [-0.1561, 2.7337, -1.8701],
   [-0.8572, 1.0238, -1.9784]],
 
   [[ 0.4284, 1.4862, 0.3352],
   [-0.7796, -0.8020, -0.1243],
   [-1.2461, -1.7069, 0.1517]],
 
   [[ 1.4593, -0.1287, 0.5369],
   [ 0.6562, 0.0616, 0.2611],
   [-1.0301, 0.4097, -1.9269]]]])
>>> m = nn.AdaptiveAvgPool2d((2, 2))
>>> output = m(input)
>>> output
tensor([[[[ 1.1892, 0.2566],
   [ 0.6860, -0.0227]],
 
   [[ 0.0833, 0.2238],
   [-1.1337, -0.6204]],
 
   [[ 0.5121, 0.1827],
   [ 0.0243, -0.2986]]]])
>>> 0.6574+1.5219+2.7337-0.1561
4.7569
>>> 4.7569/4
1.189225
>>> 

以上这篇pytorch torch.nn.AdaptiveAvgPool2d()自适应平均池化函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python标准异常和异常处理详解

python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误。你可以使用该功能来调试python程序。 1.异常处理: 本站Python教程会具体介绍。 2.断言(...

使用Python的PEAK来适配协议的教程

如果您正尝试去处理元类,或者正受困于 Twisted 中的异步编程,或者正在研究由于使用了多分派而使您精疲力尽的面向对象编程,那么您完全错了!PEAK 将所有这些中的一些要素组合到了一个...

Django通用类视图实现忘记密码重置密码功能示例

Django通用类视图实现忘记密码重置密码功能示例

前言 在Django中有大量的通用类视图,例如ListView,DetailView,CreateView,UpdateView等等,将所有重复的增删改查代码抽象成一个通用类,只需要配置...

Django 实现购物车功能的示例代码

Django 实现购物车功能的示例代码

购物车思路:使用 session 功能识别不同浏览器用户,使得用户不管是否登录了网站,均能够把想要购买的产品放在某个地方,之后随时可以显示或修改要购买的产品,等确定了之后再下订单,购物车...

Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】

Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】

本文实例讲述了Python实现曲线拟合操作。分享给大家供大家参考,具体如下: 这两天学习了用python来拟合曲线。 一、环境配置 本人比较比较懒,所以下载的全部是exe文件来安装,安装...