我试图TF GRADIENT TAPE在以下代码中了解 的目的:
import tensorflow as tf
var = tf.Variable(5.0)
with tf.GradientTape() as tape:
op = (2*var)+(var*var)
diff = tape.gradient(op,var)
print (diff)
操作:
diff = tf.Tensor(12.0, shape=(), dtype=float32)
我很困惑,因为既然var=5, the op=(2*5)+(5*5)=>35, and if I are calculating the derivative of a constantthendiff should be 0
我理解它的原因12,因为它不采用varas5而是(2*var)+(var*var)=> 2var+var**2计算这个函数的导数2+2*var=>12。
但我不明白的是,为什么var不考虑给定的值?