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

Add constructor to convert NLLS to OptimizationProblem #538

Merged
merged 3 commits into from
Nov 8, 2023

Conversation

Vaibhavdixit02
Copy link
Member

@Vaibhavdixit02 Vaibhavdixit02 changed the title [WIP] Add constructor to convert NLLS to OptimationProblem [WIP] Add constructor to convert NLLS to OptimizationProblem Nov 4, 2023
Copy link

codecov bot commented Nov 4, 2023

Codecov Report

Merging #538 (5d3ab7a) into master (ffe68ae) will decrease coverage by 0.10%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           master     #538      +/-   ##
==========================================
- Coverage   41.07%   40.98%   -0.10%     
==========================================
  Files          53       53              
  Lines        4051     4060       +9     
==========================================
  Hits         1664     1664              
- Misses       2387     2396       +9     
Files Coverage Δ
src/problems/basic_problems.jl 65.65% <0.00%> (-6.57%) ⬇️

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

if isinplace(prob)
throw(ArgumentError("Converting NonlinearLeastSquaresProblem to OptimizationProblem is not supported with in-place functions yet."))
end
optf = OptimizationFunction(sum ∘ prob.f, grad = (Jv, u, p) -> prob.f.jvp(Jv, prob.f(u, p), u, p), kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assumes jvp is defined? That's a pretty strong assumption?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah..I realise now that I was assuming this to come from being populated by an instatiate_function equivalent but it'll have to be provided by the user to be available here right?

if isinplace(prob)
throw(ArgumentError("Converting NonlinearLeastSquaresProblem to OptimizationProblem is not supported with in-place functions yet."))
end
optf = OptimizationFunction(sum ∘ prob.f, grad = (Jv, u, p) -> prob.f.jvp(Jv, prob.f(u, p), u, p), kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sum(abs2, prob.f(u,p)), not just sum

Copy link
Member Author

@Vaibhavdixit02 Vaibhavdixit02 Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the loss function for NLLS already has the square

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avik-pal wait, I thought the interface would be the same as nonlinearsolve, i.e. returning the vector of residuals. f(du,u,p) and then sum(abs2,du) is the loss. Now I'm looking at the tests and see the user is required to write it. Having that summation be implicit enforces it's actually a least squares problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right it is same as NonlinearProblem, sum ∘ prob.f should be sum(abs2,....)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC what Chris meant was the other way around, the NLLS problem should do the squaring implicitly and not ask the user to do that (and then the loss here would just be the sum)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I see what happened. SciML/NonlinearSolve.jl@b8aca89 the original test was just weird, but it was since fixed SciML/NonlinearSolve.jl@883a392. This means it's sum(abs2,...).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I had seen the previous version and added this

@Vaibhavdixit02 Vaibhavdixit02 force-pushed the nllstoopt branch 2 times, most recently from 5f200fa to 4856a9d Compare November 7, 2023 19:31
@ChrisRackauckas
Copy link
Member

Looks ready to merge? In-place is going to be much more common than out of place, so that would be good to support, but a proper error message is fine for now.

Comment on lines 1 to 2
using NonlinearSolve, Optimization, OptimizationNLopt, ForwardDiff
import FastLevenbergMarquardt, LeastSquaresOptim
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These downstream deps would be required. But just remove FastLevenbergMarquardt and LeastSquaresOptim?

Co-authored-by: Christopher Rackauckas <[email protected]>
@Vaibhavdixit02
Copy link
Member Author

Yeah should be ready. But I don't completely like the interface, it needs the adtype as positional arg for the OptimizationProblem constructor for the "automatic" conversion but can't see any other way to do it for now.

@Vaibhavdixit02 Vaibhavdixit02 changed the title [WIP] Add constructor to convert NLLS to OptimizationProblem Add constructor to convert NLLS to OptimizationProblem Nov 7, 2023
@Vaibhavdixit02 Vaibhavdixit02 merged commit 88890d5 into master Nov 8, 2023
34 of 39 checks passed
@Vaibhavdixit02 Vaibhavdixit02 deleted the nllstoopt branch November 8, 2023 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to create OptimizationProblem from NonlinearLeastSquaresProblem
3 participants