# Optim.jl callback

**URL:** <https://discourse.julialang.org/t/optim-jl-callback/52625>\
**Category:** Optimization (Mathematical)\
**Created:** [December 30, 2020, 5:46pm UTC](https://discourse.julialang.org/t/optim-jl-callback/52625 "2020-12-30T17:46:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [December 30, 2020, 5:46pm UTC](https://discourse.julialang.org/t/optim-jl-callback/52625/1 "2020-12-30T17:46:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [January 26, 2021, 4:44pm UTC](https://discourse.julialang.org/t/optim-jl-callback/52625/2 "2021-01-26T16:44:16Z")

</div>

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:

```julia
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

```
