且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

张量流图中的梯度计算不正确吗?

更新时间:2023-09-13 14:57:34

不涉及任何错误.您只需要了解什么是梯度并知道如何自己计算即可.所以(x+1)^2' = 2*(x+1).这意味着您无需计算(x+1)^2即可计算梯度.如果您要放大渐变部分,您会看到它计算出了正方形的渐变,并且发现那里确实需要图形的哪一部分:

There is no bug involved. You just need to understand what is a gradient and know how to compute one yourself. So (x+1)^2' = 2*(x+1). Which means that you do not need to calculate (x+1)^2 to calculate the gradient. If you will zoom in the gradient part you will see that it calculated the gradient of your square and figured out that it does which part of the graph is needed there :

这是一个更有趣,更直观的示例:

Here is a more interesting and more intuitive example:

import tensorflow as tf

x = tf.Variable(initial_value=3.0)
y = tf.cos(x)

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(y)

with tf.Session() as sess:
    writer = tf.summary.FileWriter('logs', sess.graph)
    writer.close()

您应该知道cos(x)' = - sin(x).这意味着只需要x即可计算梯度.这就是您在图形中看到的内容:

You should know that cos(x)' = - sin(x). Which means that only x is needed to calculate gradient. And this is what you see in the graph: