Pytorch使用

时间:2020-06-30 23:24:10   收藏:0   阅读:71

1. list, np.array, torch.tensor相互转换

ndarray = np.array(list)      # list 转 numpy数组
list = ndarray.tolist()       # numpy 转 list
tensor=torch.Tensor(list)      # list 转 torch.Tensor
list = tensor.numpy().tolist()   # torch.Tensor 转 list  先转numpy,后转list
ndarray = tensor.cpu().numpy()   # torch.Tensor 转 numpy(gpu上的tensor不能直接转为numpy,要先存到CPU)
tensor = torch.from_numpy(ndarray)  # numpy 转 torch.Tensor

 

原文:https://www.cnblogs.com/grainrain/p/13216373.html

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