问题
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