[Torch]Torch tensor与numpy互相转换
程序员文章站
2022-07-14 18:46:35
...
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.
上一篇: 【字符串】面试题之键值对(”key = valude”)字符串
下一篇: Day4_列表基础