list,tensor,numpy相互转化

六眼飞鱼酱① 提交于 2019-11-28 07:15:16

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) 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!