[Torch]Torch tensor与numpy互相转换

Torch tensor与numpy互相转换

Author: Xin Pan

Date: 2020.06.07


实验环境

torch=1.5.0 +python=3.5.3 +numpy=1.14.6

numpy转tensor

numpy_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(numpy_data)

#输出
 [[0 1 2]
 [3 4 5]] 
    
 tensor([[0, 1, 2],
        [3, 4, 5]], dtype=torch.int32)

tensor转numpy

torch_data = torch.from_numpy(numpy_data)
tensor2numpy = torch_data.numpy()

#输出
tensor([[0, 1, 2],
        [3, 4, 5]], dtype=torch.int32)

[[0 1 2]
 [3 4 5]]

还会更新其他的类型转换方法

To be continued.

你可能感兴趣的:(人工智能)