torch.nn.AvgPool2d(kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None)(input) -> Tensor
For more information, see torch.nn.AvgPool2d.
mindspore.nn.AvgPool2d(kernel_size=1, stride=1, pad_mode='valid', padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None, data_format='NCHW')(x) -> Tensor
For more information, see mindspore.nn.AvgPool2d.
PyTorch: Apply two-dimensional averaging pooling to an input signal consisting of multiple input planes.
MindSpore: This API implementation function of MindSpore is compatible with TensorFlow and PyTorch, When pad_mode
is "valid" or "same", the function is consistent with TensorFlow, and when pad_mode
is "pad", the function is consistent with PyTorch, compared with PyTorch 1.8.1, MindSpore additionally supports 3D input, which is consistent with PyTorch 1.12.
Categories | Subcategories | PyTorch | MindSpore | Differences |
---|---|---|---|---|
Parameters | Parameter 1 | kernel_size | kernel_size | Consistent function, no default values for PyTorch |
Parameter 2 | stride | stride | Consistent function, different default values of parameters | |
Parameter 3 | padding | padding | Consistent | |
Parameter 4 | ceil_mode | ceil_mode | Consistent | |
Parameter 5 | count_include_pad | count_include_pad | Consistent | |
Parameter 6 | divisor_override | divisor_override | Consistent | |
Parameter 7 | - | pad_mode | MindSpore specifies how the pooling will be filled, with optional values of "same", "valid" or "pad". PyTorch does not have this parameter | |
Parameter 8 | - | data_format | Specify the input data format in MindSpore, either "NHWC" or "NCHW". PyTorch does not have this parameter | |
Input | Single input | input | x | Same function, different parameter names |
The two APIs achieve the same function and have the same usage.
# PyTorch
import torch
import torch.nn as nn
m = nn.AvgPool2d(kernel_size=1, stride=1)
input_x = torch.tensor([[[[1, 0, 1], [0, 1, 1]]]],dtype=torch.float32)
output = m(input_x)
print(output.numpy())
# [[[[1. 0. 1.]
# [0. 1. 1.]]]]
# MindSpore
import mindspore
import mindspore.nn as nn
from mindspore import Tensor
pool = nn.AvgPool2d(kernel_size=1, stride=1)
x = Tensor([[[[1, 0, 1], [0, 1, 1]]]], dtype=mindspore.float32)
output = pool(x)
print(output)
# [[[[1. 0. 1.]
# [0. 1. 1.]]]]
Use pad mode to ensure functional consistency.
import torch
import mindspore.nn as nn
import mindspore.ops as ops
pool = nn.AvgPool2d(4, stride=1, ceil_mode=True, pad_mode='pad', padding=2)
x1 = ops.randn(6, 6, 8, 8)
output = pool(x1)
print(output.shape)
# (6, 6, 9, 9)
pool = torch.nn.AvgPool2d(4, stride=1, ceil_mode=True, padding=2)
x1 = torch.randn(6, 6, 8, 8)
output = pool(x1)
print(output.shape)
# torch.Size([6, 6, 9, 9])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。