Optim.jl callback

How to work properly with OptimizationState?

In docs I found only os.iteration.

Some like os.metadata["x"] not working with error: type Array has no field metadata

Is callback documented anywhere?

Thx!

1 Like

I also needed the history of parameters values. @pkofod answered on slack that you need to turn on the extended trace for that. Then you will have "x" in the dictionary passed to the callback.

Example:

using OptimTestProblems, Optim
problem = MultivariateProblems.UnconstrainedProblems.examples["Rosenbrock"]
f = MultivariateProblems.objective(problem)
g! = MultivariateProblems.gradient(problem)
initial_x = problem.initial_x
d2 = OnceDifferentiable(f, g!, initial_x)
method = LBFGS()
xs = []
cb = tr -> begin
            push!(xs, tr[end].metadata["x"])
            false
        end

options = Optim.Options(callback = cb, show_every=3, store_trace=true, extended_trace=true)
optimize(d2, initial_x, method, options)
xs
3 Likes