一个模型结果形式如下,提取出来
第一种方式
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()一样