python numpy.arry, pytorch.Tensor及原生list相互转换

python numpy.arry, pytorch.Tensor及原生list相互转换

1 原生list转numpy list

my_list = np.ndarray(my_list)

2 numpy.array 转原生list

my_list = my_list.tolist()

3 numpy.array转torch.Tensor

my_list = torch.from_numpy(my_list)

4 torch.Tensor转numpy.array

my_list = my_list.numpy() # cpu
my_list = my_list.cpu().numpy() # gpu

5 原生list转torch.Tensor

my_list = torch.Tensor(my_list)

6 torch.Tensor转原生list

my_list = my_list.numpy().tolist()
posted @ 2020-08-05 19:01  lzping  阅读(3191)  评论(0编辑  收藏  举报