栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

关于pytorch tensor/params没有放在同一个设备上的经验

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于pytorch tensor/params没有放在同一个设备上的经验

前言

用pytorch做深度学习时候,全部tensor和变量都要放到同一个设备上, i.e. GPU or CPU。否则会报一下错误:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_mm)

出现以上bug,目前我遇到的有两种情况:
(1)存在tensor没有放到GPU上,比较多的是中途生成的mask。通过print(tensor.is_cuda)对可以tensor进行测试,返回False说明没有在GPU上。
(2)模型参数没有放到GPU上,比较多的情况是以list形式生成多个Module子类对象时。这种情况下注意要使用nn.ModuleList([layer() for _ in range(n)])而不是普通的list。前者会自动讲模型参数注册进网络并放置于整个网络所处的设备,而后者不会。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738001.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号