【发布时间】:2021-10-02 03:05:43
【问题描述】:
在下面的代码中:
-
dy 被计算为 1。如何计算这个值(数学是什么)?根据 tf.custom_gradient 指南,dy 是梯度上游
-
为什么最终梯度会乘以 clip_norm 值(0.6)? (这意味着(v * v)的final_gradients被乘以0.6,v * v的梯度是2v,为什么要乘以0.6?)
@tf.custom_gradient def clip_gradients(y): print('y',y) def backward(dy): print('dy',dy) return tf.clip_by_norm(dy, 0.6) return y, backward v = tf.Variable(3.0) with tf.GradientTape() as t: output = clip_gradients(v * v) print('output',output) print('Final Gradient is ',t.gradient(output, v))
'''
代码输出
y tf.Tensor(9.0, shape=(), dtype=float32)
output tf.Tensor(9.0, shape=(), dtype=float32)
dy tf.Tensor(1.0, shape=(), dtype=float32)
Final Gradient is tf.Tensor(3.6000001, shape=(), dtype=float32)
【问题讨论】:
-
Why final gradients is getting multiplied by clip_norm value(0.6)?是什么意思另外,也许answer(虽然是关于 TF1)可以帮助你理解。 -
已编辑问题,请查看它意味着(v * v)的final_gradients乘以0.6,v * v的梯度为2v,为什么乘以0.6?
标签: python tensorflow gradient