tensorflow - 计算图与(计算机代数)符号表达式

标签 tensorflow pytorch theano backpropagation automatic-differentiation

我在看 Baydin et al, Automatic Differentiation in Machine Learning: a Survey, 2018 ( Arxiv ),它区分符号微分和自动微分 (AD)。然后它说:

AD Is Not Symbolic Differentiation. Symbolic differentiation is the automatic manipulation of [symbolic] expressions.

AD can be thought of as performing a non-standard interpretation of a computer program where this interpretation involves augmenting the standard computation with the calculation of various derivatives.

Evaluation traces form the basis of the AD techniques. [A computational graph (Bauer, 1974) visualizes dependency relations of (input, working, output) variables in evaluation traces.]



然后继续描述如何使用 AD 计算导数(在前向或后向模式下)。描述基本上是转换评估轨迹/计算图。

Autograd, Chainer, and PyTorch provide general-purpose reverse mode AD.



它还讨论了 Theano、TensorFlow 等,但它基本上比较了定义并运行/静态计算图(Theano,TF)与运行定义/动态计算图(PyTorch,TF Eager)。
(在我的理解中,这与 AD 是如何执行的问题是正交的,或者主要只是改变 AD 的实现方式,而不是 AD 的概念。)

Theano is a computational graph optimizer and compiler [...] and it currently handles derivatives in a highly optimized form of symbolic differentiation. The result can be interpreted as a hybrid of symbolic differentiation and reverse mode AD, but Theano does not use the general-purpose reverse accumulation as we describe in this paper. (Personal communication with the authors.)



我不确定作者是否暗示 Theano/TF 不提供通用反向模式 AD(这在我的理解中是错误的)。

我不完全理解 Theano 如何不使用通用反向累积。

另外,鉴于此定义,我不明白符号差异与 AD 有何不同。

或者:符号表达式与计算图有何不同?

相关也是可微编程

differentiable directed graphs assembled from functional blocks



我再次没有看到与计算图的区别。

和反向传播(BP):

The resulting algorithm is essentially equivalent to transforming the network evaluation function composed with the objective function under reverse mode AD, which, as we shall see, actually generalizes the backpropagation idea.



我看不出反向模式 AD 比反向传播更通用。是吗?如何?

Schmidhuber, Deep Learning in Neural Networks: An Overview, 2014 (第 5.5 节)( also )指出:

BP is also known as the reverse mode of automatic differentiation (Griewank, 2012).

最佳答案

这是一个很好的问题,它涉及 AD 中的一些根本差异,以及 PyTorch 和 TensorFlow 等大型 ML 库之间的一些根本设计差异。特别是,我认为理解按运行定义和定义并运行 AD 之间的区别令人困惑,需要一些时间来理解。

反向传播与反向模式 AD?

可以看到一个堆栈溢出问题here ,以及我的回答。基本上,区别在于您是想要标量值函数 R^n -> R 的梯度还是向量值函数 R^n -> R^m 的向量雅可比积。反向传播假设您需要标量损失函数的梯度,并且是机器学习社区中最常用于谈论神经网络训练的术语。

因此,反向模式 AD 比反向传播更通用。

符号微分与 AD 有何不同?

符号微分作用于代表输入的符号,而 AD 计算给定输入的导数的数值。

例如:假设我有函数 y = x^2。如果我要计算 y 的符号导数,我会得到值 2x 作为符号导数。现在,对于 x 的任何值,我立即知道该 x 处的导数值。但是如果我要执行自动微分,我会先设置 x 的值,比如 x=5,我的 AD 工具会告诉我导数是 2*5,但它对 x=4 处的导数一无所知因为它只计算 x=5 处的导数,而不是导数的符号表达式。

定义并运行/静态计算图和按运行定义/动态计算图之间的区别?

正如您所指出的,TF1 和 Theano 是定义并运行的,而 Pytorch、Autograd 和 TF2 是定义运行的。有什么不同?

在 TensorFlow 1 中,你告诉 TensorFlow 你要做什么,然后 TensorFlow 准备通过构建静态计算图对一些数据执行这些计算,最后你收到数据并执行计算。因此,第 1 步是告诉 TensorFlow 您要做什么,第 2 步是在 TensorFlow 获得一些数据后执行该计算。

在 Autograd 中,你不会在做之前告诉它你要做什么。 Autograd 与 TF1 不同,它会在收到数据后找出您将要对数据执行的操作。如果它接收到一个向量,它不知道将对向量执行什么计算,因为它没有提前静态计算图。它通过在代码执行时记录对每个变量的操作来“构建图形”,然后在计算结束时,您将获得一个已执行操作的列表,您可以向后遍历这些操作。这使您可以轻松地包含像 if 语句这样的控制流。在定义并运行的框架中处理控制流要困难得多。

为什么 Theano 和 TF1 不提供通用反向模式 AD?

Theano 和 TF1 不提供通用 AD,因为它们不允许控制流。实际上,TF1 做到了,但它是 a mess .

可微编程和计算图之间的区别?

来自 Wikipedia :

"Differentiable programming is a programming paradigm in which the programs can be differentiated throughout, usually via automatic differentiation."



所以可微分编程是设计程序的一种范式。另一方面,计算图是 AD 领域中用于理解由可微计算机程序执行的计算的抽象。一个是编程范式,一个是编程抽象。

关于tensorflow - 计算图与(计算机代数)符号表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55868135/

相关文章:

python - 模型适用于单个 GPU,但在尝试适用于多个 GPU 时脚本崩溃

python - 将数组打包成张量的下三角

tensorflow - 如何在 Google Colab 中将 TensorFlow 2.0 设置为默认版本

python - 使用 GoogleNews-vectors-negative300.bin 构建字典返回 ValueError : could not convert string to float

python - Theano 中的切片将矩阵转换为向量

TF2.0 中的 Tensorflow 分析

python - pytorch 为从 numpy 创建张量做了什么

python - 在 TensorFlow 中展平张量的最后两个维度

python - 输入和输出之间的相关熵

python - 如何获得每层的权重形状?