# 使用mindspore中Conv2dTranspose的outputpadding时，设置has\_bias=True时失效

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): Ascend/GPU/CPU

软件环境: MindSpore 版本: 2.0.0

执行模式（静态图/动态图）： 不限

Python 版本: 3.7.6

操作系统平台: linux

**2. 报错信息**

2.1 问题描述

使用随机生成的tensor in\_t经过Conv2d生成缩小后的tensor然后再用Conv2dTranspose恢复原始大小，但设置has\_bias=True后 output\_padding=1的设置失效了。

2.2 脚本信息

```
import mindspore as ms
import mindspore.nn as nn
from mindspore import Tensor
import mindspore.common.initializer as init

in_t = Tensor(shape = (1,256,32,32),dtype = ms.float32,init = init.Normal())

conv = nn.Conv2d(256,256,3,2,padding = 3//2,pad_mode = "pad",has_bias = True)
conv_t = nn.Conv2dTranspose(256,256,3,padding = 1,stride = 2,pad_mode = "pad",output_padding = 1,has_bias = True)
conv_tt = nn.Conv2dTranspose(256,256,3,padding = 1,stride = 2,pad_mode = "pad",output_padding = 1)

out1 = conv(in_t)
out2 = conv_t(out1)
out3 = conv_tt(out1)
print(out1.shape,out2.shape,out3.shape)
```

**3. 根因分析**

上面的结果输出是\(1, 256, 16, 16\) \(1, 256, 31, 31\) \(1, 256, 32, 32\)

设置has\_bias=True后 output\_padding=1的设置失效了，于是看下nn.Conv2dTranspose的网络定义。

```
   def construct(self, x):
        n, _, h, w = self.shape(x)
        h_out = _deconv_output_length(self.is_valid, self.is_same, self.is_pad, h, self.kernel_size[0],
                                      self.stride[0], self.dilation[0], self.padding_top + self.padding_bottom)
        w_out = _deconv_output_length(self.is_valid, self.is_same, self.is_pad, w, self.kernel_size[1],
                                      self.stride[1], self.dilation[1], self.padding_left + self.padding_right)
        if self.has_bias:
            return self.bias_add(self.conv2d_transpose(x, self.weight, (n, self.out_channels, h_out, w_out)),
                                 self.bias)
        conv2d_trans_ret = self.conv2d_transpose(x, self.weight, (n, self.out_channels, h_out, w_out))
        if isinstance(self.output_padding, tuple):
            if self.output_padding[0] < 0 or self.output_padding[0] >= max(self.dilation[0], self.stride[0]):
                raise ValueError("output_padding[0] must be in range of [0, max(stride_h, dilation_h)).")
            if self.output_padding[1] < 0 or self.output_padding[1] >= max(self.dilation[1], self.stride[1]):
                raise ValueError("output_padding[1] must be in range of [0, max(stride_w, dilation_w)).")
            if not self.is_pad and (self.output_padding[0] > 0 or self.output_padding[1] > 0):
                raise ValueError("when output_padding is not zero, pad_mode must be 'pad'")

            pad = P.Pad(paddings=((0, 0), (0, 0), (0, self.output_padding[0]), (0, self.output_padding[1])))
            return pad(conv2d_trans_ret)

        if self.output_padding == 0:
            return conv2d_trans_ret

        if self.output_padding < 0 or self.output_padding >= max(self.dilation[0], self.stride[0]):
            raise ValueError("output_padding must be in range of [0, max(stride_h, dilation_h)).")
        if self.output_padding < 0 or self.output_padding >= max(self.dilation[1], self.stride[1]):
            raise ValueError("output_padding must be in range of [0, max(stride_w, dilation_w)).")
        if not self.is_pad and self.output_padding > 0:
            raise ValueError("when output_padding is not zero, pad_mode must be 'pad'")
        pad = P.Pad(paddings=((0, 0), (0, 0), (0, self.output_padding), (0, self.output_padding)))
        return pad(conv2d_trans_ret)
```

从上面的代码上可以看出

```
        if self.has_bias:
            return self.bias_add(self.conv2d_transpose(x, self.weight, (n, self.out_channels, h_out, w_out)),
                                 self.bias)
```

当has\_bias=True 时直接就return了，但是下方还有关于output\_padding的相关处理流程没有走。

**4. 解决方案**

修改代码，has\_bias=True的时候不直接return，而是继续走完下面的流程。

```
        if self.has_bias:
            conv2d_trans_ret = self.bias_add(self.conv2d_transpose(x, self.weight, (n, self.out_channels, h_out, w_out)),
                                 self.bias)
        else:
            conv2d_trans_ret = self.conv2d_transpose(x, self.weight, (n, self.out_channels, h_out, w_out))
```

