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

yipeiwu_com5年前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网络编程学习笔记(六):Web客户端访问

6.1 最简单的爬虫 网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成。python的urllib\urllib2等模块很容易实现这一功能,下面的例...

python实现对图片进行旋转,放缩,裁剪的功能

先说明下,我这是对某个目录下的图片名称进行操作,该目录下的图片名称为1.jpg,2.jpg。。。。。这样类似的图片名。 1.旋转 # -*-coding:utf-8-*- from...

python Pandas 读取txt表格的实例

运行环境 Python 2.7 操作实例 1.原始文本格式:空格分隔的txt,例如 2016-03-22 00:06:24.4463094 中文测试字符 2016-03-22 00...

python实现图片压缩代码实例

前言 项目中大量用到图片加载,由于图片太大,加载速度很慢,因此需要对文件进行统一压缩 一:导入包 from PIL import Image import os 二:获取图片文件...

利用numpy实现一、二维数组的拼接简单代码示例

一维数组 1.numpy初始化一维数组 a = np.array([1,2,3]); print a.shape 输出的值应该为(3,) 二维数组 2.numpy初始化二维数组...