Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 497
kaiyu Avatar asked Sep 25 '19 09:09

kaiyu


People also ask

What does .item do in PyTorch?

Returns the value of this tensor as a standard Python number. This only works for tensors with one element.

How do I know if PyTorch is using my GPU?

In PyTorch, the torch. cuda package has additional support for CUDA tensor types, that implement the same function as CPU tensors, but they utilize GPUs for computation. If you want a tensor to be on GPU you can call . cuda().

What is Torch tensor ()?

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.


2 Answers

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()
like image 93
ESZ Avatar answered Sep 20 '22 07:09

ESZ


This error occurs when PyTorch tries to compute an operation between a tensor stored on a CPU and one on a GPU. At a high level there are two types of tensor - those of your data, and those of the parameters of the model, and both can be copied to the same device like so:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

data = data.to(device)
model = model.to(device)
like image 32
iacob Avatar answered Sep 22 '22 07:09

iacob