pytorch中.data()与.item()

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()
pytorch中.data()与.item()

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

c = a.item()
print(c, type(c))
## 输出为:1.0 <class 'float'>
上一篇:python__regression


下一篇:路遥知马力——Momentum动量梯度