[英]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)
.我很难找到一个可以像
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感谢您的帮助:D
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.输入也是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.