pytorch 中tensor在CPU和GPU之间转换

1. CPU tensor转GPU tensor:

cpu_imgs.cuda()
2. GPU tensor 转CPU tensor:

gpu_imgs.cpu()
3. numpy转为CPU tensor:

torch.from_numpy( imgs )
4.CPU tensor转为numpy数据:

cpu_imgs.numpy()
5. note:GPU tensor不能直接转为numpy数组,必须先转到CPU tensor。

6. 如果tensor是标量的话,可以直接使用 item() 函数(只能是标量)将值取出来:

print loss_output.item()


原文:https://blog.csdn.net/moshiyaofei/article/details/90519430 

https://blog.csdn.net/marukoheart/article/details/88091409 python之Numpy的行列维度、axis=0/1/2...便捷记忆方法

https://www.zhihu.com/question/22686450  python中字符串 s[ : -1]是什么意思?

https://blog.csdn.net/appleml/article/details/80143212 pytorch中contiguous()

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值