1. 程式人生 > >深度學習:pytorch常見錯誤總結

深度學習:pytorch常見錯誤總結

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\Anaconda
3\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)