# Optim.jl, showing trace

**URL:** https://discourse.julialang.org/t/optim-jl-showing-trace/55258
**Category:** New to Julia
**Created:** [February 14, 2021, 4:02pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258 "2021-02-14T16:02:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)
#### Post date: [February 14, 2021, 4:02pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/1 "2021-02-14T16:02:41Z")

</div>

Is there a way of not showing the time spent in each iteration in Optim.jl while using the option show\_trace=true?

The current output is as follows:

 ![Screen Shot 2021-02-14 at 11.00.18 AM](https://global.discourse-cdn.com/julialang/original/3X/4/4/44f3984342006ff45914f1755c74ff21e7cbdedc.png)  
I just want the lines with “time” not to be shown.

I currently use:

```julia
res = optimize(p->objectivefunc!(p,fp,ip),initp0,LBFGS(),
				Optim.Options(show_trace = true, show_every = 10,
                iterations=10_000, g_tol=1e-3))

```

Thanks!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 14, 2021, 5:40pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/2 "2021-02-14T17:40:15Z")

</div>

You can just put `print` statements in your objective function (or a wrapper thereof) and print out whatever information you want, including timing.

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [February 14, 2021, 7:35pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/3 "2021-02-14T19:35:58Z")

</div>

You can pass a “callback” fonction that will be called at each iteration, I think you’ll have access to more things in there, see this example :

> **[Optim.jl](https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/%23early-stopping)**
>
> Pure Julia implementations of optimization algorithms.

---

<div class="post-metadata">

### Author: ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)
#### Post date: [February 15, 2021, 3:04am UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/4 "2021-02-15T03:04:25Z")

</div>

Thanks! I’ll try this.

When I used showing trace a couple of months ago, the output was similar to the one shown here:

> **[Optim.jl](https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/%23provide-gradients)**
>
> Pure Julia implementations of optimization algorithms.

Do you know why it changed to include the cumulative time spent after each iteration?

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [February 15, 2021, 9:02am UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/5 "2021-02-15T09:02:09Z")

</div>

Some people asked for for it and I figured it was a fair request. Ideologically I agree with @stevengj that it’s probably easier to just `@info` from your objective function code.

```julia
julia> using Optim

julia> function rosenbrock(x)
           fx = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
           @info "Current objective: $fx"
           return fx
       end
rosenbrock (generic function with 1 method)

julia> result = optimize(rosenbrock, zeros(2), NelderMead())
[ Info: Current objective: 1.0
[ Info: Current objective: 0.9506640624999999
[ Info: Current objective: 1.0625
[ Info: Current objective: 1.0162890624999998
[ Info: Current objective: 0.9793678283691405
[ Info: Current objective: 0.9351905822753908
[ Info: Current objective: 0.9262175083160399
[ Info: Current objective: 0.8744485569000243
[ Info: Current objective: 0.8292372137308122
[ Info: Current objective: 0.9110676389932634
[ Info: Current objective: 0.8138906955718994
[ Info: Current objective: 0.8569140625
[ Info: Current objective: 0.7569606018066407
[ Info: Current objective: 0.7796759015694261
[ Info: Current objective: 0.7382898432016374
[ Info: Current objective: 0.8093360811471937
[ Info: Current objective: 0.6989375501871109
[ Info: Current objective: 0.7439988190308213
[ Info: Current objective: 0.6800415039062501
[ Info: Current objective: 0.7863740634918209
[ Info: Current objective: 0.6475000095367432
[ Info: Current objective: 0.7074711001291873
[ Info: Current objective: 0.6377041727304458
[ Info: Current objective: 0.7910908132791517
[ Info: Current objective: 0.5977620035409928
[ Info: Current objective: 0.66487556938082
[ Info: Current objective: 0.612618417739868
[ Info: Current objective: 0.54761962890625
[ Info: Current objective: 0.613777124322951
[ Info: Current objective: 0.8372292143106463
[ Info: Current objective: 0.5449374371790326
[ Info: Current objective: 0.5091263252892531
[ Info: Current objective: 0.5401532957355083
[ Info: Current objective: 0.6077245319262143
[ Info: Current objective: 0.524953512535576
[ Info: Current objective: 0.4830025219453091
[ Info: Current objective: 0.45644345282505117
[ Info: Current objective: 0.46051409675915245
[ Info: Current objective: 0.4038318521156908
[ Info: Current objective: 0.3653678089009192
[ Info: Current objective: 0.3850017340504563
[ Info: Current objective: 0.2993955551182538
[ Info: Current objective: 0.3055259815961579
[ Info: Current objective: 0.38803603576002554
[ Info: Current objective: 0.3481737280200155
[ Info: Current objective: 0.2860716424189399
[ Info: Current objective: 0.25977773590695985
[ Info: Current objective: 0.22658525620344372
[ Info: Current objective: 0.2700946212239731
[ Info: Current objective: 0.18341815570096573
[ Info: Current objective: 0.14448917088453422
[ Info: Current objective: 0.26328247079881417
[ Info: Current objective: 0.2123452757235843
[ Info: Current objective: 0.15520836399488133
[ Info: Current objective: 0.09866232875869388
[ Info: Current objective: 0.17137429279761307
[ Info: Current objective: 0.15896089698158278
[ Info: Current objective: 0.12940486018220818
[ Info: Current objective: 0.08124909513063966
[ Info: Current objective: 0.06483338481155441
[ Info: Current objective: 0.1686747588580646
[ Info: Current objective: 0.09799019150506968
[ Info: Current objective: 0.06322654293258788
[ Info: Current objective: 0.054847295607669294
[ Info: Current objective: 0.048796232743291285
[ Info: Current objective: 0.18967896442456766
[ Info: Current objective: 0.02140622777909333
[ Info: Current objective: 0.012198511818044756
[ Info: Current objective: 0.2141473318081354
[ Info: Current objective: 0.031252023584177786
[ Info: Current objective: 0.035642499363768176
[ Info: Current objective: 0.02175704620437325
[ Info: Current objective: 0.005976420650439886
[ Info: Current objective: 0.04008580579161205
[ Info: Current objective: 0.050187124425719015
[ Info: Current objective: 0.011045557970051096
[ Info: Current objective: 0.003594513509942018
[ Info: Current objective: 0.0026241908943930767
[ Info: Current objective: 0.011254309922952038
[ Info: Current objective: 0.005703555774137361
[ Info: Current objective: 0.015513778261616666
[ Info: Current objective: 0.0031982017037336346
[ Info: Current objective: 0.00035825898674718686
[ Info: Current objective: 0.001738947727433266
[ Info: Current objective: 0.0009040182147285054
[ Info: Current objective: 0.001049236630228722
[ Info: Current objective: 4.826749182911818e-5
[ Info: Current objective: 0.001712290645153398
[ Info: Current objective: 0.00017847404508271005
[ Info: Current objective: 0.00043867663096285904
[ Info: Current objective: 7.203738226920685e-5
[ Info: Current objective: 0.00029615089252058095
[ Info: Current objective: 3.58990668454171e-5
[ Info: Current objective: 0.00011887288106227005
[ Info: Current objective: 1.3316611971068665e-5
[ Info: Current objective: 0.00021073592484931598
[ Info: Current objective: 1.5652129074070739e-6
[ Info: Current objective: 3.438139288298314e-5
[ Info: Current objective: 9.733838927724708e-6
[ Info: Current objective: 3.4340881092973294e-5
[ Info: Current objective: 3.7230897197382935e-6
[ Info: Current objective: 2.1568773924248783e-6
[ Info: Current objective: 8.591973262003623e-6
[ Info: Current objective: 4.966342403850991e-7
[ Info: Current objective: 6.34994842917792e-6
[ Info: Current objective: 2.272706709692903e-7
[ Info: Current objective: 3.6470606536887992e-6
[ Info: Current objective: 2.120248852111823e-7
[ Info: Current objective: 1.1117560718540069e-6
[ Info: Current objective: 6.942358374981032e-8
[ Info: Current objective: 7.921504079244867e-7
[ Info: Current objective: 1.876333755675703e-8
[ Info: Current objective: 4.500770302839567e-7
[ Info: Current objective: 3.335823380335548e-8
[ Info: Current objective: 1.4234874134854208e-7
[ Info: Current objective: 1.0995395204552551e-8
[ Info: Current objective: 3.5255270584829996e-9
 * Status: success

 * Candidate solution
    Final objective value: 3.525527e-09

 * Found with
    Algorithm: Nelder-Mead

 * Convergence measures
    √(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 60
    f(x) calls: 117

```

---

<div class="post-metadata">

### Author: ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)
#### Post date: [February 15, 2021, 2:22pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/6 "2021-02-15T14:22:59Z")

</div>

Sure, but then if there are 100 function evaluations in one iteration, you print this output 100 times instead of just once (or once every nth iteration) as in the show\_trace output.  
It would have been great if printing out the cumulative time was an option we can turn on or off (though I understand that it might only be me who finds this new feature slightly inconvenient).

Thanks!

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [February 15, 2021, 2:31pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/7 "2021-02-15T14:31:56Z")

</div>

Would it be better if it was just one line?

---

<div class="post-metadata">

### Author: ![newb\_gk](https://avatars.discourse-cdn.com/v4/letter/n/e19b73/32.png) [@newb\_gk](https://discourse.julialang.org/u/newb_gk)
#### Post date: [February 15, 2021, 2:39pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/8 "2021-02-15T14:39:16Z")

</div>

I guess so, if it is possible.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [June 14, 2023, 2:47pm UTC](https://discourse.julialang.org/t/optim-jl-showing-trace/55258/9 "2023-06-14T14:47:51Z")

</div>

4 posts were split to a new topic: [How to hide output from an optimizer?](https://discourse.julialang.org/t/how-to-hide-output-from-an-optimizer/100336)
