Optimization.jl: Method error while using AutoReverseDiff

Dear all,

I’ve been wrestling with this optimization setup all day. I feel like I was close to finishing, but I’ve seem to hit another speed bump. Here is the part of the code that generates the method error. The code will obviously not run. I don’t provide a MWE because I feel this problem is really particular to my_model(). The types of the arguments can be found in the error message. Does it have anything to do with these types?

my_model(params) = -likeli(model_elements, par, param_sizes, priors, meas_ind, method_options)[1] 
param_vector     = rand(1000)
lb               = vcat(repeat([-Inf], 500), repeat([0], 500))
ub               = repeat([Inf], 1000)
f                = OptimizationFunction(my_model, Optimization.AutoReverseDiff())
prob             = OptimizationProblem(f, param_vector, lb=lb, ub=ub)  
sol              = solve(prob, LBFGS())
par_final        = sol.u

MethodError: no method matching (::var"#my_model#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}})(::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}, ::SciMLBase.NullParameters)
Closest candidates are:
  (::var"#my_model#187")(::Any) at ...
Stacktrace:
  [1] (::Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters})(::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
    @ Optimization ~/.julia/packages/Optimization/NmJMd/src/function/reversediff.jl:50
  [2] (::Optimization.var"#126#137"{Tuple{}, Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}})(x::ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}})
    @ Optimization ~/.julia/packages/Optimization/NmJMd/src/function/reversediff.jl:53
  [3] ReverseDiff.GradientTape(f::Optimization.var"#126#137"{Tuple{}, Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}}, input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}})
    @ ReverseDiff ~/.julia/packages/ReverseDiff/5MMPp/src/api/tape.jl:199
  [4] gradient!(result::Vector{Float64}, f::Function, input::Vector{Float64}, cfg::ReverseDiff.GradientConfig{ReverseDiff.TrackedArray{Float64, Float64, 1, Vector{Float64}, Vector{Float64}}})
    @ ReverseDiff ~/.julia/packages/ReverseDiff/5MMPp/src/api/gradients.jl:41
  [5] (::Optimization.var"#125#136"{Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}})(::Vector{Float64}, ::Vector{Float64})
    @ Optimization ~/.julia/packages/Optimization/NmJMd/src/function/reversediff.jl:53
  [6] (::OptimizationOptimJL.var"#5#13"{OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, OptimizationOptimJL.var"#4#12"{OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, OptimizationFunction{false, Optimization.AutoReverseDiff, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Optimization.var"#125#136"{Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}}, Optimization.var"#128#139"{Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}}, Optimization.var"#134#145", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}}, OptimizationFunction{false, Optimization.AutoReverseDiff, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Optimization.var"#125#136"{Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}}, Optimization.var"#128#139"{Optimization.var"#124#135"{OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, SciMLBase.NullParameters}}, Optimization.var"#134#145", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}})(G::Vector{Float64}, θ::Vector{Float64})
    @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/iyLQi/src/OptimizationOptimJL.jl:106
  [7] value_gradient!!(obj::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, x::Vector{Float64})
    @ NLSolversBase ~/.julia/packages/NLSolversBase/cfJrN/src/interface.jl:82
  [8] initial_state(method::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, options::Optim.Options{Float64, OptimizationOptimJL.var"#_cb#11"{OptimizationOptimJL.var"#9#17", BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, Base.Iterators.Cycle{Tuple{Optimization.NullData}}}}, d::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64})
    @ Optim ~/.julia/packages/Optim/Zq1jM/src/multivariate/solvers/first_order/bfgs.jl:94
  [9] optimize(d::TwiceDifferentiable{Float64, Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, method::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, options::Optim.Options{Float64, OptimizationOptimJL.var"#_cb#11"{OptimizationOptimJL.var"#9#17", BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, Base.Iterators.Cycle{Tuple{Optimization.NullData}}}})
    @ Optim ~/.julia/packages/Optim/Zq1jM/src/multivariate/optimize/optimize.jl:36
 [10] ___solve(prob::OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, opt::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat}, data::Base.Iterators.Cycle{Tuple{Optimization.NullData}}; callback::Function, maxiters::Nothing, maxtime::Nothing, abstol::Nothing, reltol::Nothing, progress::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OptimizationOptimJL ~/.julia/packages/OptimizationOptimJL/iyLQi/src/OptimizationOptimJL.jl:143
 [11] ___solve
    @ ~/.julia/packages/OptimizationOptimJL/iyLQi/src/OptimizationOptimJL.jl:69 [inlined]
 [12] #__solve#2
    @ ~/.julia/packages/OptimizationOptimJL/iyLQi/src/OptimizationOptimJL.jl:56 [inlined]
 [13] __solve (repeats 2 times)
    @ ~/.julia/packages/OptimizationOptimJL/iyLQi/src/OptimizationOptimJL.jl:44 [inlined]
 [14] #solve#487
    @ ~/.julia/packages/SciMLBase/mOGJz/src/solve.jl:71 [inlined]
 [15] solve(::OptimizationProblem{true, OptimizationFunction{true, Optimization.AutoReverseDiff, var"#SSM#187"{StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, MethodOptions{Bool, String, Int64, Float64}}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, Vector{Float64}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, ::BFGS{LineSearches.InitialStatic{Float64}, LineSearches.HagerZhang{Float64, Base.RefValue{Bool}}, Nothing, Nothing, Flat})
    @ SciMLBase ~/.julia/packages/SciMLBase/mOGJz/src/solve.jl:71
 [16] SSM_optimize(model_elem::StateSpaceModel{Vector{Float64}, Matrix{Float64}, Vector{Matrix{Float64}}, Int64}, method_options::MethodOptions{Bool, String, Int64, Float64})
    @ Main ~/...
 [17] top-level scope
    @ ~/...

my_model(params) only takes in one argument. It’s saying you need a function of two arguments, my_model(params,_) since you don’t use the hyperparameters.

2 Likes

Amazing. Thank you! Changing the function definition to my_model(params,_) did the trick.