天天看點

only one element tensors can be converted to Python scalars

only one element tensors can be converted to Python scalars

次元為1,1的能item()

次元多的就不能轉換:

loss=torch.Tensor([1,1])

loss.item()      
import torch
a=[[1,2,3],[2,3,4],[1]]
b=torch.Tensor(a)
print(b)
           

發現以下代碼就能引發異常,因為不能嵌套

import torch

b=[]
a=[[1,2,3],[2,3,4],[1,2,3]]
c=torch.tensor(a, dtype=torch.long)

b.append(c)

d=torch.tensor(b, dtype=torch.long)
print(d)