r"""Applies a 2D max pooling over an input. For instance, given an input of the size :`(N, C, H_{\text{in}}, W_{\text{in}})` and :attr:`kernel_size` :math:`(kH, kW)`, this layer generates the output of the size :math:`(N, C, H_{\text{out}}, W_{\text{out}})` through a process described a
| 30 | |
| 31 | |
| 32 | class MaxPool2d(_PoolNd): |
| 33 | r"""Applies a 2D max pooling over an input. |
| 34 | |
| 35 | For instance, given an input of the size :`(N, C, H_{\text{in}}, W_{\text{in}})` and |
| 36 | :attr:`kernel_size` :math:`(kH, kW)`, this layer generates the output of |
| 37 | the size :math:`(N, C, H_{\text{out}}, W_{\text{out}})` through a process described as: |
| 38 | |
| 39 | .. math:: |
| 40 | |
| 41 | \begin{aligned} |
| 42 | out(N_i, C_j, h, w) ={} & \max_{m=0, \ldots, kH-1} \max_{n=0, \ldots, kW-1} |
| 43 | \text{input}(N_i, C_j, \text{stride[0]} \times h + m, |
| 44 | \text{stride[1]} \times w + n) |
| 45 | \end{aligned} |
| 46 | |
| 47 | If :attr:`padding` is non-zero, then the input is implicitly zero-padded on |
| 48 | both sides for :attr:`padding` number of points. |
| 49 | |
| 50 | Args: |
| 51 | kernel_size(Union[int, Tuple[int, int]]): the size of the window. |
| 52 | stride(Union[int, Tuple[int, int]]): the stride of the window. Default value is ``kernel_size``. |
| 53 | padding(Union[int, Tuple[int, int]]): implicit zero padding to be added on both sides.Default: 0. |
| 54 | |
| 55 | Shape: |
| 56 | - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})` |
| 57 | - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where |
| 58 | |
| 59 | .. math:: |
| 60 | H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]} |
| 61 | \times (\text{kernel\_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor |
| 62 | |
| 63 | .. math:: |
| 64 | W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]} |
| 65 | \times (\text{kernel\_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor |
| 66 | |
| 67 | Returns: |
| 68 | Return type: module. The instance of the ``MaxPool2d`` module. |
| 69 | |
| 70 | Examples: |
| 71 | >>> import numpy as np |
| 72 | >>> m = M.MaxPool2d(kernel_size=3, stride=1, padding=0) |
| 73 | >>> inp = mge.tensor(np.arange(0, 16).astype("float32").reshape(1, 1, 4, 4)) |
| 74 | >>> oup = m(inp) |
| 75 | >>> oup.numpy() |
| 76 | array([[[[10., 11.], |
| 77 | [14., 15.]]]], dtype=float32) |
| 78 | |
| 79 | """ |
| 80 | |
| 81 | def forward(self, inp): |
| 82 | return max_pool2d(inp, self.kernel_size, self.stride, self.padding) |
| 83 | |
| 84 | |
| 85 | class AvgPool2d(_PoolNd): |