Loading

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()
image

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

c = a.item()
print(c, type(c))
## 输出为:1.0 <class 'float'>
posted @ 2021-05-15 11:39  Guang'Jun  阅读(1021)  评论(0编辑  收藏  举报