List和Numpy只能在CPU上,Tensor可以在GPU上运行,GPU上的Tensor不能直接转为Numpy,需要先放到CPU上再转。
import numpy as np
import torch
a = [[1, 1], [2, 2], [3, 3]] #list
print(a)
b = np.array(a) #list->numpy
print(b)
c = torch.tensor(a) #list->tensor
print(c)
print(b.tolist()) #numpy->list
print(c.tolist()) #tensor->list
print(torch.tensor(b)) #numpy->tensor
print(torch.from_numpy(b)) #numpy->tensor
print(c.numpy()) #tensor->numpy