ForwardDiff.jl icon indicating copy to clipboard operation
ForwardDiff.jl copied to clipboard

use `HessianConfig` to calculate gradients?

Open longemen3000 opened this issue 9 months ago • 0 comments

Hello,

Recently i came across with a problem in which already had a hessian cache, (HessianConfig + DiffResults.HessianResult), but it seems there aren't any methods for calculating for using HessianConfig (and HessianResult) when calculating gradients (of the same function with the same number and type of arguments).

I did this dirty hack to allow calculating gradients with HessianConfig + HessianResult:

hresult,hconfig = cache    
h_jconfig = hconfig.jacobian_config
seeds = h_jconfig.seeds
duals = h_jconfig.duals[1]
gconfig = ForwardDiff.GradientConfig{Nothing,eltype(aux),length(seeds),typeof(duals)}(seeds,duals)
gresult = ForwardDiff.MutableDiffResult(result.value,(result.derivs[1],))

It works for me, but it could be generalized (by passing the adequate function argument, for example)

It would be much easier to just do ForwardDiff.gradient!(hresult,f,x,hconfig) instead.

longemen3000 avatar Feb 05 '25 07:02 longemen3000