python3 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
tensor = torch.from_numpy(ndarray)  # numpy 转 torch.Tensor

你可能感兴趣的:(数据处理)