Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get latest minimizer (u) instead of the best one for optimization? #740

Open
ThummeTo opened this issue May 7, 2024 · 5 comments
Labels
question Further information is requested

Comments

@ThummeTo
Copy link

ThummeTo commented May 7, 2024

Question❓

Hi,

I really like the quality of the documentation, however I struggle to find a functionality I am pretty sure that it exists:
If I get this correct, Optimization.jl always returns the minimizer u for the best (minimum/maximum) f evaluation - this is nice and often what I want.
However in some cases one is more interested in the latest u - so the last try to find a new optimum (think e.g. of having some stochastic effects in your f). Is there a keyword or similar?

I would assume to find this on this page of the docu:
Common-Solver-Options-(Solve-Keyword-Arguments)

Thanks in advance!

@ThummeTo ThummeTo added the question Further information is requested label May 7, 2024
@Vaibhavdixit02
Copy link
Member

I really like the quality of the documentation,

I am genuinely surprised 🤣

You can use a callback to store the us and pass it to the solve call -

ustore = []
function my_storing_callback(state, l)
    push!(ustore, state.u)
    return false
end

@ThummeTo
Copy link
Author

ThummeTo commented May 7, 2024

I am genuinely surprised 🤣

Yeah, I actually found the things that I'd been looking for, so 👍 (except the one above)

Thank you! Ok, this is actually the way I am doing it right now... maybe this is something for a feature request (e.g. adding u_latest to the result struct) or does this feel like an exotic requirement?

@Vaibhavdixit02
Copy link
Member

Since some solvers already store this in their results I haven't seen a reason to do this and increase the memory usage even more for the results (and you as the user can do it with the callback)

@ChrisRackauckas
Copy link
Member

If some solvers already store this, then you just use the same reference and there's no extra memory cost?

@ThummeTo
Copy link
Author

ThummeTo commented May 14, 2024

I have another example where this is very common: Stochastic batching when optimizing ML models... without the option to pick the latest instead of the best minimizer, the optimization will return the minimizer of the best batch element loss - which might be a completely outdated parameter set ...

Also: The callback gets called one last time for the best element after optimization finished, so with the code above:

ustore = []
function my_storing_callback(state, l)
    push!(ustore, copy(state.u)) # copy required because inplace modification of `state.u`
    return false
end
ustore[end] # the best value
ustore[end-1] # actually the latest value during optimization

I didn't check the implementation, but wouldn't it be possible to let a keyword decide if either the state best or latest gets stored without additional memory required?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants