torch和numpy的相互转换


import torch
x = torch.rand(2,2)
x1 = x.numpy() # torch转换到numpy
x2 = torch.from_numpy(x1) #numpy转换torch
print("\n torch_x:\n",x,
   "\n numpy_x1:\n",x1,
   "\n torch_x2:\n",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]])

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM