关于机器学习:MindSpore报错-Data-type-conversion-of-Parameter-is-not-supporte

39次阅读

共计 3278 个字符,预计需要花费 9 分钟才能阅读完成。

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 <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…

起因剖析咱们看报错信息,在 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