如何在pytorch中修复``输入张量和隐藏张量不在同一设备上'' [英] How to fix 'Input and hidden tensors are not at the same device' in pytorch

查看:1078
本文介绍了如何在pytorch中修复``输入张量和隐藏张量不在同一设备上''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要将模型放到GPU上时,出现错误! 它说Inputs在GPU上,但是隐藏状态在CPU上. 但是,所有这些都已放置在GPU上. 我用

When I want to put the model on the GPU, there is an error! It said the Inputs is on GPU, but the hidden state is on CPU. However, all of them had been put on the GPU. I use

for m in model.parameters():
    print(m.device) #return cuda:0

可以在GPU设备上查看模型上的所有状态. 错误是"RuntimeError:输入张量和隐藏张量不在同一设备上,在cuda:0处找到了输入张量,在cpu处发现了张量"

to see all of the state on the model is on the GPU device. The error is "RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu"

Windows 10服务器 pytorch 1.2.0 + CUDA 9.2 CUDA 9.2 cudnn 7.6.3 for CUDA 9.2

Windows 10 server Pytorch 1.2.0 + cuda 9.2 cuda 9.2 cudnn 7.6.3 for cuda 9.2

if torch.cuda.is_available():

    model = model.cuda()

if torch.cuda.is_available():
    `test= test.cuda() #test is the Input`

推荐答案

您需要将模型,输入和目标移动到Cuda

You need to move the model , the inputs and the targets to Cuda

if(torch.cuda.is_available()):

   rnn.cuda()# rnn is your model 
   inputs = inputs.cuda() 
   target = target.cuda()

这篇关于如何在pytorch中修复``输入张量和隐藏张量不在同一设备上''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆