ConvTranspose2d

class ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, dilation=1, groups=1, bias=True, conv_mode='cross_correlation', compute_mode='default', **kwargs)[源代码]

对输入张量进行二维转置卷积。

该模块也称为反卷积或微步卷积。 ConvTranspose2d 可以看作是 Conv2d 操作对自身输入的梯度。

卷积通常降低了输入的大小,而转置卷积工作方式完全相反,在相同的连接方式下,它对较小的输入进行放大来输出。

参数
  • in_channels (int) – 输入数据中的通道数。

  • out_channels (int) – 输出数据中的通道数。

  • kernel_size (Union[int, Tuple[int, int]]) – 空间维度上的权重大小。如果kernel_size 是一个 int, 实际的kernel大小为 (kernel_size, kernel_size).

  • stride (Union[int, Tuple[int, int]]) – stride of the 2D convolution operation. Default: 1.

  • padding (Union[int, Tuple[int, int]]) – size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0.

  • output_padding (Union[int, Tuple[int, int]]) – size of paddings appended to output. Default: 0.

  • dilation (Union[int, Tuple[int, int]]) – dilation of the 2D convolution operation. Default: 1.

  • groups (int) – number of groups into which the input and output channels are divided, so as to perform a grouped convolution. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, in_channels // groups, out_channels // groups, height, width). Default: 1.

  • bias (bool) – wether to add a bias onto the result of convolution. Default: True conv_mode: Supports cross_correlation. Default: cross_correlation.

  • compute_mode (str) – When set to “default”, no special requirements will be placed on the precision of intermediate results. When set to “float32”, “float32” would be used for accumulator and intermediate result, but only effective when input and output are of float16 dtype. Default: ‘default’.

Shape:
  • Input: \((N, C_{in}, H_{in}, W_{in})\) or \((C_{in}, H_{in}, W_{in})\)

  • Output: \((N, C_{out}, H_{out}, W_{out})\) or \((C_{out}, H_{out}, W_{out})\), where

\[H_{out} = (H_{in} - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0] \times (\text{kernel\_size}[0] - 1) + \text{output\_padding}[0] + 1\]
\[W_{out} = (W_{in} - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1] \times (\text{kernel\_size}[1] - 1) + \text{output\_padding}[1] + 1\]
返回

module. The instance of the ConvTranspose2d module.

返回类型

Return type

实际案例

>>> import torch
>>> import megengine
>>> conv_transpose = megengine.module.conv.ConvTranspose2d(3, 64, 3, stride=2, padding=1)
>>> input = megengine.tensor(torch.randn(16, 3, 32, 32))
>>> output = conv_transpose.forward(input)
>>> print(output.numpy().shape)
(16, 64, 63, 63)

注解

  • weight 的shape通常是 (in_channels, out_channels, height, width) , 如果groups 不为 1, shape 将是 (groups, in_channels // groups, out_channels // groups, height, width)

  • bias 的shape通常为 (1, out_channels, *1)