ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | FAIL | download_stamp > now() - 6 MONTH | 7.6 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://blog.csdn.net/Jane_JU/article/details/124929496 |
| Last Crawled | 2025-08-28 17:27:13 (7 months ago) |
| First Indexed | not set |
| HTTP Status Code | 200 |
| Meta Title | RuntimeError: Invalid device string: ‘0,1,2,3‘_runtimeerror: invalid device string: 'cuda:0,1,2,3-CSDN博客 |
| Meta Description | 文章浏览阅读7.2k次。本文探讨了如何将环境变量CUDA_VISIBLE_DEVICES的设置转换为torch.device,以根据可用资源动态选择GPU或CPU。通过device属性,提高了代码的灵活性和效率。 |
| Meta Canonical | null |
| Boilerpipe Text | null |
| Markdown | # RuntimeError: Invalid device string: ‘0,1,2,3‘
原创 已于 2022-05-23 16:26:57 修改 · 7\.2k 阅读
·   0
· [  4]() ·
CC 4.0 BY-SA版权
版权声明:本文为博主原创文章,遵循 [CC 4.0 BY-SA](http://creativecommons.org/licenses/by-sa/4.0/) 版权协议,转载请附上原文出处链接和本声明。
文章标签:
[\#pytorch](https://so.csdn.net/so/search/s.do?q=pytorch&t=all&o=vip&s=&l=&f=&viparticle=&from_tracking_code=tag_word&from_code=app_blog_art) [\#人工智能](https://so.csdn.net/so/search/s.do?q=%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD&t=all&o=vip&s=&l=&f=&viparticle=&from_tracking_code=tag_word&from_code=app_blog_art) [\#python](https://so.csdn.net/so/search/s.do?q=python&t=all&o=vip&s=&l=&f=&viparticle=&from_tracking_code=tag_word&from_code=app_blog_art)
于 2022-05-23 16:22:07 首次发布
[ ImportError 专栏收录该内容](https://blog.csdn.net/jane_ju/category_11570148.html "ImportError")
5 篇文章
[订阅专栏]()
 本文探讨了如何将环境变量CUDA\_VISIBLE\_DEVICES的设置转换为torch.device,以根据可用资源动态选择GPU或CPU。通过device属性,提高了代码的灵活性和效率。
摘要生成于 [C知道](https://ai.csdn.net/?utm_source=cknow_pc_ai_abstract) ,由 DeepSeek-R1 满血版支持, [前往体验 \>](https://ai.csdn.net/?utm_source=cknow_pc_ai_abstract)

开头加上
import os
os.environ\[‘CUDA\_VISIBLE\_DEVICES’\] = ‘0,1,2,3’


改为:
device = torch.device(‘cuda’ if torch.cuda.is\_available() else ‘cpu’ )


确定要放弃本次机会?
福利倒计时
*:* *:*
 立减 ¥
普通VIP年卡可用
[立即使用](https://mall.csdn.net/vip)
[ Jane\_JU](https://blog.csdn.net/Jane_JU)
[关注]() [关注]()
- [   0]()
点赞
- [ ]()
踩
- [   4]()
收藏
觉得还不错? 一键收藏 
- 
知道了
[ 0](https://blog.csdn.net/Jane_JU/article/details/124929496#commentBox)
评论
- [ 分享]()
[复制链接]()
[分享到 QQ]()
[分享到新浪微博]()
扫一扫
- []()
[ 举报]()
[ 举报]()
[专栏目录]()
[*PyTorch*学习(*2*):torch.*device*](https://blog.csdn.net/tecsai/article/details/137096667)
[tecsai的博客](https://blog.csdn.net/tecsai)
03-28  2676
[torch.*device* 是 *PyTorch* 中的一个类,它指定了张量(Tensor)和模型应该运行在哪个设备上。在深度学习中,通常会使用 GPU 来加速计算,而 torch.*device* 允许你轻松地在 CPU 和 GPU 之间切换。](https://blog.csdn.net/tecsai/article/details/137096667)
[解决报错:*RuntimeError**:* *Invalid* *device* *string**:* ‘cuda:*0*‘](https://devpress.csdn.net/v1/article/detail/121021722)
[零碎@流年絮语 的博客](https://blog.csdn.net/qq_44824148)
10-31  1万+
[错误再现: 出错原因: os.environ\[*'CUDA*\_VISIBLE\_*DEVICE*S'\] = '*0*' *device* = torch.*device*(*'cuda*:*0*' if torch.cuda.is\_available() and use\_cuda else 'cpu') 应修改为: os.environ\[*'CUDA*\_VISIBLE\_*DEVICE*S'\] = '*0*' *device* = torch.*device*(*'cuda*' if torch.cuda.is\_available() and use\_cud](https://devpress.csdn.net/v1/article/detail/121021722)
参与评论 您还未登录,请先 登录 后发表或查看评论
[*pytorch* 报错:*RuntimeError**:* *Invalid* *device* *string**:* ‘*0*‘](https://devpress.csdn.net/v1/article/detail/117971212)
[sazass的博客](https://blog.csdn.net/sazass)
06-17  5235
[具体原因: windows下不支持函数 torch.cuda.set\_*device*(args.gpu),在linux下支持。 因此需要替换这行代码(怎么改不会)。 如下: \# torch.cuda.set\_*device*(args.gpu) \# model = model.cuda(args.gpu) cuda = torch.cuda.is\_available() if cuda*:* model.cuda() ...](https://devpress.csdn.net/v1/article/detail/117971212)
[*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal 最新发布](https://blog.csdn.net/weixin_46535650/article/details/143130672)
[weixin\_46535650的博客](https://blog.csdn.net/weixin_46535650)
10-21  292
[*device* = torch.*device*("cuda*:**1*") \# 这应该改为 cuda*:**0*。然后将对应文件中的gpu数量改为*0*(gpu都是从*0*开始编号的)查看此设备有多少GPU可用(我的是只有一个)我遇到的问题是,GPU数量不够,只有一个。在IDE中可用高级检索功能 ,通过。若是这种,就不需要修改。](https://blog.csdn.net/weixin_46535650/article/details/143130672)
[*python*报错:*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal](https://blog.csdn.net/qq_38397338/article/details/115016086)
[奔跑的小猪仔](https://blog.csdn.net/qq_38397338)
03-19  1809
[只需要在用到*device*的地方,如 torch.Tensor(\[(self.\_dim + *1*) / *2*\], *device*\=self.*device*) 加上这句话即可,表示又cuda就用cuda,没有的话就用cpu *device* = torch.*device*(“cuda*:**0*” if torch.cuda.is\_available() else “cpu”) ps:可以把"cuda*:**0*"换成"cuda*:**1*"试试 ...](https://blog.csdn.net/qq_38397338/article/details/115016086)
[*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously repo](https://yanqianyun.blog.csdn.net/article/details/140780103)
[m0\_73367097的博客](https://blog.csdn.net/m0_73367097)
07-29  2957
[*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously repo](https://yanqianyun.blog.csdn.net/article/details/140780103)
[*Invalid* *device* *string**:* ‘cuda*:* *0*‘尝试解决方法](https://devpress.csdn.net/v1/article/detail/131786926)
[GJmachine\_code的博客](https://blog.csdn.net/GJmachine_code)
07-18  1367
[尝试把中间的空格去了 我就是的。](https://devpress.csdn.net/v1/article/detail/131786926)
[torch.*device*()](https://blog.csdn.net/weixin_42663766/article/details/135407533)
[weixin\_42663766的博客](https://blog.csdn.net/weixin_42663766)
01-05  884
[表示使用第一个可用的GPU。*device* = torch.*device*("cuda" if torch.cuda.is\_available() else "cpu")),也可以是一个整数,表示要使用的GPU设备的索引号。函数,参数可以是字符串,指定要使用的设备(如。](https://blog.csdn.net/weixin_42663766/article/details/135407533)
[编程中的runtime\_error问题](https://blog.csdn.net/weixin_30621711/article/details/97241791)
[weixin\_30621711的博客](https://blog.csdn.net/weixin_30621711)
11-27  469
[前几天在编程中,代码如下: 头文件:ErrorHandlingModule.h //filename*:*ErrorHandlingModule.h \#ifndef ErrorHandlingModule\_H \#define ErrorHandlingModule\_H \#include \<stdexcept\> using namespace std; na...](https://blog.csdn.net/weixin_30621711/article/details/97241791)
[*pytorch* 使用GPU进行神经网络训练出现问题 *RuntimeError**:* CUDA error*:* *invalid* *device* ordinal](https://devpress.csdn.net/v1/article/detail/115165134)
[Childhood\_Sweetheart的博客](https://blog.csdn.net/Childhood_Sweetheart)
03-24  2386
[实验室里有两块GTX*1**0*8*0*Ti 的显卡,一开始只是使用第一块显卡,需要在代码中加入这样一行代码: *device* = torch.*device*("cuda*:**0*" if torch.cuda.is\_available() else "cpu") 但是第一块显卡被使用了,要用第二块显卡,于是我就将代码改为: *device* = torch.*device*("cuda*:**1*" if torch.cuda.is\_available() else "cpu") 但是出现了这样的错误,在网上查阅了资料才发现需要这](https://devpress.csdn.net/v1/article/detail/115165134)
[问题解决 \| *RuntimeError**:* CUDA error*:* *invalid* *device* ordinalCUDA kernel errors](https://devpress.csdn.net/v1/article/detail/136449672)
[weixin\_44649780的博客](https://blog.csdn.net/weixin_44649780)
03-04  5660
[*RuntimeError*(运行时错误): CUDA 错误:设备序号无效CUDA 内核错误可能会在其他 API 调用中异步报告,因此下面的堆栈跟踪可能不正确。为便于调试,可考虑通过 CUDA\_LAUNCH\_BLOCKING=*1*。使用 \`TORCH\_USE\_CUDA\_DSA\` 进行编译,以启用设备端断言。](https://devpress.csdn.net/v1/article/detail/136449672)
[*Python*运行错误:*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal 热门推荐](https://devpress.csdn.net/v1/article/detail/105167947)
[yxh的博客](https://blog.csdn.net/sinat_37422398)
03-28  3万+
[用*pytorch*跑实验需要用到cuda加速,开始默认用的是第*0*块gpu,后来第*0*块cpu被别人占用,所以只能切换到第*1*块gpu。 于是进行了如下修改 *device* = torch.*device*(*'cuda**:**1*' if torch.cuda.is\_available() else 'cpu' ) 但是出现了错误: *RuntimeError**:* CUDA error*:* *invalid* *device* o...](https://devpress.csdn.net/v1/article/detail/105167947)
[已解决:*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously repo](https://alextechvision.blog.csdn.net/article/details/139473439)
[科技改变人类,技术成就未来](https://blog.csdn.net/g310773517)
06-05  2641
[解决*Python*报错:*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously repo](https://alextechvision.blog.csdn.net/article/details/139473439)
[【BUG】已解决*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously](https://yanqianyun.blog.csdn.net/article/details/138339053)
[m0\_73367097的博客](https://blog.csdn.net/m0_73367097)
04-30  3705
[已解决*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal CUDA kernel errors might be asynchronously](https://yanqianyun.blog.csdn.net/article/details/138339053)
[“*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal”解决方法](https://devpress.csdn.net/v1/article/detail/131516761)
[qq\_43826289的博客](https://blog.csdn.net/qq_43826289)
07-03  8498
[参考:https*:*//blog.csdn.net/qxqxqzzz/article/details/*1**0*77*2**0*675。用的*Pytorch*分布式代码,指定的每个节点GPU个数和你指定的GPU序号不对应(我出错的地方就是在这里)用于日常排错,自用。其他小伙伴跟我不一样的错误可以参考我给出博客地址,看看自己到底是哪里除了错误。S就只指定了两个GPU,对应不起来导致的。另:rank表示的结点之间的主、从关系。通过nnodes指定总共使用*1*个节点,通过。指定了该节点启*3*个进程,但是前面。](https://devpress.csdn.net/v1/article/detail/131516761)
[DEBUG-- *RuntimeError**:* CUDA error*:* *invalid* *device* ordinal](https://blog.csdn.net/qq_45736022/article/details/122663779)
[qq\_45736022的博客](https://blog.csdn.net/qq_45736022)
01-24  3411
[在使用以下代碼訓練時會報錯*RuntimeError**:* CUDA error*:* *invalid* *device* ordinal, CUDA\_VISIBLE\_*DEVICE*S=*0,1,2,3*,4,5,6,7 \\ *python* -m torch.distributed.launch \\ --nproc\_per\_node 8 原因是机器的GPU数量和指定的GPU数量不一致,如果机器上只有4个GPU,就需要将代码修改为: CUDA\_VISIBLE\_*DEVICE*S=*0,1,2,3* \\ *python* -m torch.](https://blog.csdn.net/qq_45736022/article/details/122663779)
[cuda常见报错](https://blog.csdn.net/qq_41900846/article/details/128536185)
[qq\_41900846的博客](https://blog.csdn.net/qq_41900846)
01-03  8020
[cuda常见报错](https://blog.csdn.net/qq_41900846/article/details/128536185)
[成功解决*RuntimeError**:* radix\_sort*:* failed on *1*st step*:* cudaError*Invalid**Device**:* *invalid* *device* ordinal](https://devpress.csdn.net/v1/article/detail/121270986)
[qq\_55068938的博客](https://blog.csdn.net/qq_55068938)
11-11  6514
[成功解决*RuntimeError**:* radix\_sort*:* failed on *1*st step*:* cudaError*Invalid**Device**:* *invalid* *device* ordinal 项目场景: linux下detectron*2*异常解决 问题描述: 最近用到了detectron*2*来进行实例分割,遇到了一个非常奇葩的问题,根据detectron*2*的要求,我的*pytorch*版本选择了*1*.8.*0*。我先在windows跑了几天,服务器回来后,打算移植到服务器上,同样也安装了*1*.8.*0*的*pytorch*。](https://devpress.csdn.net/v1/article/detail/121270986)
[*RuntimeError**:* *Invalid* *device* *string**:* \&\#*3*9;*0*\&\#*3*9;](https://wenku.csdn.net/answer/7d3a76e4e8714a1181c478e644d82e96)
04-03
[This error usually occurs when a *device* *string* is not recognized or is *invalid*. Here are some possible causes and solutions*:* *1*. Incorrect *device* *string* format*:* Make sure that the *device* *string* ...](https://wenku.csdn.net/answer/7d3a76e4e8714a1181c478e644d82e96)
[](https://blog.csdn.net/Jane_JU)
[Jane\_JU](https://blog.csdn.net/Jane_JU "Jane_JU")
博客等级 
码龄5年
[15 原创](https://blog.csdn.net/Jane_JU)
8
点赞
50
收藏
322
粉丝
[关注]()
[私信](https://im.csdn.net/chat/Jane_JU)
[](https://mp.csdn.net/edit?utm_source=blog)
### 热门文章
- [由于目标计算机积极拒绝,无法连接。 已解决  11472](https://blog.csdn.net/Jane_JU/article/details/107817242)
- [全局特征和局部特征  8863](https://blog.csdn.net/Jane_JU/article/details/121804972)
- [RuntimeError: Invalid device string: ‘0,1,2,3‘  7294](https://blog.csdn.net/Jane_JU/article/details/124929496)
- [完全小白配置esp8266环境的艰难过程ing  3266](https://blog.csdn.net/Jane_JU/article/details/109190561)
- [抑制背景干扰的行人重识别方法  1855](https://blog.csdn.net/Jane_JU/article/details/123069720)
### 分类专栏
- [ ImportError](https://blog.csdn.net/jane_ju/category_11570148.html)
5篇
- [ 行人重识别](https://blog.csdn.net/jane_ju/category_11527270.html)
6篇
- [ python](https://blog.csdn.net/jane_ju/category_10264267.html)
2篇
- [ 综述阅读](https://blog.csdn.net/jane_ju/category_11646755.html)
1篇
- [ FasterRCNN](https://blog.csdn.net/jane_ju/category_11497632.html)
1篇
- [ socket](https://blog.csdn.net/jane_ju/category_10625770.html)
- [ 爬虫](https://blog.csdn.net/jane_ju/category_10264268.html)
1篇
[展开全部 ]() [收起 ]()
上一篇:
[UnboundLocalError: local variable ‘cls\_num‘ referenced before assignment](https://blog.csdn.net/Jane_JU/article/details/124827745)
### 最新评论
- [全局特征和局部特征](https://blog.csdn.net/Jane_JU/article/details/121804972#comments_24706403)
[501摆烂达人:](https://blog.csdn.net/ljg729350527) 什么是空间信息
### 大家在看
- [基于multisim的多级放大电路仿真  68](https://blog.csdn.net/m0_57179159/article/details/150659978)
- [二分搜索(非递归以及递归实现)  179](https://blog.csdn.net/jester666/article/details/150947446)
- [【Ruoyi 解密 - 12. JDK17的新特性】------ 从Java 8 到 Java 17:向Scala看齐的“简洁革命”,同宗JVM下的效率狂飙  1](https://blog.csdn.net/weixin_45091011/article/details/150967172)
- [基于multisim高低电平报警器的设计](https://blog.csdn.net/m0_57179159/article/details/150641158)
- [嵌入式学习硬件I.MX6ULL(十) LCD](https://blog.csdn.net/2202_75781451/article/details/150884272)
### 最新文章
- [UnboundLocalError: local variable ‘cls\_num‘ referenced before assignment](https://blog.csdn.net/Jane_JU/article/details/124827745)
- [ImportError: cannot import name ‘\_validate\_lengths‘ 解决方法](https://blog.csdn.net/Jane_JU/article/details/124803099)
- [0x00611F29 处有未经处理的异常(在 suanfaxunlian.exe 中): 0xC00000FD: Stack overflow (参数: 0x00000000, 0x00622000)](https://blog.csdn.net/Jane_JU/article/details/123755458)
[2022年11篇](https://blog.csdn.net/Jane_JU?type=blog&year=2022&month=05)
[2021年2篇](https://blog.csdn.net/Jane_JU?type=blog&year=2021&month=12)
[2020年3篇](https://blog.csdn.net/Jane_JU?type=blog&year=2020&month=12)
### 目录
展开全部 
收起 
### 目录
展开全部 
收起 
上一篇:
[UnboundLocalError: local variable ‘cls\_num‘ referenced before assignment](https://blog.csdn.net/Jane_JU/article/details/124827745)
### 分类专栏
- [ ImportError](https://blog.csdn.net/jane_ju/category_11570148.html)
5篇
- [ 行人重识别](https://blog.csdn.net/jane_ju/category_11527270.html)
6篇
- [ python](https://blog.csdn.net/jane_ju/category_10264267.html)
2篇
- [ 综述阅读](https://blog.csdn.net/jane_ju/category_11646755.html)
1篇
- [ FasterRCNN](https://blog.csdn.net/jane_ju/category_11497632.html)
1篇
- [ socket](https://blog.csdn.net/jane_ju/category_10625770.html)
- [ 爬虫](https://blog.csdn.net/jane_ju/category_10264268.html)
1篇
[展开全部 ]() [收起 ]()
### 目录
评论 
被折叠的 条评论 [为什么被折叠?](https://blogdev.blog.csdn.net/article/details/122245662) [到【灌水乐园】发言](https://bbs.csdn.net/forums/FreeZone)
[查看更多评论]()
添加红包

下一步

知道了
![]()
成就一亿技术人\!
领取后你会自动成为博主和红包主的粉丝 [规则]()
[](https://blog.csdn.net/Jane_JU/article/details/124929496)
hope\_wisdom
发出的红包
实付元
[使用余额支付]()
 点击重新获取
扫码支付
钱包余额 0

抵扣说明:
1\.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。
[余额充值](https://i.csdn.net/#/wallet/balance/recharge)
![]() |
| Readable Markdown | null |
| Shard | 8 (laksa) |
| Root Hash | 14895566225899102208 |
| Unparsed URL | net,csdn!blog,/Jane_JU/article/details/124929496 s443 |