import torch
from torch.autograd import Variable
a=Variable(torch.Tensor([2,3]),requires_grad=True)
b=a+3
c=b*3
out=c.mean()
out.backward()
print("a.data")
print(a.data)
print("b.data")
print(b.data)
print("c.data")
print(c.data)
print("out.data")
print(out.data)
print(out.data.item())
print(type(out.data))
print(type(out.data.item()))
print(type(a))
print(a.grad)
输出
可见 torch.data 获取的类型是tensor, item则是float