Why tf.constant does not work for nested gradient tapes?

I’m doing the gradient tape exercise and found that x = tf.constant(3.0) doesn’t work for the nested gradient tape. The error code is as follows:

Target should be a list or nested structure of Tensors or Variables to be differentiated, but recieved None

However, when I changed to tf.Variable(3.0), it worked just fine!

Does anyone know the reason? Thanks!

Hi @claire_bi ,

Could you provide a screenshot of your code?

Thank you.

Hi @RAJESH_CHERIAN_ROY ,

The screenshot is as follows:

When I change to tf.Variable, the code works:

You need to watch x before defining the formula.
For example:
tf.watch(x)
y = x**3