I would like to intercept gradients that are backpropagated in my Tensorflow graph, which are not based on the loss (∂L/∂w
), but based on some other node in the graph, for example the class scores (∂s/∂w
) in a classification problem or some activation (∂a/∂w
) to see how it changes when certain weights w
change.
How can one implement this efficiently in Tensorflow? Intuitively, the gradients should already all be there for backprop of the loss as intermediate results, so there should be a solution without a big overhead.
I am already aware of the following suggestions, which don't exactly solve the problem:
The Tensorflow method
tf.gradients(ys, xs)
, which computes the gradient for every y in ys w.r.t. every xs, but then, for every x in xs sums over all y. Applying this function for every y in ys separately, however, induces a large computational overhead.This stackoverflow post, which ask this question for the derivative of the loss w.r.t. some parameters, i.e.
∂L/∂w
.The part of the documentation, which proposes to call
optimizer.compute_gradients()
as an easy to use 'wrapper' aroundtf.gradients()
. However, calling this function for every variable of interest introduces again a large computational overhead.
Update: Phrased differently, what I want is the Jacobian of any component of the computational graph w.r.t. any other. This topic has been touched in this recent Tensorflow issue, but is described as currently not being efficiently/conveniently implemented therein.