文章目录
1.说明
CLASS torch.nn.AdaptiveMaxPool2d(output_size, return_indices=False)
Applies a 2D adaptive max pooling over an input signal composed of several input planes.The output is of size
H
o
u
t
×
W
o
u
t
H_{out} \times W_{out}
Hout×Wout, or any input size. The number of output features is equal to the number of input planes.
在由多个输入平面组成的输入信号上应用二维自适应最大池化。输出的大小为指定的output_size,任何输入的大小。输出特征的个数等于输入平面的个数;
- output_size:用户可以指定输出的张量的大小
- return_indices:默认是False,如果为True,则返回一个元祖(Tensor,Indices),0:表示output_size大小的张量值,1:表示返回的索引。
2. 代码
# -*- coding: utf-8 -*-
# @Project: zc
# @Author: zc
# @File name: AdaptiveMaxPool
# @Create time: 2022/1/3 7:47
# 1.导入相关数据库
import torch
from torch import nn
# 2. 定义最大自适应池化操作,输出大小为(5,7)
m = nn.AdaptiveMaxPool2d((5,7))
# 3.定义最大自适应池化操作,输出大小为(7,7)
n = nn.AdaptiveMaxPool2d(7)
# 4.定义输入张量,形状(1,64,9)
input = torch.randn(1,64,9)
# 5.input=(1,64,9)->AdaptiveMaxPool2d((5,7))->output_m=[1,5,7]
output_m = m(input)
# 6.input=(1,64,9)->AdaptiveMaxPool2d((7))->output_m=[1,7,7]
output_n = n(input)
# 7. input2 = [1,64,10,9]
input2 = torch.randn(1,64,10,9)
# 8. input2 = [1,64,10,9] -> AdaptiveMaxPool2d((5,7)) -> output2=[1,64,5,7]
output_2 = m(input2)
# 9.定义最大自适应池化操作,输出大小为(None,7),None表示不变
k = nn.AdaptiveMaxPool2d((None,7))
# 10. input2 = [1,64,10,9] -> AdaptiveMaxPool2d((None,7)) -> output2=[1,64,10,7]
output_k = k(input2)
t = nn.AdaptiveMaxPool2d((5,7),return_indices=True)
output_2_T = t(input2)
# print(output_2_T[0] == output_2 )
print(f'output_2={output_2.shape}')
# print(f'output_2_T={output_2_T}')
print(f'output_m={output_m.shape}')
print(f'output_n={output_n.shape}')
print(f'output_k={output_k.shape}')
3. 结果
output_2=torch.Size([1, 64, 5, 7])
output_m=torch.Size([1, 5, 7])
output_n=torch.Size([1, 7, 7])
output_k=torch.Size([1, 64, 10, 7])