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

1 报错描述

1.1 系统环境

Hardware Environment(Ascend/GPU/CPU): Ascend
Software 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 <module>
    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/zh-CN/master/note/operator_list_implicit.html.

原因分析

我们看报错信息,在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上进行了说明:

因此, 如果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)

此时执行成功,输出如下:

output: (Tensor(shape=[2, 2], dtype=Float32, value=
[[ 5.95575690e-01,  3.89676481e-01],
 [ 9.85252112e-02,  4.88201708e-01]]), Tensor(shape=[2, 2], dtype=Float32, 
 value=[[ 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接口

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值