0
点赞
收藏
分享

微信扫一扫

机器学习笔记之list, numpy.array, torch.Tensor 格式相互转化

辰鑫chenxin 2022-05-24 阅读 82
人工智能

0x00 list 转 numpy

ndarray = np.array(list)


0x01 numpy 转 list

list = ndarray.tolist()


0x02 list 转 torch.Tensor

tensor=torch.Tensor(list)


0x03 torch.Tensor 转 list

先转numpy,后转list

list = tensor.numpy().tolist()


0x04 torch.Tensor 转 numpy

ndarray = tensor.numpy()

*gpu上的tensor不能直接转为numpy

ndarray = tensor.cpu().numpy()


0x05 numpy 转 torch.Tensor

tensor = torch.from_numpy(ndarray) 




举报

相关推荐

0 条评论