Created
December 3, 2021 07:38
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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