r"""Applies a 2D convolution over an input tensor. For instance, given an input of the size :math:`(N, C_{\text{in}}, H, W)`, this layer generates an output of the size :math:`(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})` through the process described as below: .. math::
| 259 | |
| 260 | |
| 261 | class Conv2d(_ConvNd): |
| 262 | r"""Applies a 2D convolution over an input tensor. |
| 263 | |
| 264 | For instance, given an input of the size :math:`(N, C_{\text{in}}, H, W)`, |
| 265 | this layer generates an output of the size |
| 266 | :math:`(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})` through the |
| 267 | process described as below: |
| 268 | |
| 269 | .. math:: |
| 270 | \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + |
| 271 | \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k) |
| 272 | |
| 273 | where :math:`\star` is the valid 2D cross-correlation operator, |
| 274 | :math:`N` is batch size, :math:`C` denotes number of channels, |
| 275 | :math:`H` is height of input planes in pixels, and :math:`W` is |
| 276 | width in pixels. |
| 277 | |
| 278 | In general, output feature maps' shapes can be inferred as follows: |
| 279 | |
| 280 | input: :math:`(N, C_{\text{in}}, H_{\text{in}}, W_{\text{in}})` |
| 281 | |
| 282 | output: :math:`(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}})` where |
| 283 | |
| 284 | .. math:: |
| 285 | \text{H}_{out} = \lfloor \frac{\text{H}_{in} + 2 * \text{padding[0]} - |
| 286 | \text{dilation[0]} * (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1 \rfloor |
| 287 | |
| 288 | .. math:: |
| 289 | \text{W}_{out} = \lfloor \frac{\text{W}_{in} + 2 * \text{padding[1]} - |
| 290 | \text{dilation[1]} * (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1 \rfloor |
| 291 | |
| 292 | When `groups == in_channels` and `out_channels == K * in_channels`, |
| 293 | where K is a positive integer, this operation is also known as depthwise |
| 294 | convolution. |
| 295 | |
| 296 | In other words, for an input of size :math:`(N, C_{\text{in}}, H_{\text{in}}, W_{\text{in}})`, |
| 297 | a depthwise convolution with a depthwise multiplier `K`, can be constructed |
| 298 | by arguments :math:`(in\_channels=C_{\text{in}}, out\_channels=C_{\text{in}} \times K, ..., groups=C_{\text{in}})`. |
| 299 | |
| 300 | Args: |
| 301 | in_channels(int): number of input channels. |
| 302 | out_channels(int): number of output channels. |
| 303 | kernel_size(Union[int, Tuple[int, int]]): size of weight on spatial dimensions. If kernel_size is |
| 304 | an :class:`int`, the actual kernel size would be |
| 305 | ``(kernel_size, kernel_size)``. |
| 306 | stride(Union[int, Tuple[int, int]]): stride of the 2D convolution operation. Default: 1. |
| 307 | padding(Union[int, Tuple[int, int]]): size of the paddings added to the input on both sides of its |
| 308 | spatial dimensions. Default: 0. |
| 309 | dilation(Union[int, Tuple[int, int]]): dilation of the 2D convolution operation. Default: 1. |
| 310 | groups(int): number of groups into which the input and output channels are divided, |
| 311 | so as to perform a ``grouped convolution``. When ``groups`` is not 1, |
| 312 | ``in_channels`` and ``out_channels`` must be divisible by ``groups``, |
| 313 | and the shape of weight should be ``(groups, out_channel // groups, |
| 314 | in_channels // groups, height, width)``. Default: 1. |
| 315 | bias(bool): whether to add a bias onto the result of convolution. Default: True. |
| 316 | conv_mode(str): supports `cross_correlation`. Default: `cross_correlation`. |
| 317 | compute_mode(str): when set to "default", no special requirements will be |
| 318 | placed on the precision of intermediate results. When set to "float32", |
no outgoing calls