可以用 torch.fron_numpy 将 Numpy 数组转化为 PyTorch 张量。
1 import torch 2 from torch.autograd import Variable 3 import numpy as np 4 x = np.array([[1,2,3],[2,3,4]]) 5 xt = torch.from_numpy(x) 6 print(xt)
1 import torch 2 from torch.autograd import Variable 3 import numpy as np 4 x = np.array([[1,2,3],[2,3,4]]) 5 xt = torch.from_numpy(x) 6 print(xt)