参考链接: class torch.nn.AdaptiveAvgPool2d(output_size)
参考链接: torch.nn.functional.adaptive_avg_pool2d(input, output_size)
代码实验:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> from torch import nn
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002185FBED330>
>>>
>>> model = nn.AdaptiveAvgPool2d((5,7))
>>>
>>> data_in = torch.randn(1, 64, 8, 9)
>>> data_in.shape
torch.Size([1, 64, 8, 9])
>>>
>>> torch.mean(data_in)
tensor(6.0037e-06)
>>>
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 5, 7])
>>> torch.mean(data_out)
tensor(-0.0014)
>>>
>>>
>>>
>>> model = nn.AdaptiveAvgPool2d((4,3))
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 4, 3])
>>> torch.mean(data_out)
tensor(6.0081e-06)
>>>
>>>
>>> model = nn.AdaptiveAvgPool2d((5))
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 5, 5])
>>> torch.mean(data_out)
tensor(-0.0043)
>>>
>>>
>>>
>>> model = nn.AdaptiveMaxPool2d((None, 7))
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 8, 7])
>>> torch.mean(data_out)
tensor(0.6099)
>>>
>>>
>>> model = nn.AdaptiveMaxPool2d((4,None))
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 4, 9])
>>> torch.mean(data_out)
tensor(0.5667)
>>>
>>>
>>>
>>>
>>> model = nn.AdaptiveMaxPool2d((None,None))
>>> data_out = model(data_in)
>>> data_out.shape
torch.Size([1, 64, 8, 9])
>>> torch.mean(data_out)
tensor(6.0037e-06)
>>>
>>>
>>>
代码实验:
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>conda activate ssd4pytorch1_2_0
(ssd4pytorch1_2_0) C:\Users\chenxuqi>python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> from torch import nn
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x00000196C660D330>
>>>
>>> data_in = torch.randn(1, 64, 8, 9)
>>> data_in.shape
torch.Size([1, 64, 8, 9])
>>>
>>> data_out = nn.functional.adaptive_avg_pool2d(data_in, output_size=5)
>>> data_out.shape
torch.Size([1, 64, 5, 5])
>>>
>>>
>>> data_out = nn.functional.adaptive_avg_pool2d(data_in, output_size=(4,3))
>>> data_out.shape
torch.Size([1, 64, 4, 3])
>>>
>>>
>>>
>>> nn.functional.adaptive_avg_pool2d(data_in, output_size=(4,None)).shape
torch.Size([1, 64, 4, 9])
>>>
>>> nn.functional.adaptive_avg_pool2d(data_in, output_size=(None,None)).shape
torch.Size([1, 64, 8, 9])
>>>
>>> nn.functional.adaptive_avg_pool2d(data_in, output_size=(None,4)).shape
torch.Size([1, 64, 8, 4])
>>>
>>>
>>>