Output of optimize() in Optim package

651 views
Skip to first unread message

Oliver Lylloff

unread,
Jan 4, 2014, 10:52:58 AM1/4/14
to julia...@googlegroups.com
Hello all,

I'm trying to get acquainted with the Optim package - so far I think everything looks very interesting.
I would like to get the solution vector of each iteration as an output (e.g. for plotting) - how do I do that?

Trying the Rosenbrock example from https://github.com/JuliaOpt/Optim.jl with extended_trace option

optimize(f,g!,[0.0,0.0],method=:gradient_descent,extended_trace=true)

prints the solution vector x at each iteration but I can't seem to access it and store it. Any ideas?


Best, 
Oliver


John Myles White

unread,
Jan 4, 2014, 10:56:03 AM1/4/14
to julia...@googlegroups.com
Hi Oliver,

The result of optimize is an object with a field called minimum that has the solution.

Try something like the following:

julia> res = optimize(x -> (10.0 - x[1])^2, [0.0], method = :gradient_descent)
Results of Optimization Algorithm
* Algorithm: Gradient Descent
* Starting Point: 0

* Minimum: 10.000000000118629

* Value of Function at Minimum: 0.000000
* Iterations: 1
* Convergence: true
* |x - x'| < 1.0e-32: false
* |f(x) - f(x')| / |f(x)| < 1.0e-08: false
* |g(x)| < 1.0e-08: true
* Exceeded Maximum Number of Iterations: false
* Objective Function Calls: 4
* Gradient Call: 4

julia> res.minimum
1-element Array{Float64,1}:
10.0

— John

Oliver Lylloff

unread,
Jan 4, 2014, 11:14:59 AM1/4/14
to julia...@googlegroups.com
Hi John,
Thanks for the fast answer. 

I still don't think I get the output I want. The res.minimum only return the converged solution (one vector), however I would like to get the minimum for all 1 to N iterations (N vectors). Setting store_trace=true gets me closer, since res.trace prints the information I'm seeking but not as an field (res.trace.x).

Hope it makes sense.
Best,
Oliver

John Myles White

unread,
Jan 4, 2014, 11:19:56 AM1/4/14
to julia...@googlegroups.com
Hi Oliver,

If you’re looking for the current state at each iteration, you want to check res.trace.states.

One way to field out this kind of information is to use the names function:

julia> names(res)
15-element Array{Symbol,1}:
:method
:initial_x
:minimum
:f_minimum
:iterations
:iteration_converged
:x_converged
:xtol
:f_converged
:ftol
:gr_converged
:grtol
:trace
:f_calls
:g_calls

julia> names(res.trace)
1-element Array{Symbol,1}:
:states

Of course, you still need to figure out what the fields mean, but we’ve tried to use sensible names for the fields in Optim.

— John

Oliver Lylloff

unread,
Jan 4, 2014, 12:04:03 PM1/4/14
to julia...@googlegroups.com
Thanks John, 

Still trying to get a hold of the basics but this seemed to do the trick:

res = optimize(f,g!,[0.0, 0.0], method = :gradient_descent,store_trace=true,extended_trace=true);

n = res.iterations+1;
x_iter = zeros(2,n);
for i = 1:n
  x_iter[:,i] = res.trace.states[i].metadata["x"];
end

Best, 
Oliver
Reply all
Reply to author
Forward
0 new messages