Pytorch中张量如何转换为其他Python对象

141 阅读1分钟

torch张量和numpy数组互转

.numpy():张量转数组
.torch.tensor():数组转张量

image.png

X为初始张量

torch张量转换为Python标量

要将⼤⼩为1的张量转换为Python标量,我们可以调⽤item函数或Python的内置函数。

.item():张量转为标量
.int()……: 强制类型转换

image.png