在深度学习训练后,需要计算每个epoch得到的模型的训练效果的时候,一般会用到detach() item() cpu() numpy()等函数。
例如
import torch.optim as optim
import torch.utils.data
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
gpu_info = Variable(torch.randn(3,3)).cuda()
print(gpu_info)
tensor([[ 0.9621, -1.0931, -0.8481],
[-0.1668, -1.3945, 0.6562],
[ 0.6152, 0.4177, -0.3538]], device='cuda:0')
item()返回的是tensor中的值,且只能返回单个值(标量),不能返回向量,使用返回loss等。
loss.item() # 获得loss的值
detach阻断反向传播,返回值仍为tensor
gpu_info.detach() #返回tensor,仍在gpu上
tensor([[ 0.9621, -1.0931, -0.8481],
[-0.1668, -1.3945, 0.6562],
[ 0.6152, 0.4177, -0.3538]], device='cuda:0')
cpu()将变量放在cpu上,仍为tensor:
gpu_info.cpu()
tensor([[ 0.9621, -1.0931, -0.8481],
[-0.1668, -1.3945, 0.6562],
[ 0.6152, 0.4177, -0.3538]])
numpy()将tensor转换为numpy:
注意cuda上面的变量类型只能是tensor,不能是其他
gpu_info.detach().numpy()
TypeError Traceback (most recent call last)
in ()
----> 1 gpu_info.detach().numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
gpu_info.cpu().numpy()
array([[ 0.9621306 , -1.0930926 , -0.8481391 ],
[-0.1667992 , -1.3945109 , 0.656157 ],
[ 0.6151904 , 0.41773367, -0.35378388]], dtype=float32)