Optimization.jl
Optimization.jl copied to clipboard
Static arrays + autodiff
I tried using static vectors for parameters, which works nicely. But not when combined with autodiff:
julia> using Optimization, OptimizationOptimJL, StaticArrays, ForwardDiff
# don't specify inplace/outofplace:
julia> of = OptimizationFunction((x, p) -> sum(x), Optimization.AutoForwardDiff())
julia> prob = OptimizationProblem(of, SVector(0., 0.), nothing)
julia> solve(prob, Optim.GradientDescent())
ERROR: setindex!(::SVector{2, Float64}, value, ::Int) is not defined.
# specify out of place:
julia> of = OptimizationFunction{false}((x, p) -> sum(x), Optimization.AutoForwardDiff())
julia> prob = OptimizationProblem(of, SVector(0., 0.), nothing)
julia> solve(prob, Optim.GradientDescent())
ERROR: Use OptimizationFunction to pass the derivatives or automatically generate them with one of the autodiff backends
Is this expected?
The ForwardDiff support for StaticArrays need to avoid the mutable DiffResults that we currently have it setup with. So I guess it's expected in that, now that you have mentioned it in an issue, I can see why it fails. Though I think it was just untested before. Not hard to fix though, but needs the effort.
Beware that static arrays don't play nice with DiffResults sometimes:
https://github.com/JuliaDiff/DiffResults.jl/issues/26