当前位置: 代码迷 >> 综合 >> pytorch中tensor与其他数据结构的转化(list, array)
  详细解决方案

pytorch中tensor与其他数据结构的转化(list, array)

热度:61   发布时间:2024-03-08 23:26:02.0

本文记录cpu张量、cuda张量、list和array之间的转换关系。 

import torch
import numpy as np
# list -> tensor(cpu)
l0 = [1, 2, 3]
t = torch.Tensor(l0)# tensor(cpu) -> numpy -> list
a = t.numpy()
l1 = t.numpy().tolist()# list -> numpy
a0 = np.array(l0)# numpy -> tensor(cpu)
t1 = torch.from_numpy(a0)# tensor(cpu) -> tensor(cuda)
tc = t1.cuda()# tensor(cuda) -> list
l2 = tc.cpu().numpy().tolist()

 

  相关解决方案