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