So I have a function f (minus the log-likelihood ℓ retrieved from Turing in my case) and I need two things:
1.) gradient (∇f)(x) in some point x
2.) and how it changes in a direction θ:
∂ₜ(t -> ∇f(x + t*θ)) in t = 0
So I figured out how to compute it in one run
return function (y, x, θ)
x_ = x + Dual{:hSrkahPmmC}(0.0, 1.0)*θ
y_ = ForwardDiff.gradient(x->-ℓ(x), x_)
y .= value.(y_)
y, dot(θ, y_).partials[]
end
but I got warned that this might create tag-order problems if ℓ uses ForwardDiff down the road - perhaps (I am not sure because the outer gradient wraps the inner custom tag. Any pointers for me?