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())#后四者一样,都是把数值取出来

loss,loss.cpu().data 及 loss.cpu().detach().numpy()等辨析_第1张图片

 结果:

loss,loss.cpu().data 及 loss.cpu().detach().numpy()等辨析_第2张图片

你可能感兴趣的:(python)