之前讲了numpy和panda等类型的转换,这次讲tensor
tensor长数据输出不完整,可以将tensor转换成list,也可以将tensor转化成numpy等类型
list,tensor,numpy类型互换的方法 参考
1.1 list转numpy
ndarray = np.array(list)
1.2 numpy 转 list
list = ndarray.tolist()
2.1 list 转 torch.Tensor
tensor=torch.Tensor(list)
2.2 torch.Tensor 转 list
先转numpy,后转list
list = tensor.numpy().tolist()
3.1 torch.Tensor 转 numpy
ndarray = tensor.numpy()
*gpu上的tensor不能直接转为numpy
ndarray = tensor.cpu().numpy()
3.2 numpy 转 torch.Tensor
tensor = torch.from_numpy(ndarray)