MindSpore报错 Data type conversion of 'Parameter' is not supporte

1 报错描述1.1 系统环境Hardware Environment(Ascend/GPU/CPU): AscendSoftware Environment:– MindSpore version (source or binary): 1.8.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 脚本训练脚本是通过构建ApplyPowerSign的单算子网络,根据AddSign算法更新可变Tensor。脚本如下: 01 class Net(nn.Cell):
02 def __init__(self):
03 super(Net, self).__init__()
04 self.apply_power_sign = ops.ApplyPowerSign()
05 self.var = Parameter(Tensor(np.array([[0.6, 0.4],
06 [0.1, 0.5]]).astype(np.float16)), name="var")
07 self.m = Parameter(Tensor(np.array([[0.6, 0.5],
08 [0.2, 0.6]]).astype(np.float32)), name="m")
09 self.lr = 0.001
10 self.logbase = np.e
11 self.sign_decay = 0.99
12 self.beta = 0.9
13 def construct(self, grad):
14 out = self.apply_power_sign(self.var, self.m, self.lr, self.logbase,
15 self.sign_decay, self.beta, grad)
16 return out
17
18 net = Net()
19 grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
20 output = net(grad)
21 print(output)
1.2.2 报错这里报错信息如下:Traceback (most recent call last):
File "ApplyPowerSign.py", line 27, in

output = net(grad)

File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 573, in call
out = self.compile_and_run(*args)

File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 956, in compile_and_run
self.compile(*inputs)

File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/nn/cell.py", line 929, in compile
_cell_graph_executor.compile(self, *inputs, phase=self.phase, auto_parallel_mode=self._auto_parallel_mode)

File "/root/archiconda3/envs/lilinjie_high/lib/python3.7/site-packages/mindspore/common/api.py", line 1063, in compile
result = self._graph_executor.compile(obj, args_list, phase, self._use_vm_mode())

RuntimeError: Data type conversion of 'Parameter' is not supported, so data type float16 cannot be converted to data type float32 automatically.
For more details, please refer at https://www.mindspore.cn/docs...
原因分析我们看报错信息,在AttributeError中,写到RuntimeError: Data type conversion of ‘Parameter’ is not supported, so data type float16 cannot be converted to data type float32 automatically,意思是Parameter数据类型不能发生转换。这点关于Parameter的使用在官网API上进行了说明:
MindSpore报错 Data type conversion of 'Parameter' is not supporte
文章图片

因此, 如果Parameter作为网络的部分输入, 需要在构图阶段将其数据类型确定, 不支持在中途对其进行数据类型转换。2 解决方法基于上面已知的原因,很容易做出如下修改: 01 class Net(nn.Cell):
02 def __init__(self):
03 super(Net, self).__init__()
04 self.apply_power_sign = ops.ApplyPowerSign()
05 self.var = Parameter(Tensor(np.array([[0.6, 0.4],
06 [0.1, 0.5]]).astype(np.float32)), name="var")
07 self.m = Parameter(Tensor(np.array([[0.6, 0.5],
08 [0.2, 0.6]]).astype(np.float32)), name="m")
09 self.lr = 0.001
10 self.logbase = np.e
11 self.sign_decay = 0.99
12 self.beta = 0.9
13 def construct(self, grad):
14 out = self.apply_power_sign(self.var, self.m, self.lr, self.logbase,
15 self.sign_decay, self.beta, grad)
16 return out
17
18 net = Net()
19 grad = Tensor(np.array([[0.3, 0.7], [0.1, 0.8]]).astype(np.float32))
20 output = net(grad)
21 print(output)
【MindSpore报错 Data type conversion of 'Parameter' is not supporte】此时执行成功,输出如下:output: (Tensor(shape=[2, 2], dtype=Float32, value=https://www.it610.com/article/
[[ 5.95575690e-01, 3.89676481e-01],
[ 9.85252112e-02, 4.88201708e-01]]), Tensor(shape=[2, 2], dtype=Float32,
value=https://www.it610.com/article/[[ 5.70000052e-01, 5.19999981e-01],
[ 1.89999998e-01, 6.20000064e-01]]))
3 总结定位报错问题的步骤:1、找到报错的用户代码行:20 output = net(grad); 2、 根据日志报错信息中的关键字,缩小分析问题的范围Data type conversion of ‘Parameter’ is not supported, so data type float16 cannot be converted to data type float32 automatically. ; 3、需要重点关注变量定义、初始化的正确性。4 参考文档4.1 ApplyPowerSign算子API接口

    推荐阅读