- 查看tensor的维度
tensor类型不方便查看维度,可以转为numpy的array类型,然后用shape查看。
print(data.numpy().shape)
- 将list转换为tensor类型
直接的想法是用torch.tensor()直接进行转换,但是当list中含有tensor类型的值时会报错:ValueError:only one element tensors can be converted to Python scalars。
解决方法:参见这篇博客
val= torch.tensor([item.cpu().detach().numpy() for item in val]).cuda()