Gradients
The supertype Gradient comprises different ways of taking gradients:
We first start by showing GradientAutodiff:
f(x::AbstractArray) = sum(x .^ 2)
x = rand(3)
grad = GradientAutodiff(f, x)GradientAutodiff{Float64, typeof(Main.f), ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(Main.f), Float64}, Float64, 3, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(Main.f), Float64}, Float64, 3}}}}(Main.f, ForwardDiff.GradientConfig{ForwardDiff.Tag{typeof(Main.f), Float64}, Float64, 3, Vector{ForwardDiff.Dual{ForwardDiff.Tag{typeof(Main.f), Float64}, Float64, 3}}}((Partials(1.0, 0.0, 0.0), Partials(0.0, 1.0, 0.0), Partials(0.0, 0.0, 1.0)), ForwardDiff.Dual{ForwardDiff.Tag{typeof(Main.f), Float64}, Float64, 3}[Dual{ForwardDiff.Tag{typeof(Main.f), Float64}}(0.0,5.0e-324,1.0e-323,1.0e-323), Dual{ForwardDiff.Tag{typeof(Main.f), Float64}}(2.0e-323,2.5e-323,3.0e-323,3.5e-323), Dual{ForwardDiff.Tag{typeof(Main.f), Float64}}(4.0e-323,4.4e-323,5.0e-323,5.0e-324)]))When calling an instance of Gradient we can use the functions gradient and gradient![1]:
gradient(x, grad)3-element Vector{Float64}:
1.042427591070766
1.1736135149066969
1.7817573961855622- 1Internally these functions call functors that are implemented for the individual
structs derived fromGradient, but for consistency (especially with regards toOptimizerProblems) we recommend using the functionsgradientandgradient!.