MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / avg_pool2d

Function avg_pool2d

imperative/python/megengine/functional/nn.py:715–762  ·  view source on GitHub ↗

r"""Applies 2D average pooling over an input tensor. Refer to :class:`~.AvgPool2d` for more information. Args: inp: input tensor of shape :math:`(N, C, H_{\text{in}}, W_{\text{in}})` . kernel_size: size of the window used to calculate the average value. stride: stri

(
    inp: Tensor,
    kernel_size: Union[int, Tuple[int, int]],
    stride: Optional[Union[int, Tuple[int, int]]] = None,
    padding: Union[int, Tuple[int, int]] = 0,
    mode: str = "average_count_exclude_padding",
)

Source from the content-addressed store, hash-verified

713
714
715def avg_pool2d(
716 inp: Tensor,
717 kernel_size: Union[int, Tuple[int, int]],
718 stride: Optional[Union[int, Tuple[int, int]]] = None,
719 padding: Union[int, Tuple[int, int]] = 0,
720 mode: str = "average_count_exclude_padding",
721) -> Tensor:
722 r"""Applies 2D average pooling over an input tensor.
723
724 Refer to :class:`~.AvgPool2d` for more information.
725
726 Args:
727 inp: input tensor of shape :math:`(N, C, H_{\text{in}}, W_{\text{in}})` .
728 kernel_size: size of the window used to calculate the average value.
729 stride: stride of the window. Default value is ``kernel_size``.
730 padding: implicit zero padding added on both sides. Default: 0.
731 mode: whether to include the padding values while calculating the average, set
732 to "average" will do counting.
733 Default: "average_count_exclude_padding"
734
735 Returns:
736 output tensor of shape :math:`(N, C, H_{\text{out}}, W_{\text{out}})`.
737
738 Examples:
739 >>> import numpy as np
740 >>> inp = Tensor(np.arange(1 * 1 * 3 * 4).astype(np.float32).reshape(1, 1, 3, 4))
741 >>> F.avg_pool2d(inp, kernel_size=2, stride=2, padding=[1,0], mode="average")
742 Tensor([[[[0.25 1.25]
743 [6.5 8.5 ]]]], device=xpux:0)
744 """
745 if stride is None:
746 stride = kernel_size
747 window_h, window_w = expand_hw(kernel_size)
748 stride_h, stride_w = expand_hw(stride)
749 padding_h, padding_w = expand_hw(padding)
750
751 op = builtin.Pooling(
752 window_h=window_h,
753 window_w=window_w,
754 stride_h=stride_h,
755 stride_w=stride_w,
756 pad_h=padding_h,
757 pad_w=padding_w,
758 mode=mode,
759 strategy=get_execution_strategy(),
760 )
761 (output,) = apply(op, inp)
762 return output
763
764
765def adaptive_max_pool2d(

Callers 1

forwardMethod · 0.85

Calls 3

expand_hwFunction · 0.85
get_execution_strategyFunction · 0.70
applyFunction · 0.50

Tested by

no test coverage detected