maosolny
maosolny

Reputation: 1

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

I have a problem with finding a place where I can implement sth like that model = model.to(device). Below I add stacktrace:

Traceback (most recent call last):
  File "main.py", line 161, in <module>
    algo.train()
  File "C:\Users\mati5\OneDrive\Pulpit\GAN\FAJNE\AttnGAN2(1)\code\trainer.py", line 329, in train
    fake_imgs, _, mu, logvar = netG(noise, sent_em_gpt, words_em_gpt, mask)
  File "C:\Anaconda\envs\attngan-new\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\mati5\OneDrive\Pulpit\GAN\FAJNE\AttnGAN2(1)\code\model.py", line 440, in forward
    c_code, mu, logvar = self.ca_net(sent_emb)
  File "C:\Anaconda\envs\attngan-new\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\mati5\OneDrive\Pulpit\GAN\FAJNE\AttnGAN2(1)\code\model.py", line 311, in forward
    mu, logvar = self.encode(text_embedding)
  File "C:\Users\mati5\OneDrive\Pulpit\GAN\FAJNE\AttnGAN2(1)\code\model.py", line 296, in encode
    x = self.relu(self.fc(text_embedding))
  File "C:\Anaconda\envs\attngan-new\lib\site-packages\torch\nn\modules\module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Anaconda\envs\attngan-new\lib\site-packages\torch\nn\modules\linear.py", line 103, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Anaconda\envs\attngan-new\lib\site-packages\torch\nn\functional.py", line 1848, in linear
    return torch._C._nn.linear(input, weight, bias)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat1 in method wrapper_addmm) 

Thanks for your help :D

Upvotes: 0

Views: 4191

Answers (1)

Ophir Yaniv
Ophir Yaniv

Reputation: 366

It looks like your model and input tensors aren't on the same device. Please make sure to use

.to(device)

On the input too.

Upvotes: 0

Related Questions