1 系统环境
硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU
MindSpore版本: 1.9.0
执行模式(PyNative/ Graph): 不设置模式
Python版本: 3.7.5
操作系统平台: 不限
2 报错信息
2.1 问题描述
运行教程中的示例代码出现报错
2.2 报错信息
RuntimeError: The graph generated form MindIR is not support to execute in the PynativeMode, please convert to the GraphMode.
2.3 脚本代码(代码格式,可上传附件)
import mindspore.nn as nn
net = nn.Conv2d(1,1,kernel_size=3,weight_init="ones")
input = Tensor(np.ones([1,1,3,3]).astype(np.float32))
print (input)
ms.export(net,input,file_name="net",file_foemat="MINDIR")
graph = ms.load("net.mindir")
net = nn.GraphCell(graph)
output = net(input)
print(out_put)
3 根因分析
1、在MindSpore1.9.0版本中默认模式更改,该问题是用例没有设置默认导致的,可以在用例中添加设置图模式。
2、未引入mindspore并设置别名ms,需要在开始import mindspore as ms
3、未引入numpy并设置别名np,需要在开始import numpy as np
4、ms.export函数参数名file_foemat拼写错误,需要改成改成file_format
5、print(out_put)中的out_put参数错误,需要改成output
4 解决方案
import mindspore as ms
import numpy as np
import mindspore.nn as nn
net = nn.Conv2d(1,1,kernel_size=3,weight_init="ones")
input = ms.Tensor(np.ones([1,1,3,3]).astype(np.float32))
print(input)
ms.export(net,input,file_name="net",file_foemat="MINDIR")
mindspore.set_context(mode=mindspore.GRAPH_MODE)
graph = ms.load("net.mindir")
net = nn.GraphCell(graph)
output = net(input)
print(out_put)
[[[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]]]
[[[[4. 6. 4.]
[6. 9. 6.]
[4. 6. 4.]]]]