JuliaDiff / AbstractDifferentiation.jl

An abstract interface for automatic differentiation.
https://juliadiff.org/AbstractDifferentiation.jl/
MIT License
137 stars 18 forks source link

value_gradient_and_hessian for ForwardDiff returns gradient of type Dual #46

Closed sethaxen closed 2 years ago

sethaxen commented 2 years ago
julia> using AbstractDifferentiation

julia> ba = AD.ForwardDiffBackend();

julia> x = randn(3);

julia> AD.gradient(ba, sum, x)
([1.0, 1.0, 1.0],)

julia> AD.value_and_gradient(ba, sum, x)
(-0.056101093099405724, ([1.0, 1.0, 1.0],))

julia> AD.value_gradient_and_hessian(ba, sum, x)
(-0.056101093099405724, (ForwardDiff.Dual{ForwardDiff.Tag{ComposedFunction{typeof(AbstractDifferentiation.asarray), AbstractDifferentiation.var"#3#4"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, AbstractDifferentiation.var"#9#10"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, typeof(sum)}, Tuple{Vector{Float64}}}}, Float64}, Float64, 3}[Dual{ForwardDiff.Tag{ComposedFunction{typeof(AbstractDifferentiation.asarray), AbstractDifferentiation.var"#3#4"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, AbstractDifferentiation.var"#9#10"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, typeof(sum)}, Tuple{Vector{Float64}}}}, Float64}}(1.0,0.0,0.0,0.0), Dual{ForwardDiff.Tag{ComposedFunction{typeof(AbstractDifferentiation.asarray), AbstractDifferentiation.var"#3#4"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, AbstractDifferentiation.var"#9#10"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, typeof(sum)}, Tuple{Vector{Float64}}}}, Float64}}(1.0,0.0,0.0,0.0), Dual{ForwardDiff.Tag{ComposedFunction{typeof(AbstractDifferentiation.asarray), AbstractDifferentiation.var"#3#4"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, AbstractDifferentiation.var"#9#10"{AbstractDifferentiation.ForwardDiffBackend{Nothing}, typeof(sum)}, Tuple{Vector{Float64}}}}, Float64}}(1.0,0.0,0.0,0.0)],), ([0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0],))
mohamed82008 commented 2 years ago

I think we need one more primal_value method like https://github.com/JuliaDiff/AbstractDifferentiation.jl/blob/master/src/forwarddiff.jl#L41 for arrays of duals.