data = np.array([1, 2, 3, 4])
t1 = torch.Tensor(data)
t2 = torch.tensor(data)
t3 = torch.as_tensor(data)
t4 = torch.from_numpy(data)
data[0] = 0
data[1] = 0
t1–t4的结果
tensor([1., 2., 3., 4.])
tensor([1, 2, 3, 4], dtype=torch.int32)
tensor([0, 0, 3, 4], dtype=torch.int32)
tensor([0, 0, 3, 4], dtype=torch.int32)
share data | copy data |
---|---|
torch.as_tensor() | torch.tensor() |
torch.from_numpy() | torch.Tensor() |