# AD with respect to Flux parameters

**URL:** <https://discourse.julialang.org/t/ad-with-respect-to-flux-parameters/76468>\
**Category:** Machine Learning\
**Created:** [February 15, 2022, 3:38am UTC](https://discourse.julialang.org/t/ad-with-respect-to-flux-parameters/76468 "2022-02-15T03:38:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![raktim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raktim/32/11188_2.png) [@raktim](https://discourse.julialang.org/u/raktim)\
**Post date:** [February 15, 2022, 3:38am UTC](https://discourse.julialang.org/t/ad-with-respect-to-flux-parameters/76468/1 "2022-02-15T03:38:15Z")

</div>

I am trying to compute gradient of a function with respect to Flux parameters.  
Example:

```julia
using Flux, ForwardDiff

function f(x::Vector)
    return [x[2]; -x[1] + (1 - x[1]^2) * x[2]]
end

# Function approximation
n = 2; 
fhat = Chain(Dense(2,n),Dense(n,n,sigmoid),Dense(n,1));
ps = Flux.params(fhat);

# Define vector field and divergence
F(x) = f(x) * uhat(x)[1];    
divF(x) = Flux.tr(ForwardDiff.jacobian(F,x)); 

# Compute gradients w.r.t function parameters
x = [1,2];
grads1 = gradient(() -> sum(F(x)), ps); @show grads[ps[1]] # Works as expected.
grads2 = gradient(() -> divF(x), ps); @show grads[ps[1]] # Nothing.

```

grads2 returns nothing  
How can I obtain derivative of divF(x) w.r.t ps?

Thanks.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [February 15, 2022, 5:11am UTC](https://discourse.julialang.org/t/ad-with-respect-to-flux-parameters/76468/2 "2022-02-15T05:11:17Z")

</div>

`ForwardDiff.jacobian(F, x)` does not know about derivatives with respect to `F`. Maybe we should make that an error again…

(Your variable names require some guessing.)
