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

时间:2020-03-13 22:17:01   收藏:0   阅读:278

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) 

原文:https://www.cnblogs.com/JetpropelledSnake/p/12489238.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!