JuliaNLSolvers / NLSolversBase.jl

Base package for optimization and equation solver software in JuliaNLSolvers
Other
30 stars 28 forks source link

non-modifying fg #106

Closed tpapp closed 4 years ago

tpapp commented 5 years ago

This is more of a support request than an issue, I could not find this in the docs. I have a closure fg which calculates values and derivatives, eg

fx, gx = fg(x)

For an MWE, consider

fg(x) = 0.5*sum(abs2, x), x

How can I wrap this in OnceDifferentiable? I want to use this with Optim.jl.

tpapp commented 5 years ago

Here is an MWE with only_fg!, the question is whether this can be simplified:

using Optim
using NLSolversBase: only_fg!
fg(x) = 0.5*sum(abs2, x), x
function fg!(F, G, x)
    f, g = fg(x)
    G ≠ nothing && copy!(G, g)
    f
end
optimize(OnceDifferentiable(only_fg!(fg!), ones(3)), ones(3), LBFGS())
tpapp commented 5 years ago

Friendly bump, I still could not figure out a better solution (but the above works).

pkofod commented 5 years ago

I'm not sure why I didn't get any notifications here, sorry, didn't see it until now. Don't hesitate tagging me in the future :)

Are you looking for something that only takes in x or which part is it that is important to you?

tpapp commented 5 years ago

I simply have code that calculates a function and its gradient, and I am wondering if I can just make a OnceDifferentiable out of it without making a modifying wrapper.

Or, failing that, if my solution above is the recommended one.

pkofod commented 5 years ago

Why am I not getting any notifications for this repo? I have set it to watching, but I didn't get this notification, nor Tim's PR that I just merged...

pkofod commented 5 years ago

did you try the OnceDifferentiable(only_fg(fg)) route? I think that's been included for quite a while.

tpapp commented 5 years ago

No, I went with only_fg!, code here.

Where is only_fg documented? I am not sure how to use it.

pkofod commented 5 years ago

It's not, but


function f(x)
  sum(x->x^2,x)
end
g(x) = 2 .* x
fg(x) = f(x), g(x)
od_fg = OnceDifferentiable(only_fg(fg), x)
tpapp commented 5 years ago

Thanks. I may modify my code above.

My main concern about this package (in general) is that API stability commitments are unclear, so I am wary of using undocumented functions in stable packages. Some documentation (ie the usual Documenter.jl-generated pages and docstrings) would help a lot.

pkofod commented 5 years ago

Thanks. I may modify my code above.

My main concern about this package (in general) is that API stability commitments are unclear, so I am wary of using undocumented functions in stable packages. Some documentation (ie the usual Documenter.jl-generated pages and docstrings) would help a lot.

Okay, I guess I can say what is expected to be stable. But breaking changes should only happen without major versions for things that are not exported.

Overall, most of this package should be expected to be stable, as most new development is spent elsewhere and will superceed this.

tpapp commented 5 years ago

most new development is spent elsewhere and will superceed this

can you tell me where that is happening, if there is a public repo already?

tkf commented 5 years ago

@tpapp I think the repo is https://github.com/pkofod/NLSolvers.jl and the plan is presented in https://www.youtube.com/watch?v=GfRSPEhewwM

pkofod commented 5 years ago

I don't always keep it up to date, but it does work to some extent :) Some of it is coherent, some of it is thrown in ! I have some recent changes off-line based on discussions with @tkf on problematic updates (damped BFGS, etc), and I hope to push a proper More & Sorensen trust region sub-problem solver soon.