以后博客在 https://oldpan.me 中更新
1、expected CPU tensor (got CUDA tensor)
期望得到CPU类型张量,得到的却是CUDA张量类型。
很典型的错误,例如计算图中有的参数为cuda型有的参数却是cpu型就会遇到这样的错误。
>>> import torch
>>> from torch.autograd import Variable
>>> a = torch.Tensor([1])
>>> b = torch.Tensor([2])
>>> a = Variable(a)
>>> b = Variable(b)
>>> a.requires_grad = True
>>> b = b.type(torch.cuda.FloatTensor)
>>> c = a + b # 这里a和b两个张量不在同一个空间一个在cpu中另一个在gpu中因此会引发错误
Traceback (most recent call last):
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-16-60f555c9e9aa>", line 1, in <module>
c = a + b
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\variable.py", line 813, in __add__
return self.add(other)
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\variable.py", line 319, in add
return self._add(other, False)
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\variable.py", line 313, in _add
return Add.apply(self, other, inplace)
File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\_functions\basic_ops.py", line 17, in forward
return a.add(b)
TypeError: add received an invalid combination of arguments - got (torch.cuda.FloatTensor), but expected one of:
* (float value)
didn't match because some of the arguments have invalid types: (!torch.cuda.FloatTensor!)
* (torch.FloatTensor other)
didn't match because some of the arguments have invalid types: (!torch.cuda.FloatTensor!)
* (torch.SparseFloatTensor other)
didn't match because some of the arguments have invalid types: (!torch.cuda.FloatTensor!)
* (float value, torch.FloatTensor other)
* (float value, torch.SparseFloatTensor other)