天天看點

tensor,numpy,list轉換

numpy2tensor:

b= torch.from_numpy(a)

tensor2numpy:

b= a.numpy()

numpy2list:

b = list(a) or b = a.tolist()

list2numpy:

n = numpy.array(a)

list2tensor:

b= torch.Tensor(a)

使用torch.Tensor()也可以将numpy轉換為tensor,但是與torch.from_numpy()不同,不是共享記憶體,是以速度較慢。