-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathManoptRecursiveArrayToolsExt.jl
More file actions
131 lines (122 loc) · 4.38 KB
/
Copy pathManoptRecursiveArrayToolsExt.jl
File metadata and controls
131 lines (122 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module ManoptRecursiveArrayToolsExt
using Manopt
using ManifoldsBase
using ManifoldsBase: submanifold_components
import Manopt:
max_stepsize,
alternating_gradient_descent,
alternating_gradient_descent!,
get_gradient,
get_gradient!,
set_parameter!
using Manopt: _tex, _var, ManifoldDefaultsFactory, _produce_type
using RecursiveArrayTools
@doc """
X = get_gradient(M::ProductManifold, ago::ManifoldAlternatingGradientObjective, p)
get_gradient!(M::ProductManifold, P::ManifoldAlternatingGradientObjective, X, p)
Evaluate all summands gradients at a point `p` on the `ProductManifold M` (in place of `X`)
"""
get_gradient(M::ProductManifold, ::ManifoldAlternatingGradientObjective, ::Any...)
function get_gradient(
M::AbstractManifold,
mago::ManifoldAlternatingGradientObjective{AllocatingEvaluation, TC, <:AbstractVector},
p,
) where {TC}
return ArrayPartition([gi(M, p) for gi in mago.gradient!!]...)
end
@doc """
X = get_gradient(M::AbstractManifold, p::ManifoldAlternatingGradientObjective, p, i)
get_gradient!(M::AbstractManifold, p::ManifoldAlternatingGradientObjective, X, p, i)
Evaluate one of the component gradients ``$(_tex(:grad)) f_i``, ``i∈ $(_tex(:set, "1,…,n"))``, at `x` (in place of `Y`).
"""
function get_gradient(
M::ProductManifold,
mago::ManifoldAlternatingGradientObjective{AllocatingEvaluation, TC, <:Function},
p,
i,
) where {TC}
return get_gradient(M, mago, p)[M, i]
end
function get_gradient!(
M::AbstractManifold,
X,
mago::ManifoldAlternatingGradientObjective{InplaceEvaluation, TC, <:AbstractVector},
p,
) where {TC}
for (gi, Xi) in zip(mago.gradient!!, submanifold_components(M, X))
gi(M, Xi, p)
end
return X
end
function get_gradient!(
M::ProductManifold,
X,
mago::ManifoldAlternatingGradientObjective{AllocatingEvaluation, TC, <:Function},
p,
k,
) where {TC}
copyto!(M[k], X, mago.gradient!!(M, p)[M, k])
return X
end
function alternating_gradient_descent(
M::ProductManifold,
f,
grad_f::Union{TgF, AbstractVector{<:TgF}},
p = rand(M);
evaluation::AbstractEvaluationType = AllocatingEvaluation(),
kwargs...,
) where {TgF}
ago = ManifoldAlternatingGradientObjective(f, grad_f; evaluation = evaluation)
return alternating_gradient_descent(M, ago, p; evaluation = evaluation, kwargs...)
end
function alternating_gradient_descent(
M::ProductManifold, ago::ManifoldAlternatingGradientObjective, p; kwargs...
)
Manopt.keywords_accepted(alternating_gradient_descent; kwargs...)
q = copy(M, p)
return alternating_gradient_descent!(M, ago, q; kwargs...)
end
function alternating_gradient_descent!(
M::ProductManifold,
f,
grad_f::Union{TgF, AbstractVector{<:TgF}},
p;
evaluation::AbstractEvaluationType = AllocatingEvaluation(),
kwargs...,
) where {TgF}
agmo = ManifoldAlternatingGradientObjective(f, grad_f; evaluation = evaluation)
return alternating_gradient_descent!(M, agmo, p; evaluation = evaluation, kwargs...)
end
function alternating_gradient_descent!(
M::ProductManifold,
agmo::ManifoldAlternatingGradientObjective,
p;
inner_iterations::Int = 5,
stopping_criterion::StoppingCriterion = StopAfterIteration(100) |
StopWhenGradientNormLess(1.0e-9),
stepsize::Union{Stepsize, ManifoldDefaultsFactory} = default_stepsize(
M, AlternatingGradientDescentState
),
order_type::Symbol = :Linear,
order = collect(1:length(M.manifolds)),
retraction_method::AbstractRetractionMethod = default_retraction_method(M, typeof(p)),
kwargs...,
)
Manopt.keywords_accepted(alternating_gradient_descent!; kwargs...)
dagmo = Manopt.decorate_objective!(M, agmo; kwargs...)
dmp = DefaultManoptProblem(M, dagmo)
agds = AlternatingGradientDescentState(
M;
p = p,
inner_iterations = inner_iterations,
stopping_criterion = stopping_criterion,
stepsize = _produce_type(stepsize, M),
order_type = order_type,
order = order,
retraction_method = retraction_method,
)
agds = Manopt.decorate_state!(agds; kwargs...)
Manopt.solve!(dmp, agds)
return Manopt.get_solver_return(get_objective(dmp), agds)
end
end