1 系统环境
硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU
MindSpore版本: mindspore=2.0.0
执行模式(PyNative/ Graph):不限
Python版本: Python=3.7
操作系统平台: 不限
2 报错信息
2.1 问题描述
mindspore.numpy.sqrt在图模式下 int输入结果不对,在pynative模式报错。两种模式结果不一致。全部报错,或者全部为 2.236
2.2 脚本代码
import mindspore as ms
import mindspore.nn as nn
class TestModel(nn.Cell):
def construct(self, x):
x = ms.numpy.sqrt(x.shape[-1])
return x
inputs = ms.Tensor([1, 2, 3, 4, 5])
model = TestModel()
ms.set_context(mode=0)
print(model(inputs))
ms.set_context(mode=1)
print(model(inputs))
3 根因分析
整形输入目前算子不支持,所以需要更改输入类型。又在construct里面的x.shape[-1]会将数据转为整形,所以我们需要使用tensor里面的value方法将数据提出来并且保持数据类型。
4 解决方案
修改对应的数据类型即可输出正确的结果。
import mindspore as ms
import mindspore.nn as nn
class TestModel(nn.Cell):
def construct(self, x):
x = ms.numpy.sqrt(x[-1].value())
return x
inputs = ms.Tensor([1, 2, 3, 4, 5], ms.float32)
model = TestModel()
ms.set_context(mode=0)
print(model(inputs))
ms.set_context(mode=1)
print(model(inputs))
