Hello,
x = tf.constant(3.0)
with tf.GradientTape() as tape:
tape.watch(x)
[var.name for var in tape.watched_variables()]
This displays nothing.
Thanks
Hello,
x = tf.constant(3.0)
with tf.GradientTape() as tape:
tape.watch(x)
[var.name for var in tape.watched_variables()]
This displays nothing.
Thanks
For Constants
gradients will not be recorded. Instead you should use tf.Variable
for the GradientTape
to watched.
For example, the following fails to calculate a gradient because the tf.Tensor
is not “watched” by default, and the tf.Variable
is not trainable:
# A trainable variable
x0 = tf.Variable(3.0, name='x0')
# Not trainable
x1 = tf.Variable(3.0, name='x1', trainable=False)
# Not a Variable: A variable + tensor returns a tensor.
x2 = tf.Variable(2.0, name='x2') + 1.0
# Not a variable
x3 = tf.constant(3.0, name='x3')
with tf.GradientTape() as tape:
y = (x0**2) + (x1**2) + (x2**2)
grad = tape.gradient(y, [x0, x1, x2, x3])
for g in grad:
print(g)
Output:
tf.Tensor(6.0, shape=(), dtype=float32)
None
None
None
Note: TF1.x is deprecated, please use 2.x for better compatibility.
Thank you!