[Pytorch函数]loss,loss.cpu().data 及 loss.cpu().detach().numpy()等辨析

print('1111',loss)
print('2222',loss.data)#tensor且GPU
print('3333',loss.cpu())
print('4444',loss.cpu().data)#tensor且CPU
# print('5555',loss.cpu().data[0])#报错 IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number
# print('6666',loss.cpu().numpy())#报错 RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.
print('7777',loss.cpu().detach().numpy())
print('8888',loss.cpu().data.numpy())
print('9999',loss.cpu().item())
print('aaaa',loss.item())#后四者一样,都是把数值取出来

在这里插入图片描述
结果:
在这里插入图片描述

你可能感兴趣的:(Pytorch函数,python,深度学习,pytorch)