Pytorch中格式的轉化

Python中 list, numpy, torch格式相互轉化

1、list 轉 numpy

ndarray = np.array(list)

2、numpy 轉 list

list = ndarray.tolist()

3、list 轉 torch.Tensor

tensor=torch.Tensor(list)

4、torch.Tensor 轉 list

先轉numpy,後轉list

list = tensor.numpy().tolist()

5、torch.Tensor 轉 numpy

ndarray = tensor.numpy()

*gpu上的tensor不能直接轉爲numpy

ndarray = tensor.cpu().numpy()

6、numpy 轉 torch.Tensor

tensor = torch.from_numpy(ndarray) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章