Skip to content

Instantly share code, notes, and snippets.

@MohanaRC
Created November 7, 2022 13:07
Show Gist options
  • Save MohanaRC/e0736700ebcade484e6c49759f42a655 to your computer and use it in GitHub Desktop.
Save MohanaRC/e0736700ebcade484e6c49759f42a655 to your computer and use it in GitHub Desktop.
def tf_gradient_tape2(x):
"""
Simple implementation to understand the functioning of gradient tape for chain rule
Inputs:
x: Tensor value
Returns:
EagerTensor: Derivative of z with respect to input tensor y
"""
with tf.GradientTape() as t:
t.watch(x)
y=x*x ## Defining y(x)=x**2
z=y*y ## Defining z(y)=y**2
# Calculating the derivative of z with respect to x
dz_dx = t.gradient(z, x)
return dz_dx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment