# Novel method for feature importance using ReverseMode + ForwardMode autodiff?

**URL:** https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837
**Category:** Machine Learning
**Tags:** forwarddiff, reversediff
**Created:** [November 5, 2023, 8:23pm UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837 "2023-11-05T20:23:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nielsls](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nielsls/32/18482_2.png) [@nielsls](https://discourse.julialang.org/u/nielsls)
#### Post date: [November 5, 2023, 8:23pm UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837/1 "2023-11-05T20:23:31Z")

</div>

Using a combination of reverse- and forward-mode autodiff I have come across a highly efficient method for attributing the change in a differentiable scalar-valued function f: R^N-\>R^1 (e.g. a neural network) to the input factors.

As far as I can tell, the method of attributing the change in a neural network to the input factors is called [Integrated Gradients](https://www.tensorflow.org/tutorials/interpretability/integrated_gradients). However, to do this TensorFlow does slow, cumbersome Riemann integration.

Instead, for a sufficiently differentiable (!) network it may be done by doing nested forwardmode autodiff on: k(t) = grad(f)(x + tv) at t = 0 where x is the baseline and x + v is the observation to be explained.

For details see my write-up [higherorder\_pnl\_explain.pdf](https://github.com/nielsls/pnlexplain/blob/main/higherorder_pnl_explain.pdf)  
_(note: my field is finance where instead of a neural network we try to explain the change in a pricing function for a financial instrument - but the math is the same :))._

I’m pretty sure this is novel within finance but is it also novel within ML?  
Or did the TensorFlow guys just not bother doing forward-mode autodiff?

Thought/comments very welcome!

ping @ChrisRackauckas

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 5, 2023, 8:32pm UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837/2 "2023-11-05T20:32:53Z")

</div>

Yes this is the forward-over-reverse algorithm for Hessian-vector products (and Hessians). I mention it in the SciML book in “Forward-Over-Reverse and Hessian-Free Products”

[https://book.sciml.ai/notes/10-Basic\_Parameter\_Estimation-Reverse-Mode\_AD-and\_Inverse\_Problems/](https://book.sciml.ai/notes/10-Basic_Parameter_Estimation-Reverse-Mode_AD-and_Inverse_Problems/)

---

<div class="post-metadata">

### Author: ![nielsls](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nielsls/32/18482_2.png) [@nielsls](https://discourse.julialang.org/u/nielsls)
#### Post date: [November 5, 2023, 9:02pm UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837/3 "2023-11-05T21:02:02Z")

</div>

Yes, but not quite. One forward-pass over reverse only provides a 1st order approximation of the change in the gradient. Instead, the idea is to do **M** forward-passes over 1 reverse to efficiently approximate the _integrated gradient_ to order **M**.  
Also, wondering why the Google guys didn’t do it… Guess it’s hard in TF - would basically be a one-liner in Julia…

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [November 5, 2023, 9:12pm UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837/4 "2023-11-05T21:12:51Z")

</div>

Griewank’s book has a proof IIRC that M forward over 1 reverse is the optimal way to do higher order.

---

<div class="post-metadata">

### Author: ![nielsls](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nielsls/32/18482_2.png) [@nielsls](https://discourse.julialang.org/u/nielsls)
#### Post date: [November 7, 2023, 8:04am UTC](https://discourse.julialang.org/t/novel-method-for-feature-importance-using-reversemode-forwardmode-autodiff/105837/5 "2023-11-07T08:04:33Z")

</div>

Don’t have access to the Griewank book - although it does sound very interesting!

Anyways, for future reference (realizing the initial post was less clear) the key thing is that for a differentiable function f and vectors x,v, we can efficiently calculate the vector G to order M where G fulfills the decomposition: f(x+v) - f(x) = v’G.

Intensive googling suggests this trick is not widely known. Yet I imagine it has many interesting applications - not least in ML and finance.
