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中的“输入和隐藏张量不在同一设


基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01