r"""Applies a 2D transposed convolution over an input tensor. This module is also known as a deconvolution or a fractionally-strided convolution. :class:`ConvTranspose2d` can be seen as the gradient of :class:`Conv2d` operation with respect to its input. Convolution usually reduces
| 580 | |
| 581 | |
| 582 | class ConvTranspose2d(_ConvNd): |
| 583 | r"""Applies a 2D transposed convolution over an input tensor. |
| 584 | |
| 585 | This module is also known as a deconvolution or a fractionally-strided convolution. |
| 586 | :class:`ConvTranspose2d` can be seen as the gradient of :class:`Conv2d` operation |
| 587 | with respect to its input. |
| 588 | |
| 589 | Convolution usually reduces the size of input, while transposed convolution works |
| 590 | the opposite way, transforming a smaller input to a larger output while preserving the |
| 591 | connectivity pattern. |
| 592 | |
| 593 | Args: |
| 594 | in_channels(int): number of input channels. |
| 595 | out_channels(int): number of output channels. |
| 596 | kernel_size(Union[int, Tuple[int, int]]): size of weight on spatial dimensions. If ``kernel_size`` is |
| 597 | an :class:`int`, the actual kernel size would be |
| 598 | ``(kernel_size, kernel_size)``. |
| 599 | stride(Union[int, Tuple[int, int]]): stride of the 2D convolution operation. Default: 1. |
| 600 | padding(Union[int, Tuple[int, int]]): size of the paddings added to the input on both sides of its |
| 601 | spatial dimensions. Only zero-padding is supported. Default: 0. |
| 602 | output_padding(Union[int, Tuple[int, int]]): size of paddings appended to output. Default: 0. |
| 603 | dilation(Union[int, Tuple[int, int]]): dilation of the 2D convolution operation. Default: 1. |
| 604 | groups(int): number of groups into which the input and output channels are divided, |
| 605 | so as to perform a ``grouped convolution``. When ``groups`` is not 1, |
| 606 | ``in_channels`` and ``out_channels`` must be divisible by groups, |
| 607 | and the shape of weight should be ``(groups, in_channels // groups, |
| 608 | out_channels // groups, height, width)``. Default: 1. |
| 609 | bias(bool): wether to add a bias onto the result of convolution. Default: True |
| 610 | conv_mode: Supports `cross_correlation`. Default: `cross_correlation`. |
| 611 | compute_mode(str): When set to "default", no special requirements will be |
| 612 | placed on the precision of intermediate results. When set to "float32", |
| 613 | "float32" would be used for accumulator and intermediate result, but only |
| 614 | effective when input and output are of float16 dtype. Default: 'default'. |
| 615 | |
| 616 | Shape: |
| 617 | - Input: :math:`(N, C_{in}, H_{in}, W_{in})` or :math:`(C_{in}, H_{in}, W_{in})` |
| 618 | - Output: :math:`(N, C_{out}, H_{out}, W_{out})` or :math:`(C_{out}, H_{out}, W_{out})`, where |
| 619 | |
| 620 | .. math:: |
| 621 | H_{out} = (H_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0] |
| 622 | \times (\text{kernel\_size}[0] - 1) + \text{output\_padding}[0] + 1 |
| 623 | .. math:: |
| 624 | W_{out} = (W_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1] |
| 625 | \times (\text{kernel\_size}[1] - 1) + \text{output\_padding}[1] + 1 |
| 626 | |
| 627 | Returns: |
| 628 | Return type: module. The instance of the ``ConvTranspose2d`` module. |
| 629 | |
| 630 | Examples: |
| 631 | >>> import torch |
| 632 | >>> import megengine |
| 633 | >>> conv_transpose = megengine.module.conv.ConvTranspose2d(3, 64, 3, stride=2, padding=1) |
| 634 | >>> input = megengine.tensor(torch.randn(16, 3, 32, 32)) |
| 635 | >>> output = conv_transpose.forward(input) |
| 636 | >>> print(output.numpy().shape) |
| 637 | (16, 64, 63, 63) |
| 638 | |
| 639 |
no outgoing calls