user17951472
user17951472

Reputation: 21

problems on google colab pytorch learning-RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu

I'm very newbie on pytorch and deep learning, and I got some error while running a sample code from deep learning class.

When I run the code I attached below, There comes an errors like,

text = torch.from_numpy(data['text']).long().cuda(0)

# feature extraction
mel_gt = get_mel(audio)

# shift mel spectrogram -> the input of the network
mel_shift = torch.cat((torch.zeros_like(mel_gt)[:,:,:1], mel_gt[:,:,:-1]) ,axis=-1)

# inference
mel_est, attention = model(mel_shift, text)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

I can hardly understand because I also check whether the tensors are on cuba with these code, they all return true..

print(mel_shift.is_cuda)    
print(mel_gt.is_cuda)
print(text.is_cuda)

Can you guys figure out what's the problem?? I need big big help,,

Upvotes: 2

Views: 3398

Answers (1)

Ahana Deb
Ahana Deb

Reputation: 11

Check if your model is loaded on cuda by running next(model.parameters()).is_cuda. If it returns False, load the model on CUDA using

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

Upvotes: 1

Related Questions