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

Alternative fix to extra allocations in get_gradient! from Jacobian #301

Merged
merged 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Manopt"
uuid = "0fc0a36d-df90-57f3-8f93-d78a9fc72bb5"
authors = ["Ronny Bergmann <[email protected]>"]
version = "0.4.37"
version = "0.4.38"

[deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/plans/objective.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ and internally
get_gradient_function
```

#### Internal Helpers

```@docs
get_gradient_from_Jacobian!
```

### Subgradient Objective

```@docs
Expand Down
34 changes: 28 additions & 6 deletions src/plans/nonlinear_least_squares_plan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ function get_cost(
return 1//2 * norm(residual_values)^2
end

"""
get_gradient_from_Jacobian!(
M::AbstractManifold,
X,
nlso::NonlinearLeastSquaresObjective{InplaceEvaluation},
p,
Jval=zeros(nlso.num_components, manifold_dimension(M)),
)

Compute gradient of [`NonlinearLeastSquaresObjective`](@ref) `nlso` at point `p` in place of
`X`, with temporary Jacobian stored in the optional argument `Jval`.
"""
function get_gradient_from_Jacobian!(
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
M::AbstractManifold,
X,
nlso::NonlinearLeastSquaresObjective{InplaceEvaluation},
p,
Jval=zeros(nlso.num_components, manifold_dimension(M)),
)
basis_p = _maybe_get_basis(M, p, nlso.jacobian_tangent_basis)
nlso.jacobian!!(M, Jval, p; basis_domain=basis_p)
residual_values = zeros(nlso.num_components)
nlso.f(M, residual_values, p)
get_vector!(M, X, p, transpose(Jval) * residual_values, basis_p)
return X
end

function get_gradient(
M::AbstractManifold, nlso::NonlinearLeastSquaresObjective{AllocatingEvaluation}, p
)
Expand Down Expand Up @@ -101,12 +128,7 @@ end
function get_gradient!(
M::AbstractManifold, X, nlso::NonlinearLeastSquaresObjective{InplaceEvaluation}, p
)
basis_p = _maybe_get_basis(M, p, nlso.jacobian_tangent_basis)
Jval = zeros(nlso.num_components, manifold_dimension(M))
nlso.jacobian!!(M, Jval, p; basis_domain=basis_p)
residual_values = zeros(nlso.num_components)
nlso.f(M, residual_values, p)
get_vector!(M, X, p, transpose(Jval) * residual_values, basis_p)
get_gradient_from_Jacobian!(M, X, nlso, p)
return X
end

Expand Down
5 changes: 3 additions & 2 deletions src/solvers/LevenbergMarquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ function initialize_solver!(
dmp::DefaultManoptProblem{mT,<:NonlinearLeastSquaresObjective{InplaceEvaluation}},
lms::LevenbergMarquardtState,
) where {mT<:AbstractManifold}
get_objective(dmp).f(get_manifold(dmp), lms.residual_values, lms.p)
lms.X = get_gradient(dmp, lms.p)
M = get_manifold(dmp)
get_objective(dmp).f(M, lms.residual_values, lms.p)
get_gradient_from_Jacobian!(M, lms.X, get_objective(dmp), lms.p, lms.jacF)
return lms
end

Expand Down
2 changes: 2 additions & 0 deletions test/solvers/test_Levenberg_Marquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ end
X_r2 = similar(x0)
get_gradient!(p_r2, X_r2, x0)
@test isapprox(X_r2, [270.3617451389837, 677.6730784956912])
@test isapprox(get_gradient(p_r2, x0), [270.3617451389837, 677.6730784956912])

p_r2_mut = DefaultManoptProblem(
M,
Expand All @@ -228,6 +229,7 @@ end
X_r2 = similar(x0)
get_gradient!(p_r2_mut, X_r2, x0)
@test isapprox(X_r2, [270.3617451389837, 677.6730784956912])
@test isapprox(get_gradient(p_r2_mut, x0), [270.3617451389837, 677.6730784956912])

@testset "errors" begin
@test_throws ArgumentError LevenbergMarquardtState(
Expand Down