天天看点

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)