# Optimization.solve, accessing the number of iterations

**URL:** <https://discourse.julialang.org/t/optimization-solve-accessing-the-number-of-iterations/92402>\
**Category:** General Usage\
**Created:** [January 2, 2023, 12:57am UTC](https://discourse.julialang.org/t/optimization-solve-accessing-the-number-of-iterations/92402 "2023-01-02T00:57:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [January 2, 2023, 12:57am UTC](https://discourse.julialang.org/t/optimization-solve-accessing-the-number-of-iterations/92402/1 "2023-01-02T00:57:07Z")

</div>

When using `Optimization.jl` to perform function minimization or weight evaluation in neural networks using Lux, how can I access the iteration number and other optimization characteristics that vary in time from the `callback` function? I am currently inserting data I need in a global dictionary. But my specific problem is accessing the iteration number in the optimization procedure. Any advice? Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 2, 2023, 5:24am UTC](https://discourse.julialang.org/t/optimization-solve-accessing-the-number-of-iterations/92402/2 "2023-01-02T05:24:24Z")

</div>

It’s not accessible in the solution object right now, but could be. Open an issue for it, but that will take a bit. In the mean time, just use a Ref and count calls of callbacks or the objective function, depending on what you’re trying to measure.

---

<div class="post-metadata">

**Author:** ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)\
**Post date:** [January 2, 2023, 7:56am UTC](https://discourse.julialang.org/t/optimization-solve-accessing-the-number-of-iterations/92402/3 "2023-01-02T07:56:32Z")

</div>

Since you can access the original result from the optimizer in the `original` field of the `OptimizationSolution` if the optimizer library stores these fields that you need you can access them from there.

Example with Optim

```julia
using Optimization, ForwardDiff, Zygote

rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
_p = [1.0, 100.0]

f = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff())
l1 = rosenbrock(x0, _p)
prob = OptimizationProblem(f, x0, _p)

## Optim.jl Solvers

using OptimizationOptimJL

# Start with some derivative-free optimizers

sol = solve(prob, SimulatedAnnealing())

sol.original

sol.original.iterations

```
