Skip to content

Instantly share code, notes, and snippets.

@denizyuret
Created December 3, 2021 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denizyuret/1af3577afbe6a53d61bc75f86fed4ac4 to your computer and use it in GitHub Desktop.
Save denizyuret/1af3577afbe6a53d61bc75f86fed4ac4 to your computer and use it in GitHub Desktop.
@Kausta: Tensorflow also contains a documentation for higher order gradients with nested tapes in https://www.tensorflow.org/guide/advanced_autodiff#higher-order_gradients, and it is followed (in the same link) by an input gradient penalty example (The gradient of (the magnitude of the gradient with respect to the inputs) with respect to the mo…
# R1 regularization (Hypothetical)
with tf.GradientTape() as t2:
with tf.GradientTape() as t1:
# Discriminator outputs
disc_out = forward(w, x)
# Regular Loss
fl = loss(disc_out)
# Gradients with respect to the inputs
g = t1.gradient(sum(disc_out), x)
# Mean squared sum of the gradients
fl += mean(square(g))
# Gradient of (loss + mse of grads wrt input) with respect to model weights
grad = t2.gradient(fl, w)
w -= grad
# Input gradient penalty example from tensorflow documentation
with tf.GradientTape() as t2:
# The inner tape only takes the gradient with respect to the input,
# not the variables.
with tf.GradientTape(watch_accessed_variables=False) as t1:
t1.watch(x)
y = layer(x)
out = tf.reduce_sum(layer(x)**2)
# 1. Calculate the input gradient.
g1 = t1.gradient(out, x)
# 2. Calculate the magnitude of the input gradient.
g1_mag = tf.norm(g1)
# 3. Calculate the gradient of the magnitude with respect to the model.
dg1_mag = t2.gradient(g1_mag, layer.trainable_variables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment