python - 计算 tensorflow 梯度

标签 python tensorflow calculus

我对 tensorflow gradient documentation 中的示例感到困惑用于计算梯度。

a = tf.constant(0.)
b = 2 * a
g = tf.gradients(a + b, [a, b])

with tf.Session() as sess:
    print(sess.run(g))

给出 [3.0, 1.0]

我觉得我真的错过了一些明显的东西,但是如果 a本质上是 0然后b因此a+b =0 。那么,对 a 和 b 求零是如何得到像 [3.0, 1.0] 这样的结果的? .

我相信我在这里误解了 tensorflow 结构/语法。

最佳答案

为了进行比较,请考虑一个实变量的实值函数f:RR,由f<给出/em>(x) = 10x。这里,f'(x) = 10,无论x的值如何,所以特别是f' (0) = 10。

同样,正如教程中所解释的,或多或少根据定义,total derivative (a, b) ↦ a + bb(a>a) = 2 a 是 (3, 1),与 a 无关。

举一个不那么简单的例子,让我们考虑一下

a = tf.constant(5.)
b = 2 * a
g = tf.gradients(a**3 + 2*b**2, [a, b])

with tf.Session() as sess:
    print(sess.run(g))

这里,关于a的全导数是aa³ + 2(2 a)² = a³ + 8 a² 变为 a ↦ 3 a² + 16 < em>a,而关于 b 的导数为 a ↦ 4 b(a ) = 8a。因此,在 a = 5 时,我们期望结果为 (3 · 5² + 16 · 5, 8 · 5) = (155, 40),并且运行代码正是您得到的结果。

关于python - 计算 tensorflow 梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685934/

相关文章:

algorithm - 泰勒(麦克劳林)级数的高效生成

mysql - 如何计算mysql在时间范围内的方差和标准差

python - 如何使用 Facebook Prophet 选择初始、期间、范围和截止值?

python - 在 View 中保存方法

python - 为什么命名元组总是被 python 的 GC 跟踪?

python - Pandas 将时间列添加到日期索引

machine-learning - Tensorflow保存模型: GraphDef cannot be larger than 2GB

algorithm - 创建有利于较小数字的随机数

python - Tensorflow 高级索引 : Assign a smaller tensor into a bigger one into a position based on two index tensors

python - 如何在 Azure ML 中使用 TensorFlow Hub