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

Function max_pool2d

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

r"""Applies a 2D max pooling over an input tensor. Refer to :class:`~.MaxPool2d` 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 max value. stride: stride of t

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

Source from the content-addressed store, hash-verified

667
668
669def max_pool2d(
670 inp: Tensor,
671 kernel_size: Union[int, Tuple[int, int]],
672 stride: Optional[Union[int, Tuple[int, int]]] = None,
673 padding: Union[int, Tuple[int, int]] = 0,
674) -> Tensor:
675 r"""Applies a 2D max pooling over an input tensor.
676
677 Refer to :class:`~.MaxPool2d` for more information.
678
679 Args:
680 inp: input tensor of shape :math:`(N, C, H_{\text{in}}, W_{\text{in}})`.
681 kernel_size: size of the window used to calculate the max value.
682 stride: stride of the window. Default value is ``kernel_size``.
683 padding: implicit zero padding added on both sides. Default: 0.
684
685 Returns:
686 output tensor of shape `(N, C, H_{\text{out}}, W_{\text{out}})`.
687
688 Examples:
689 >>> import numpy as np
690 >>> input = Tensor(np.arange(1 * 1 * 3 * 4).astype(np.float32).reshape(1, 1, 3, 4))
691 >>> F.nn.max_pool2d(input, 2, 1, 0)
692 Tensor([[[[ 5. 6. 7.]
693 [ 9. 10. 11.]]]], device=xpux:0)
694 """
695 if stride is None:
696 stride = kernel_size
697 window_h, window_w = expand_hw(kernel_size)
698 stride_h, stride_w = expand_hw(stride)
699 padding_h, padding_w = expand_hw(padding)
700
701 op = builtin.Pooling(
702 window_h=window_h,
703 window_w=window_w,
704 stride_h=stride_h,
705 stride_w=stride_w,
706 pad_h=padding_h,
707 pad_w=padding_w,
708 mode="max",
709 strategy=get_execution_strategy(),
710 )
711 (output,) = apply(op, inp)
712 return output
713
714
715def avg_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