1 报错描述
1.1 系统环境
Hardware Environment(Ascend/GPU/CPU): GPU
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的单算子网络,对输入张量计算二维卷积。脚本如下:
01 class Net(nn.Cell):
02 def __init__(self):
03 super(Net, self).__init__()
04 self.kernel = np.random.randn(64, 3, 7, 7).astype(np.float32)
05 self.conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=1, padding=0,weight_init=Tensor(self.kernel))
06
07 def construct(self, x):
08 out = self.conv(x)
09 return out
10
11 net = Net()
12 x = Tensor(np.ones((32, 224, 112, 3, 5), dtype=np.float32), mindspore.float32)
13 out = net(x)
14 print('out',out.shape)
1.2.2 报错
这里报错信息如下:
Traceback (most recent call last):
File "demo.py", line 11, in <module>
out = net(x)
…
ValueError: mindspore/core/utils/check_convert_utils.cc:395 CheckInteger] For primitive[Conv2D], the x shape size must be equal to 4, but got 5.
The function call stack (See file ' /demo/rank_0/om/analyze_fail.dat' for more details):
\# 0 In file demo.py(08)
out = self.conv(x)
原因分析
在MindSpore 1.6版本,在construct中创建和使用Tensor,如在脚本中第8行代码所示。
接着看报错信息,在ValueError中,写到the x shape size must be equal to 4, but got 5,意思是传的输入的shape为5,但是你需要传入的是4维数据,其实由weight_init参数便可知传入维度数据为4维,并且该参数的维度应该和in_channels、out_channels、kernel_size对应,在官网中对conv2d算子也做了详细的传参说明。`
检查代码发现,12行代码x的维度为确实不等于4,此时需要检查传入值或者查看网络架构是否有问题。
2 解决方法
基于上面已知的原因,很容易做出如下修改:
此时执行成功,输出如下:
out (32, 64, 224, 112)
3 总结
定位报错问题的步骤:
1、找到报错的用户代码行:out = self.conv(x);
2、 根据日志报错信息中的关键字,缩小分析问题的范围: the x shape size must be equal to 4, but got 5;
3、 需要重点关注变量定义、初始化的正确性;
其中,在1.3版本以前,卷积核只支持[1~63],不支持超过该范围的卷积操作,在后续版本已不在有该限制。