目录
- 关于torch.tensor
- 关于tensor的操作
- 关于梯度gradients
关于torch.tensor
关于tensor的操作
x1=torch.ones(3,3)
x=torch.ones(2,2,requires_grad=True)
print(x1,'\n',x)
y=x+2
print(y)
print(x.grad_fn)
print(y.grad_fn)
z=y*y*3
out=z.mean()
print(z,out)
注意
a=torch.randn(2,2)
a=((a*3)/(a-1))
print(a.requires_grad)
a.requires_grad_(True)
print(a.requires_grad)
b=(a*a).sum()
print(b)
print(b.grad_fn)
关于梯度gradients
out.backward()
print(x.grad)
print(x.requires_grad)
print((x**2).requires_grad)
with torch.no_grad():
print((x**2).requires_grad)
y=x.detach()
print(y.requires_grad)
print(x)
print(y)
print(x.eq(y).all())