Torch.tensor.backward()方法的使用举例

理解optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理

Autograd: 自动求导

import torcha=torch.randn(2,2) # tensor默认requires_grad=Falsea=((a*3)/(a-1))print(a.requires_grad)print(a.grad_fn) # leaf tensor的.grad_fn属性为Noneb = torch.tensor([4.0, 3.0, 2.0], requires_grad=True)print(b.requires_grad)print(b.grad_fn) # leaf tensor的grad_fn属性为Nonec = (a*a).sum()print(c.requires_grad) # 由于a.requires_grad=False,所以c.requires_grad=Falseprint(c.grad_fn) # 由于自变量tensor,也即a的requires_grad=False,作用在其上的运算不会被跟踪,所以c.grad_fn=Nonea.requires_grad_(True) # 通过内置requires_grad_()方法改变其requires_grad属性print(a.requires_grad) # Trued = (a*a).sum()print(d.requires_grad) # 由于a.requires_grad=True,所以d.requires_grad=Trueprint(d.grad_fn) # 由于a.requires_grad=True,所以d.grad_fn有内容# https://blog.csdn.net/wangweiwells/article/details/101223420# 但requires_grad属性为True并不意味着可以得到对其的gradient,还要看其是否为leaf tensore1 = torch.rand(10, requires_grad=True) + 2print(e1.requires_grad) # Trueprint(e1.is_leaf) # False,因为e是由加法运算得到的,所以不是leaf tensor,不能得到对e的梯度# 但所有requires_grad=False的Tensor都为leaf Tensor(同时也不能得到对它们的梯度)e2 = torch.rand(10) + 2print(e2.requires_grad) # Falseprint(e2.is_leaf) # Truee3 = e2.sum()print(e3.requires_grad) # Falseprint(e3.grad_fn) # Nonee3.backward() # 由于e3.grad_fn=None,所以此处会报错# 评估模型等情况下,我们不需要跟踪计算历史或使用内存,此时可用torch.tensor.detach()或以下方法with torch.no_grad():print(d.requires_grad) # Truef = (a*a).sum()print(f.requires_grad) # False