共计 3054 个字符,预计需要花费 8 分钟才能阅读完成。
1 报错形容 1.1 零碎环境 Hardware Environment(Ascend/GPU/CPU): AscendSoftware 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 = “valid”
08 self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode=”valid”)
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(“otput shape: “, y)
1.2.2 报错这里报错信息如下:Traceback (most recent call last):
File “C:/Users/l30026544/PycharmProjects/q2_map/new/I4GYSL.py”, line 20, in <module>
y = net(x).shape
File “C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py”, line 586, in call
out = self.compile_and_run(*args)
File “C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py”, line 964, in compile_and_run
self.compile(*inputs)
File “C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\nn\cell.py”, line 937, in compile
_cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)
File “C:\Users\l30026544\PycharmProjects\q2_map\lib\site-packages\mindspore\common\api.py”, 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 对 Conv2D 的 output 形容,当 pad_mode 为“valid”时,抉择第二组公式代入数据失去 H -out 为 -59. 因而解决的办法有调整传入的参数大小或者将 pad_mode 设置为“same”
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 = “valid”
08 self.conv2d = nn.Conv2d(self.in_channels, self.out_channels, self.kernel_size, pad_mode=”valid”)
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(“otput shape: “, 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 接口