2x gradient in GradientTape

Hello everyone !) I am trying to understand how works 2x gradient call at the same GradientTape -

x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as g:
  g.watch(x)
  y = x * x
  z = y * y
dz_dx = g.gradient(z, x)  # (4*x^3 at x = 3)
print(dz_dx)
>> tf.Tensor(108.0, shape=(), dtype=float32)
dy_dx = g.gradient(y, x)
print(dy_dx)
>> 
tf.Tensor(6.0, shape=(), dtype=float32)

first, we get ‘2x’ derivative from y = x * x, then we get ‘4x^3’ derivative from z = y * y…
So i can`t understand how obtained '4
x^3’ ). Thanks !

@Denis,

Welcome to the Tensorflow Forum!

To compute the derivative of z with respect to x, we need to apply the chain rule of differentiation

dz/dx = dz/dy * dy/dx

where dy/dx is the derivative of y with respect to x and dz/dy is the derivative of z with respect to y.

Since y = x * x then dy/dx results 2 * x. Similarly for dz/dy becomes 2 * y since z=y * y .

As defined earlier substituting y= x * x in 2y becomes 2 * x * x.

Overall dz/dx becomes (2 * x * x) * (2 * x) which is 4 * x^3.

Thank you!

2 Likes

Nice explanation ! Thank you !)