How to fix 'Input and hidden tensors are not at the same device' in pytorch

一世执手 提交于 2020-05-30 08:04:43

问题


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

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 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`

回答1:


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()


来源:https://stackoverflow.com/questions/58095627/how-to-fix-input-and-hidden-tensors-are-not-at-the-same-device-in-pytorch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!