简体   繁体   English

pytorch 如何计算简单函数的导数?

[英]How does pytorch compute derivatives for simple functions?

When we talk about the auto-differentiation in the pytorch, we are usually presented a graphical structures of tensors based on their formulas, and pytorch will compute the gradients by tracing down the graphical tree using chain rules.当我们谈论 pytorch 中的自微分时,通常会根据其公式为我们呈现张量的图形结构,而 pytorch 将通过使用链式规则追踪图形树来计算梯度。 However, I want to know what will happen at the leaf nodes?但是,我想知道叶子节点会发生什么? Does pytorch hardcode a whole list of basic functions with their analytical derivatives, or does it compute the gradients using numerical methods? pytorch 是否使用解析导数对整个基本函数列表进行硬编码,还是使用数值方法计算梯度? A quick example:一个简单的例子:

import torch

def f(x):
    return x ** 2
x = torch.tensor([1.0], requires_grad=True)
y = f(x)
y.backward()
print(x.grad) # 2.0

In this example, does pytorch compute the derivative by $$ (x^2)' = 2x = 2 * 1 = 2 $$, or does pytorch compute in a way similar to $$ (1.00001^2 - 1^2) / (1.000001 - 1) ~ 2 $$?在此示例中,pytorch 是通过 $$ (x^2)' = 2x = 2 * 1 = 2 $$ 计算导数,还是 pytorch 以类似于 $$ (1.00001^2 - 1^2) 的方式计算 / (1.000001 - 1) ~ 2 $$?

Thanks!谢谢!

See this paper for exact answer, specifically section 2.1 or figure 2.有关确切答案,请参阅本文,特别是第 2.1 节或图 2。

In short, PyTorch has a list of basic functions and the expression of their derivatives.简而言之,PyTorch 有一个基本函数列表和它们的派生表达式。 So, what is done in your case (y =x x), is evaluating $$ y' = 2 x $$.因此,在您的情况下(y = x x)所做的是评估 $$ y' = 2 x $$。

The numerical method you mentioned is called numerical differentiation or finite differences, and it is an approximation of the derivative.您提到的数值方法称为数值微分或有限差分,它是导数的近似值。 But it is not what PyTorch does.但这不是 PyTorch 所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM