Python中 list, numpy, torch格式相互轉化
1、list 轉 numpy
ndarray = np.array(list)
2、numpy 轉 list
list = ndarray.tolist()
3、list 轉 torch.Tensor
tensor=torch.Tensor(list)
4、torch.Tensor 轉 list
先轉numpy,後轉list
list = tensor.numpy().tolist()
5、torch.Tensor 轉 numpy
ndarray = tensor.numpy()
*gpu上的tensor不能直接轉爲numpy
ndarray = tensor.cpu().numpy()
6、numpy 轉 torch.Tensor
tensor = torch.from_numpy(ndarray)