-
-
Notifications
You must be signed in to change notification settings - Fork 99
Improve the OptimizationManopt.jl interface #1009
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
Changes from 5 commits
a074422
a064672
98b43be
dbddd25
0ac3af5
efc42ea
b488dc4
1b4864d
18c558a
ec2183b
561d9e2
213760e
0a47945
b980305
2e46ca1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,20 +65,15 @@ function call_manopt_optimizer( | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = Manopt.AllocatingEvaluation(), | ||
| stepsize::Stepsize = ArmijoLinesearch(M), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| opts = gradient_descent(M, | ||
| opts = Manopt.gradient_descent(M, | ||
| loss, | ||
| gradF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| stepsize, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| return_state = true, # return the (full, decorated) solver state | ||
| kwargs... | ||
| ) | ||
| minimizer = Manopt.get_solver_result(opts) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opts) | ||
| end | ||
|
|
@@ -90,13 +85,9 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, opt::NelderMea | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| kwargs...) | ||
| opts = NelderMead(M, | ||
| loss; | ||
| return_state = true, | ||
| stopping_criterion, | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| opts = NelderMead(M, loss; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opts) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opts) | ||
| end | ||
|
|
@@ -109,20 +100,15 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| stepsize::Stepsize = ArmijoLinesearch(M), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| opts = conjugate_gradient_descent(M, | ||
| opts = Manopt.conjugate_gradient_descent(M, | ||
| loss, | ||
| gradF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| stepsize, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| kwargs... | ||
| ) | ||
| minimizer = Manopt.get_solver_result(opts) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opts) | ||
| end | ||
|
|
@@ -135,25 +121,11 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| population_size::Int = 100, | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| inverse_retraction_method::AbstractInverseRetractionMethod = default_inverse_retraction_method(M), | ||
| vector_transport_method::AbstractVectorTransportMethod = default_vector_transport_method(M), | ||
| kwargs...) | ||
| initial_population = vcat([x0], [rand(M) for _ in 1:(population_size - 1)]) | ||
| opts = particle_swarm(M, | ||
| loss; | ||
| x0 = initial_population, | ||
| n = population_size, | ||
| return_state = true, | ||
| retraction_method, | ||
| inverse_retraction_method, | ||
| vector_transport_method, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| swarm = [x0, [rand(M) for _ in 1:(population_size - 1)]...] | ||
| opts = particle_swarm(M, loss, swarm; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opts) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opts) | ||
| end | ||
|
|
@@ -167,28 +139,10 @@ function call_manopt_optimizer(M::Manopt.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| vector_transport_method::AbstractVectorTransportMethod = default_vector_transport_method(M), | ||
| stepsize = WolfePowellLinesearch(M; | ||
| retraction_method = retraction_method, | ||
| vector_transport_method = vector_transport_method, | ||
| linesearch_stopsize = 1e-12), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs... | ||
| ) | ||
| opts = quasi_Newton(M, | ||
| loss, | ||
| gradF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| retraction_method, | ||
| vector_transport_method, | ||
| stepsize, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| opts = quasi_Newton(M, loss, gradF, x0; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opts) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opts) | ||
| end | ||
|
|
@@ -200,19 +154,9 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| vector_transport_method::AbstractVectorTransportMethod = default_vector_transport_method(M), | ||
| basis = Manopt.DefaultOrthonormalBasis(), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| opt = cma_es(M, | ||
| loss, | ||
| x0; | ||
| return_state = true, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| opt = cma_es(M, loss, x0; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opt) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opt) | ||
| end | ||
|
|
@@ -224,22 +168,9 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| vector_transport_method::AbstractVectorTransportMethod = default_vector_transport_method(M), | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| opt = convex_bundle_method!(M, | ||
| loss, | ||
| gradF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| retraction_method, | ||
| vector_transport_method, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| opt = convex_bundle_method(M, loss, gradF, x0; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opt) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opt) | ||
| end | ||
|
|
@@ -252,21 +183,13 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| gradF, | ||
| x0; | ||
| hessF = nothing, | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| kwargs...) | ||
| opt = adaptive_regularization_with_cubics(M, | ||
| loss, | ||
| gradF, | ||
| hessF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| retraction_method, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
|
|
||
| opt = if isnothing(hessF) | ||
| adaptive_regularization_with_cubics(M, loss, gradF, x0; return_state = true, kwargs...) | ||
| else | ||
| adaptive_regularization_with_cubics(M, loss, gradF, hessF, x0; return_state = true, kwargs...) | ||
| end | ||
| minimizer = Manopt.get_solver_result(opt) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opt) | ||
| end | ||
|
|
@@ -279,21 +202,12 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| gradF, | ||
| x0; | ||
| hessF = nothing, | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| kwargs...) | ||
| opt = trust_regions(M, | ||
| loss, | ||
| gradF, | ||
| hessF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| retraction = retraction_method, | ||
| stopping_criterion, | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| opt = if isnothing(hessF) | ||
| trust_regions(M, loss, gradF, x0; return_state = true, kwargs...) | ||
| else | ||
| trust_regions(M, loss, gradF, hessF, x0; return_state = true, kwargs...) | ||
| end | ||
| minimizer = Manopt.get_solver_result(opt) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opt) | ||
| end | ||
|
|
@@ -305,22 +219,9 @@ function call_manopt_optimizer(M::ManifoldsBase.AbstractManifold, | |
| loss, | ||
| gradF, | ||
| x0; | ||
| stopping_criterion::Union{Manopt.StoppingCriterion, Manopt.StoppingCriterionSet}, | ||
| evaluation::AbstractEvaluationType = InplaceEvaluation(), | ||
| retraction_method::AbstractRetractionMethod = default_retraction_method(M), | ||
| stepsize::Stepsize = DecreasingStepsize(; length = 2.0, shift = 2), | ||
| kwargs...) | ||
| opt = Frank_Wolfe_method(M, | ||
| loss, | ||
| gradF, | ||
| x0; | ||
| return_state = true, | ||
| evaluation, | ||
| retraction_method, | ||
| stopping_criterion, | ||
| stepsize, | ||
| hessF=nothing, # ignore that keyword for this solver | ||
| kwargs...) | ||
| # we unwrap DebugOptions here | ||
| opt = Frank_Wolfe_method(M, loss, gradF, x0; return_state = true, kwargs...) | ||
| minimizer = Manopt.get_solver_result(opt) | ||
| return (; minimizer = minimizer, minimum = loss(M, minimizer), options = opt) | ||
| end | ||
|
|
@@ -332,20 +233,25 @@ function SciMLBase.requiresgradient(opt::Union{ | |
| AdaptiveRegularizationCubicOptimizer, TrustRegionsOptimizer}) | ||
| true | ||
| end | ||
| # TODO: WHY? they both still accept not passing it | ||
| function SciMLBase.requireshessian(opt::Union{ | ||
| AdaptiveRegularizationCubicOptimizer, TrustRegionsOptimizer}) | ||
| true | ||
| end | ||
|
|
||
|
||
| function build_loss(f::OptimizationFunction, prob, cb) | ||
| function (::AbstractManifold, θ) | ||
| # TODO: I do not understand this. Why is the manifold not used? | ||
| # Either this is an Euclidean cost, then we should probably still call `embed`, | ||
| # or it is not, then we need M. | ||
| return function (::AbstractManifold, θ) | ||
|
Comment on lines
+247
to
+250
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should check what best to do, the current one works in some cases, but not all.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This I don't know. Do you need to know the manifold to know how to calculate the loss? I guess to know the mapping for some parameter values in some representations?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature of cost/grad/hess always has the first parameter as the manifold, since it allows to implement several costs for arbitrary manifolds, e..g. the Karcher mean to minimise the distances squared. My main problem is that I do not understand which cost that is
as long as embed is the identity, like for SPDs and the sphere the current code works. But for fixed rank it for example would not work. |
||
| x = f.f(θ, prob.p) | ||
| cb(x, θ) | ||
| __x = first(x) | ||
| return prob.sense === Optimization.MaxSense ? -__x : __x | ||
| end | ||
| end | ||
|
|
||
| #TODO: What does the “true” mean here? | ||
| function build_gradF(f::OptimizationFunction{true}) | ||
| function g(M::AbstractManifold, G, θ) | ||
| f.grad(G, θ) | ||
|
|
@@ -356,6 +262,7 @@ function build_gradF(f::OptimizationFunction{true}) | |
| f.grad(G, θ) | ||
| return riemannian_gradient(M, θ, G) | ||
| end | ||
| return g | ||
| end | ||
kellertuer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function build_hessF(f::OptimizationFunction{true}) | ||
kellertuer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -373,6 +280,7 @@ function build_hessF(f::OptimizationFunction{true}) | |
| f.grad(G, θ) | ||
| return riemannian_Hessian(M, θ, G, H, X) | ||
| end | ||
| return h | ||
| end | ||
|
|
||
| function SciMLBase.__solve(cache::OptimizationCache{ | ||
|
|
@@ -395,8 +303,7 @@ function SciMLBase.__solve(cache::OptimizationCache{ | |
| LC, | ||
| UC, | ||
| S, | ||
| O <: | ||
| AbstractManoptOptimizer, | ||
| O <: AbstractManoptOptimizer, | ||
| D, | ||
| P, | ||
| C | ||
|
|
@@ -418,6 +325,7 @@ function SciMLBase.__solve(cache::OptimizationCache{ | |
| u = θ, | ||
| p = cache.p, | ||
| objective = x[1]) | ||
| #TODO: What is this callback for? | ||
ChrisRackauckas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cb_call = cache.callback(opt_state, x...) | ||
kellertuer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if !(cb_call isa Bool) | ||
| error("The callback should return a boolean `halt` for whether to stop the optimization process.") | ||
|
|
@@ -452,7 +360,7 @@ function SciMLBase.__solve(cache::OptimizationCache{ | |
| solver_kwarg..., stopping_criterion = stopping_criterion, hessF) | ||
|
|
||
| asc = get_stopping_criterion(opt_res.options) | ||
| opt_ret = Manopt.indicates_convergence(asc) ? ReturnCode.Success : ReturnCode.Failure | ||
| opt_ret = Manopt.has_converged(asc) ? ReturnCode.Success : ReturnCode.Failure | ||
|
|
||
| return SciMLBase.build_solution(cache, | ||
| cache.opt, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.