torch和numpy的相互转换
Posted peixu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了torch和numpy的相互转换相关的知识,希望对你有一定的参考价值。
import torch x = torch.rand(2,2) x1 = x.numpy() # torch转换到numpy x2 = torch.from_numpy(x1) #numpy转换torch print(" torch_x: ",x, " numpy_x1: ",x1, " torch_x2: ",x2)
out:
torch_x: tensor([[0.8195, 0.8927], [0.7544, 0.8827]]) numpy_x1: [[0.8194596 0.8926985] [0.754393 0.882745 ]] torch_x2: tensor([[0.8195, 0.8927], [0.7544, 0.8827]])
以上是关于torch和numpy的相互转换的主要内容,如果未能解决你的问题,请参考以下文章
Pythontorch.Tensornumpy.ndarraylist三者之间的相互转换
Python中 list, numpy.array, torch.Tensor 格式相互转化