# 报错： Conv2D第三维输出数据类型必须是正整数或者SHP\_ANY

**1. 报错描述**

1.1 系统环境

```
Hardware Environment(Ascend/GPU/CPU): Ascend Software Environment: -- MindSpore version (source or binary): 1.6.0 -- Python version (e.g., Python 3.7.5): 3.7.6 -- OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 4.15.0-74-generic -- GCC/Compiler version (if compiled from source):
```

1.2 基本信息

1.2.1 脚本

训练脚本是通过构建Conv2D的单算子网络，来实现2d卷积操作的。脚本如下：

```
 01 class Net(nn.Cell):
 02     def __init__(self, in_channels, out_channels, kernel_size):
 03         super(Net, self).__init__()
 04         self.in_channels = in_channels
 05         self.out_channels = out_channels
 06         self.kernel_size = kernel_size
 07         # self.pad_mode = &quot;valid&quot;
 08         self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode=&quot;valid&quot;)
 09 
 10     def construct(self, x):
 11         return self.conv2d(x)
 12 
 13 net = Net(in_channels=2, out_channels=10, kernel_size=64)
 14 x = Tensor(np.random.randn(1, 2, 4, 64), mindspore.float32)
 15 y = net(x).shape
 16 print(&quot;otput shape: &quot;, y)
```

1.2.2 报错

这里报错信息如下：

```
Traceback (most recent call last):
  File &quot;C:/Users/user1/PycharmProjects/q2_map/new/I4GYSL.py&quot;, line 20, in &lt;module&gt;
    y = net(x).shape
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py&quot;, line 586, in __call__
    out = self.compile_and_run(*args)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py&quot;, line 964, in compile_and_run
    self.compile(*inputs)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py&quot;, line 937, in compile
    _cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
  File &quot;C:\Users\user1\PycharmProjects\q2_map\lib\site-packages\mindspore\common\api.py&quot;, line 1006, in compile
    result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())
ValueError: build\mindspore\merge\mindspore\core\ops_merge.cc:6605 CheckShapeAnyAndPositive] Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59
WARNING: Logging before InitGoogleLogging() is written to STDERR
[CRITICAL] CORE(2540,1,?):2022-6-28 21:12:34 [build\mindspore\merge\mindspore\core\ops_merge.cc:6605] CheckShapeAnyAndPositive] Conv2D output_shape shape element [2] must be positive integer or SHP_ANY, but got -59
```

原因分析

我们看报错信息，在ValueError中，写到Conv2D output\_shape shape element \[2\] must be positive integer or SHP\_ANY, but got -59，意思是Conv2d的输出形状的第三维也就是H-out应该是大于0的整数或者-1， 而实际得到的是-59. 结合官网API对Conv2 D的output描述，当pad\_mode为“valid”时，选择第二组公式代入数据得到H-out为-59. 因此解决的方法有调整传入的参数大小或者将pad\_mode设置为“same”

![](figures/9-78.png)

**2. 解决方法**

基于上面已知的原因，很容易做出如下修改：

```
 01 class Net(nn.Cell):
 02     def __init__(self, in_channels, out_channels, kernel_size):
 03         super(Net, self).__init__()
 04         self.in_channels = in_channels
 05         self.out_channels = out_channels
 06         self.kernel_size = kernel_size
 07         # self.pad_mode = &quot;valid&quot;
 08         self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode=&quot;valid&quot;)
 09 
 10     def construct(self, x):
 11         return self.conv2d(x)
 12 
 13 net = Net(in_channels=2, out_channels=10, kernel_size=64)
 14 x = Tensor(np.random.randn(1, 2, 64, 64), mindspore.float32)
 15 y = net(x).shape
 16 print(&quot;otput shape: &quot;, y)
```

此时执行成功，输出如下：

```
otput shape: (1, 10, 1, 1)
```

**3. 总结**

定位报错问题的步骤：

1、找到报错的用户代码行：y = net\(x\).shape;

2、 根据日志报错信息中的关键字，缩小分析问题的范围Conv2D output\_shape shape element \[2\] must be positive integer or SHP\_ANY, but got -59 ;

3、需要重点关注变量定义、初始化的正确性。

**4. 参考文档**

4.1  [Conv2D算子API接口](https://www.mindspore.cn/docs/en/r1.7/api_python/nn/mindspore.nn.Conv2d.html?highlight=conv2d)

