How to fix #39;Input and hidden tensors are not at the same device#39; in pytorch(如何修复pytorch中的“输入和隐藏张量不在同一设备上)
问题描述
当我想将模型放到 GPU 上时,出现以下错误:
When I want to put the model on the GPU, I get the following error:
运行时错误:输入张量和隐藏张量不在同一设备上,在 cuda:0 处找到输入张量,在 cpu 处找到隐藏张量"
"RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu"
但是,以上所有内容都已放在 GPU 上:
However, all of the above had been put on the GPU:
for m in model.parameters():
print(m.device) #return cuda:0
if torch.cuda.is_available():
model = model.cuda()
test = test.cuda() # test is the Input
Windows 10 服务器
pytorch 1.2.0 + cuda 9.2
CUDA 9.2
cudnn 7.6.3 用于 cuda 9.2
Windows 10 server
Pytorch 1.2.0 + cuda 9.2
cuda 9.2
cudnn 7.6.3 for cuda 9.2
推荐答案
您需要将模型、输入和目标移动到 Cuda:
You need to move the model, the inputs, and the targets to Cuda:
if torch.cuda.is_available():
model.cuda()
inputs = inputs.cuda()
target = target.cuda()
这篇关于如何修复pytorch中的“输入和隐藏张量不在同一设备上"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何修复pytorch中的“输入和隐藏张量不在同一设
基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
