Optimization.jl interface for Turing models

I feel like I’ve seen an example somewhere of how to construct an Optimization.OptimizationFunction from a Turing model, but am not finding it at the moment. Can anyone help me find it? Did I imagine this functionality?

I can use the Optim.jl interface for MLE/MAP estimation no problem, but would like to try fitting some models with the wider galaxy of solvers that are easily available through Optimization.jl.

1 Like

Giving this a bump…the example below lets me create an OptimizationFunction and OptimizationProblem from the Turing model, but throws a StackOverflowError when I try to solve it. Is this a bug, or just functionality that isn’t implemented?

using Turing
using Optimization, OptimizationOptimJL

@model function example(x, y)
    a ~ Normal()
    b ~ Normal()
    σ ~ Gamma()

    μ = a .+ b .* x
    y ~ MvNormal(μ, σ)

x = rand(10)
y = 1 .+ 2x + randn(10)

m = example(x, y)
f = OptimizationFunction(m)
p = OptimizationProblem(f, zeros(3))
solve(p, NelderMead())
1 Like

There is an exported interface, but it’s not documented:

julia> p = optim_problem(m, MAP());

julia> solve(p.prob, NelderMead())
retcode: Success
u: 3-element Vector{Float64}:
1 Like

Thanks! I knew I’d seen it somewhere. Would a documentation PR be wanted, or is this interface still experimental?

I don’t know of there’s a reason. I’d recommend opening an issue first to find out before putting in the work.