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)
Original: https://blog.csdn.net/weixin_48456383/article/details/124416038
Author: 奶奶滴,为什么不学java
Title: Python中 list, numpy.array, torch.Tensor 格式相互转化
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/760539/
转载文章受原作者版权保护。转载请注明原作者出处!