执行时遇到‘context.set_context’报错怎么办

 时间:2026-04-24 11:43:21

1、1. 系统环境:

Hardware Environment(Ascend/GPU/CPU): ALL

Software Environment:

MindSpore version (source or binary): 1.7.0 & Earlier versions 

Python version (e.g., Python 3.7.5): 3.7.6

OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu

GCC/Compiler version (if compiled from source):

2、2. python代码样例

from mindspore import context 

context.set_context(device_target="Ascend")

3、3. 报错信息:

ValueError: For 'context.set_context', package type xxx support 'device_target' type xxx, but got xxx.")

4、4. 原因分析

context.set_context方法为设置脚本运行的后端,有Ascend、GPU、CPU。


Ascend、GPU环境可以运行CPU后端,CPU环境不能运行Ascend、GPU后端,Ascend和GPU互相不能切换。
如果脚本设置的运行后端与脚本运行的硬件不匹配,则会报这个错误。

5、5. 解决方法

查看当前运行的硬件,在context.set_context中设置对应的即可。

6、6. 参考文档

具体可参考可以参考: 配置运行信息

  • windows终端怎么打开
  • 校园网老是间歇性掉线怎么解决?
  • opencv安装教程
  • 手机egde怎么选择地区?
  • Visual Studio如何把C#代码变成可执行程序
  • 热门搜索
    脸上长斑怎么去除 避孕方法有哪些 worked怎么读 治疗颈椎病的方法 爱奇艺怎么了 治疗近视的方法 兰州旅游景点大全介绍 腌萝卜怎么腌好吃又脆 脂肪瘤最新治疗方法 家常川菜