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

17,391

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()
Share:
17,391

Related videos on Youtube

kaiyu
Author by

kaiyu

Updated on July 10, 2022

Comments

  • kaiyu
    kaiyu almost 2 years

    When I want to put the model on the GPU, I get the following error:

    "RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu"

    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 server
    Pytorch 1.2.0 + cuda 9.2
    cuda 9.2
    cudnn 7.6.3 for cuda 9.2