RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 ‘mat2‘ in

RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 ‘mat2’ in call to _th_mm

最近在利用Linux服务器训练强化学习模型,由于是现在CPU上跑的,后来放到了GPU跑,结果出现了上述问题,
在这里插入图片描述

大致意思是object 的 device 类型期望得到的是 cuda 类型,但是实际上的类型确实 cpu 类型

经检查发现虽然device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu"),device是GPU,但是我的模型未放到设备上,如图
在这里插入图片描述

将其改为
在这里插入图片描述

即可。

一般遇到这个错误可以从以下几个方面着手:

  • 模型是否放到了CUDA上 model = model.to(device)

  • 输入数据是否放到了CUDA上 data = data.to(device)

  • 模型内部新建的张量是否放到了CUDA上 state = torch.FloatTensor(state).to(device)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Contents at a Glance Introduction .................................................................................................1 1 Getting Started..............................................................................................5 2 Decompiling Classes...................................................................................13 3 Obfuscating Classes....................................................................................27 4 Hacking Non-Public Methods and Variables of a Class.............................43 5 Replacing and Patching Application Classes .............................................51 6 Using Effective Tracing...............................................................................63 7 Manipulating Java Security ........................................................................69 8 Snooping the Runtime Environment.........................................................75 9 Cracking Code with Unorthodox Debuggers ............................................81 10 Using Profilers for Application Runtime Analysis .....................................89 11 Load-Testing to Find and Fix Scalability Problems..................................105 12 Reverse Engineering Applications............................................................121 13 Eavesdropping Techniques.......................................................................127 14 Controlling Class Loading........................................................................139 15 Replacing and Patching Core Java Classes...............................................149 16 Intercepting Control Flow........................................................................155 17 Understanding and Tweaking Bytecode ..................................................165 18 Total Control with Native Code Patching ...............................................185 19 Protecting Commercial Applications from Hacking................................201 A Commercial Software License ..................................................................227 B Resources...................................................................................................233 C Quiz Answers ............................................................................................239 Index.........................................................................................................249
根据你提供的引用信息,你在运行PyTorch代码时遇到了一个报错:RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'。这个错误是因为你在代码中使用了一个生成器(generator),但是你期望它在GPU上运行,而实际上它在CPU上运行。 根据引用\[1\],你可以通过修改代码中的生成器参数来解决这个问题。根据引用\[2\],你可以在生成器参数中添加一个`device='cuda'`的参数,将生成器指定为在GPU上运行。这样,生成器就会在GPU上运行,而不是在CPU上。 另外,根据引用\[3\],你还可以尝试使用`torch.set_default_tensor_type('torch.cuda.FloatTensor')`将默认的张量类型设置为在GPU上的浮点型。这样,数据加载到GPU上时就不需要再进行`data.cuda()`操作了。 综上所述,你可以尝试在生成器参数中添加`device='cuda'`,或者使用`torch.set_default_tensor_type('torch.cuda.FloatTensor')`来解决这个问题。 #### 引用[.reference_title] - *1* [RuntimeError: Expected a ‘cpudevice type for generator but found cuda](https://blog.csdn.net/qq_41813454/article/details/129812083)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [RuntimeError: Expected a ‘cudadevice type for generator but found ‘cpu](https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/124649651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [RuntimeError:Expected a ‘cudadevice type for generator but found ‘cpu](https://blog.csdn.net/u013114420/article/details/121018262)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wavehaha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值