Optim.jl, showing trace

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:


I just want the lines with “time” not to be shown.

I currently use:

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!

1 Like

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

1 Like

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 :

https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/#early-stopping

Thanks! I’ll try this.

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

https://julianlsolvers.github.io/Optim.jl/stable/#user/tipsandtricks/#provide-gradients

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

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> 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ᵢ-ȳ)²)/n ≤ 1.0e-08

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

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!

1 Like

Would it be better if it was just one line?

1 Like

I guess so, if it is possible.