python中list-numpy.array-torch.tensor互相转换
⭐️list 转 numpy
ndarray = np.array(list)
⭐️list 转 Tensor
tensor = torch.Tensor(list)
⭐️numpy 转 list
list = ndarray.tolist()
⭐️numpy 转 Tensor
tensor = torch.from_numpy(ndarray)
⭐️Tensor 转 list
需要先转为numpy,再转为list
list = tensor.numpy().tolist()
⭐️Tensor 转 numpy
ndarray = tensor.numpy()
如果tensor再GPU上需要先转到cpu,再转为numpy
ndarray = tensor.cpu().numpy()
还没有评论,来说两句吧...