pytorch自动求导学习

1.pytorch自动求导机制

 https://zhuanlan.zhihu.com/p/79801410

只能对浮点类型的tensor设置x.requires_grad_(True);

 import torch
 # Creating the graph
 x = torch.tensor(1.0, requires_grad = True)
 z = x ** 3
 z.backward() #Computes the gradient
 print(x.grad.data) #Prints '3' which is dz/dx 

在前向传播的时候会形成一个动态计算图DCG,这样在反向的时候backward,其实是有一个隐式的参数传递, 用来作为叶子节点终止求导。

torch不显式地构造雅可比矩阵,而是构造Jacobian vector product雅可比矩阵向量乘积,这样计算更快。

后面还举了一个例子,当X通过f(X)构造成了Y时,这是Loss对每一个y求导,那么每一y其实对每一个x也都导数,这就是一个矩阵,通过矩阵和之前的向量求积,就能得到Loss对每一个x的偏导数,这样就可以更新梯度了。

原文地址:https://www.cnblogs.com/BlueBlueSea/p/13035802.html