pytorch中.data()与.item()

pytorch中.data()与.item() - Guang'Jun - 博客园 

1..data()
将变量(Variable)变为tensor,将requires_grad设置为Flase

a = torch.tensor([1.0], requires_grad=True)
b = a.data
print(b, b.requires_grad)
## 输出为: tensor([1.]) False

2..item()

a = torch.tensor([1.0], requires_grad=True)

c = a.item()
print(c, type(c))
## 输出为:1.0 

你可能感兴趣的:(pytorch,python)