r"""Applies a 2D average pooling over an input. For instance, given an input of the size :math:`(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 de
| 83 | |
| 84 | |
| 85 | class AvgPool2d(_PoolNd): |
| 86 | r"""Applies a 2D average pooling over an input. |
| 87 | |
| 88 | For instance, given an input of the size :math:`(N, C, H_{\text{in}}, W_{\text{in}})` and |
| 89 | :attr:`kernel_size` :math:`(kH, kW)`, this layer generates the output of |
| 90 | the size :math:`(N, C, H_{\text{out}}, W_{\text{out}})` through a process described as: |
| 91 | |
| 92 | .. math:: |
| 93 | |
| 94 | out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1} |
| 95 | input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n) |
| 96 | |
| 97 | If :attr:`padding` is non-zero, then the input is implicitly zero-padded on |
| 98 | both sides for :attr:`padding` number of points. |
| 99 | |
| 100 | Args: |
| 101 | kernel_size(Union[int, Tuple[int, int]]): the size of the window. |
| 102 | stride(Union[int, Tuple[int, int]]): the stride of the window. Default value is ``kernel_size``. |
| 103 | padding(Union[int, Tuple[int, int]]): implicit zero padding to be added on both sides.Default: 0. |
| 104 | mode(str): whether to include the padding values while calculating the average, set |
| 105 | to "average" will do counting. |
| 106 | Default: "average_count_exclude_padding" |
| 107 | |
| 108 | Shape: |
| 109 | - Input: :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`. |
| 110 | - Output: :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, where |
| 111 | |
| 112 | .. math:: |
| 113 | H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[0] - |
| 114 | \text{kernel\_size}[0]}{\text{stride}[0]} + 1\right\rfloor |
| 115 | |
| 116 | .. math:: |
| 117 | W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[1] - |
| 118 | \text{kernel\_size}[1]}{\text{stride}[1]} + 1\right\rfloor |
| 119 | |
| 120 | Returns: |
| 121 | Return type: module. The instance of the ``AvgPool2d`` module. |
| 122 | |
| 123 | Examples: |
| 124 | >>> import numpy as np |
| 125 | >>> m = M.AvgPool2d(kernel_size=2, stride=2, padding=[1,0], mode="average") |
| 126 | >>> inp = mge.tensor(np.arange(1 * 1 * 3 * 4).astype(np.float32).reshape(1, 1, 3, 4)) |
| 127 | >>> output = m(inp) |
| 128 | >>> output |
| 129 | Tensor([[[[0.25 1.25] |
| 130 | [6.5 8.5 ]]]], device=xpux:0) |
| 131 | |
| 132 | """ |
| 133 | |
| 134 | def __init__( |
| 135 | self, |
| 136 | kernel_size: Union[int, Tuple[int, int]], |
| 137 | stride: Union[int, Tuple[int, int]] = None, |
| 138 | padding: Union[int, Tuple[int, int]] = 0, |
| 139 | mode: str = "average_count_exclude_padding", |
| 140 | **kwargs |
| 141 | ): |
| 142 | super(AvgPool2d, self).__init__(kernel_size, stride, padding, **kwargs) |