栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch经过gpu训练的模型的结果使用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch经过gpu训练的模型的结果使用

一个模型结果形式如下,提取出来 第一种方式
pre_re = mymodel(x)
pre_re : tensor([ 预测值的结果 ],device='cuda:0,' ,grad_fn=)
#######################想要使用预测结果的内容
#1.需要将输入预测进行detach(),因为tensor数据是一个整体对象
pre_re.detach()的结果仍然时Tensor类型,若想使用需要使用numpy()
detach后,pre_re : tensor([ 预测值的结果 ],device='cuda:0,')
############################
# 2. 但cuda的Tensor,需要将detach()过的Tensor拷贝放入cpu中,再用numpy(),否则会出现下面的问题
经过cpu()后,pre_re.detach().cpu()结果:pre_re : tensor([ 预测值的结果 ],)
can't convert CUDA tensor to numpy,use Tensor.cpu() to copy the tensor to host memory first.
# 3.经过detach和cup后再进行numpy
结果得到想要的数据[ 预测值的结果   ]
总的过程
re_pre.detach().cpu().numpy()  #剩下的数据格式需要自己按需调整
第二种方式
pre_re = mymodel(x)
pre_re的形式 : tensor([ 预测值的结果 ],device='cuda:0,' ,grad_fn=)

使用pre_re.data.cpu().numpu()
pre_re.data的功能和detach()一样
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/355766.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号