-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathgradient_plan.jl
More file actions
849 lines (698 loc) · 27.7 KB
/
gradient_plan.jl
File metadata and controls
849 lines (698 loc) · 27.7 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
@doc raw"""
AbstractManifoldGradientObjective{E<:AbstractEvaluationType, TC, TG} <: AbstractManifoldCostObjective{E, TC}
An abstract type for all objectives that provide a (full) gradient, where
`T` is a [`AbstractEvaluationType`](@ref) for the gradient function.
"""
abstract type AbstractManifoldGradientObjective{E<:AbstractEvaluationType,TC,TG} <:
AbstractManifoldCostObjective{E,TC} end
@doc raw"""
get_gradient_function(amgo::AbstractManifoldGradientObjective, recursive=false)
return the function to evaluate (just) the gradient ``\operatorname{grad} f(p)``,
where either the gradient function using the decorator or without the decorator is used.
By default `recursive` is set to `false`, since usually to just pass the gradient function
somewhere, one still wants for example the cached one or the one that still counts calls.
Depending on the [`AbstractEvaluationType`](@ref) `E` this is a function
* `(M, p) -> X` for the [`AllocatingEvaluation`](@ref) case
* `(M, X, p) -> X` for the [`InplaceEvaluation`](@ref) working in-place of `X`.
"""
function get_gradient_function(amgo::AbstractManifoldGradientObjective, recursive=false)
return amgo.gradient!!
end
function get_gradient_function(admo::AbstractDecoratedManifoldObjective, recursive=false)
return get_gradient_function(get_objective(admo, recursive))
end
@doc raw"""
ManifoldGradientObjective{T<:AbstractEvaluationType} <: AbstractManifoldGradientObjective{T}
specify an objective containing a cost and its gradient
# Fields
* `cost`: a function ``f: \mathcal M → ℝ``
* `gradient!!`: the gradient ``\operatorname{grad}f: \mathcal M → \mathcal T\mathcal M``
of the cost function ``f``.
Depending on the [`AbstractEvaluationType`](@ref) `T` the gradient can have to forms
* as a function `(M, p) -> X` that allocates memory for `X`, an [`AllocatingEvaluation`](@ref)
* as a function `(M, X, p) -> X` that work in place of `X`, an [`InplaceEvaluation`](@ref)
# Constructors
ManifoldGradientObjective(cost, gradient; evaluation=AllocatingEvaluation())
# Used with
[`gradient_descent`](@ref), [`conjugate_gradient_descent`](@ref), [`quasi_Newton`](@ref)
"""
struct ManifoldGradientObjective{T<:AbstractEvaluationType,C,G} <:
AbstractManifoldGradientObjective{T,C,G}
cost::C
gradient!!::G
end
function ManifoldGradientObjective(
cost::C, gradient::G; evaluation::AbstractEvaluationType=AllocatingEvaluation()
) where {C,G}
return ManifoldGradientObjective{typeof(evaluation),C,G}(cost, gradient)
end
@doc raw"""
ManifoldCostGradientObjective{T} <: AbstractManifoldObjective{T}
specify an objective containing one function to perform a combined computation of cost and its gradient
# Fields
* `costgrad!!`: a function that computes both the cost ``f: \mathcal M → ℝ``
and its gradient ``\operatorname{grad}f: \mathcal M → \mathcal T\mathcal M``
Depending on the [`AbstractEvaluationType`](@ref) `T` the gradient can have to forms
* as a function `(M, p) -> (c, X)` that allocates memory for the gradient `X`, an [`AllocatingEvaluation`](@ref)
* as a function `(M, X, p) -> (c, X)` that work in place of `X`, an [`InplaceEvaluation`](@ref)
# Constructors
ManifoldCostGradientObjective(costgrad; evaluation=AllocatingEvaluation())
# Used with
[`gradient_descent`](@ref), [`conjugate_gradient_descent`](@ref), [`quasi_Newton`](@ref)
"""
struct ManifoldCostGradientObjective{T,CG} <: AbstractManifoldGradientObjective{T,CG,CG}
costgrad!!::CG
end
function ManifoldCostGradientObjective(
costgrad::CG; evaluation::AbstractEvaluationType=AllocatingEvaluation()
) where {CG}
return ManifoldCostGradientObjective{typeof(evaluation),CG}(costgrad)
end
get_cost_function(cgo::ManifoldCostGradientObjective) = (M, p) -> get_cost(M, cgo, p)
function get_gradient_function(cgo::ManifoldCostGradientObjective)
return (M, p) -> get_gradient(M, cgo, p)
end
#
# and internal helper to make the dispatch nicer
#
function get_cost_and_gradient(
M::AbstractManifold, cgo::ManifoldCostGradientObjective{AllocatingEvaluation}, p
)
return cgo.costgrad!!(M, p)
end
function get_cost_and_gradient(
M::AbstractManifold, cgo::ManifoldCostGradientObjective{InplaceEvaluation}, p
)
X = zero_vector(M, p)
return cgo.costgrad!!(M, X, p)
end
function get_cost_and_gradient!(
M::AbstractManifold, X, cgo::ManifoldCostGradientObjective{AllocatingEvaluation}, p
)
(c, Y) = cgo.costgrad!!(M, p)
copyto!(M, X, p, Y)
return (c, X)
end
function get_cost_and_gradient!(
M::AbstractManifold, X, cgo::ManifoldCostGradientObjective{InplaceEvaluation}, p
)
return cgo.costgrad!!(M, X, p)
end
function get_cost(M::AbstractManifold, cgo::ManifoldCostGradientObjective, p)
v, _ = get_cost_and_gradient(M, cgo, p)
return v
end
@doc raw"""
get_gradient(amp::AbstractManoptProblem, p)
get_gradient!(amp::AbstractManoptProblem, X, p)
evaluate the gradient of an [`AbstractManoptProblem`](@ref) `amp` at the point `p`.
The evaluation is done in place of `X` for the `!`-variant.
"""
function get_gradient(mp::AbstractManoptProblem, p)
return get_gradient(get_manifold(mp), get_objective(mp), p)
end
function get_gradient!(mp::AbstractManoptProblem, X, p)
return get_gradient!(get_manifold(mp), X, get_objective(mp), p)
end
"""
get_gradient(M::AbstractManifold, mgo::AbstractManifoldGradientObjective{T}, p)
get_gradient!(M::AbstractManifold, X, mgo::AbstractManifoldGradientObjective{T}, p)
evaluate the gradient of a [`AbstractManifoldGradientObjective{T}`](@ref) `mgo` at `p`.
The evaluation is done in place of `X` for the `!`-variant.
The `T=`[`AllocatingEvaluation`](@ref) problem might still allocate memory within.
When the non-mutating variant is called with a `T=`[`InplaceEvaluation`](@ref)
memory for the result is allocated.
Note that the order of parameters follows the philosophy of `Manifolds.jl`, namely that
even for the mutating variant, the manifold is the first parameter and the (in-place) tangent
vector `X` comes second.
"""
get_gradient(M::AbstractManifold, mgo::AbstractManifoldGradientObjective, p)
function get_gradient(M::AbstractManifold, admo::AbstractDecoratedManifoldObjective, p)
return get_gradient(M, get_objective(admo, false), p)
end
function get_gradient(
M::AbstractManifold, mgo::AbstractManifoldGradientObjective{AllocatingEvaluation}, p
)
return mgo.gradient!!(M, p)
end
function get_gradient(
M::AbstractManifold, mgo::AbstractManifoldGradientObjective{InplaceEvaluation}, p
)
X = zero_vector(M, p)
mgo.gradient!!(M, X, p)
return X
end
function get_gradient(M::AbstractManifold, mcgo::ManifoldCostGradientObjective, p)
_, X = get_cost_and_gradient(M, mcgo, p)
return X
end
function get_gradient!(M::AbstractManifold, X, admo::AbstractDecoratedManifoldObjective, p)
return get_gradient!(M, X, get_objective(admo, false), p)
end
function get_gradient!(
M::AbstractManifold, X, mgo::AbstractManifoldGradientObjective{AllocatingEvaluation}, p
)
copyto!(M, X, p, mgo.gradient!!(M, p))
return X
end
function get_gradient!(
M::AbstractManifold, X, mgo::AbstractManifoldGradientObjective{InplaceEvaluation}, p
)
mgo.gradient!!(M, X, p)
return X
end
function get_gradient!(M::AbstractManifold, X, mcgo::ManifoldCostGradientObjective, p)
get_cost_and_gradient!(M, X, mcgo, p)
return X
end
"""
X = get_subgradient(M::AbstractManifold, sgo::AbstractManifoldGradientObjective, p)
get_subgradient!(M::AbstractManifold, X, sgo::AbstractManifoldGradientObjective, p)
Evaluate the subgradient, which for the case of a objective having a gradient, means evaluating the
gradient itself.
While in general, the result might not be deterministic, for this case it is.
"""
function get_subgradient(M::AbstractManifold, agmo::AbstractManifoldGradientObjective, p)
return get_gradient(M, agmo, p)
end
function get_subgradient!(
M::AbstractManifold, X, agmo::AbstractManifoldGradientObjective, p
)
return get_gradient!(M, X, agmo, p)
end
@doc raw"""
get_gradient(agst::AbstractGradientSolverState)
return the gradient stored within gradient options.
THe default returns `agst.X`.
"""
get_gradient(agst::AbstractGradientSolverState) = agst.X
@doc raw"""
set_gradient!(agst::AbstractGradientSolverState, M, p, X)
set the (current) gradient stored within an [`AbstractGradientSolverState`](@ref) to `X`.
The default function modifies `s.X`.
"""
function set_gradient!(agst::AbstractGradientSolverState, M, p, X)
copyto!(M, agst.X, p, X)
return agst
end
@doc raw"""
get_iterate(agst::AbstractGradientSolverState)
return the iterate stored within gradient options.
THe default returns `agst.p`.
"""
get_iterate(agst::AbstractGradientSolverState) = agst.p
@doc raw"""
set_iterate!(agst::AbstractGradientSolverState, M, p)
set the (current) iterate stored within an [`AbstractGradientSolverState`](@ref) to `p`.
The default function modifies `s.p`.
"""
function set_iterate!(agst::AbstractGradientSolverState, M, p)
copyto!(M, agst.p, p)
return agst
end
"""
DirectionUpdateRule
A general functor, that handles direction update rules. It's fields are usually
only a [`StoreStateAction`](@ref) by default initialized to the fields required
for the specific coefficient, but can also be replaced by a (common, global)
individual one that provides these values.
"""
abstract type DirectionUpdateRule end
"""
IdentityUpdateRule <: DirectionUpdateRule
The default gradient direction update is the identity, usually it just evaluates the gradient.
You can also use `Gradient()` to create the corresponding factory, though this only delays
this parameter-free instantiation to later.
"""
struct IdentityUpdateRule <: DirectionUpdateRule end
Gradient() = ManifoldDefaultsFactory(Manopt.IdentityUpdateRule; requires_manifold=false)
"""
MomentumGradientRule <: DirectionUpdateRule
Store the necessary information to compute the [`MomentumGradient`](@ref)
direction update.
# Fields
$(_var(:Field, :p, "p_old"))
* `momentum::Real`: factor for the momentum
* `direction`: internal [`DirectionUpdateRule`](@ref) to determine directions
to add the momentum to.
$(_var(:Field, :vector_transport_method))
$(_var(:Field, :X, "X_old"))
# Constructors
MomentumGradientRule(M::AbstractManifold; kwargs...)
Initialize a momentum gradient rule to `s`, where `p` and `X` are memory for interim values.
## Keyword arguments
$(_var(:Keyword, :p))
* `s=`[`IdentityUpdateRule`](@ref)`()`
* `momentum=0.2`
$(_var(:Keyword, :vector_transport_method))
$(_var(:Keyword, :X))
# See also
[`MomentumGradient`](@ref)
"""
mutable struct MomentumGradientRule{
P,T,D<:DirectionUpdateRule,R<:Real,VTM<:AbstractVectorTransportMethod
} <: DirectionUpdateRule
momentum::R
p_old::P
direction::D
vector_transport_method::VTM
X_old::T
end
function MomentumGradientRule(
M::AbstractManifold;
p::P=rand(M),
direction::Union{<:DirectionUpdateRule,ManifoldDefaultsFactory}=Gradient(),
vector_transport_method::VTM=default_vector_transport_method(M, typeof(p)),
X::Q=zero_vector(M, p),
momentum::F=0.2,
) where {P,Q,F<:Real,VTM<:AbstractVectorTransportMethod}
dir = _produce_type(direction, M)
return MomentumGradientRule{P,Q,typeof(dir),F,VTM}(
momentum, p, dir, vector_transport_method, X
)
end
function (mg::MomentumGradientRule)(
mp::AbstractManoptProblem, s::AbstractGradientSolverState, k
)
M = get_manifold(mp)
p = get_iterate(s)
step, dir = mg.direction(mp, s, k) #get inner direction and step size
mg.X_old =
mg.momentum *
vector_transport_to(M, mg.p_old, mg.X_old, p, mg.vector_transport_method) -
step .* dir
copyto!(M, mg.p_old, p)
return step, -mg.X_old
end
"""
MomentumGradient()
Append a momentum to a gradient processor, where the last direction and last iterate are
stored and the new is composed as ``η_i = m*η_{i-1}' - s d_i``,
where ``sd_i`` is the current (inner) direction and ``η_{i-1}'`` is the vector transported
last direction multiplied by momentum ``m``.
# Input
* `M` (optional)
# Keyword arguments
$(_var(:Keyword, :p))
* `direction=`[`IdentityUpdateRule`](@ref) preprocess the actual gradient before adding momentum
$(_var(:Keyword, :X))
* `momentum=0.2` amount of momentum to use
$(_var(:Keyword, :vector_transport_method))
$(_note(:ManifoldDefaultFactory, "MomentumGradientRule"))
"""
MomentumGradient(args...; kwargs...) =
ManifoldDefaultsFactory(Manopt.MomentumGradientRule, args...; kwargs...)
"""
AverageGradientRule <: DirectionUpdateRule
Add an average of gradients to a gradient processor. A set of previous directions (from the
inner processor) and the last iterate are stored. The average is taken after vector transporting
them to the current iterates tangent space.
# Fields
* `gradients`: the last `n` gradient/direction updates
* `last_iterate`: last iterate (needed to transport the gradients)
* `direction`: internal [`DirectionUpdateRule`](@ref) to determine directions to apply the averaging to
$(_var(:Keyword, :vector_transport_method))
# Constructors
AverageGradientRule(
M::AbstractManifold;
p::P=rand(M);
n::Int=10
direction::Union{<:DirectionUpdateRule,ManifoldDefaultsFactory}=IdentityUpdateRule(),
gradients = fill(zero_vector(p.M, o.x),n),
last_iterate = deepcopy(x0),
vector_transport_method = default_vector_transport_method(M, typeof(p))
)
Add average to a gradient problem, where
* `n`: determines the size of averaging
* `direction`: is the internal [`DirectionUpdateRule`](@ref) to determine the gradients to store
* `gradients`: can be pre-filled with some history
* `last_iterate`: stores the last iterate
$(_var(:Keyword, :vector_transport_method))
"""
mutable struct AverageGradientRule{
P,T,D<:DirectionUpdateRule,VTM<:AbstractVectorTransportMethod
} <: DirectionUpdateRule
gradients::AbstractVector{T}
last_iterate::P
direction::D
vector_transport_method::VTM
end
function AverageGradientRule(
M::AbstractManifold;
p::P=rand(M),
n::Int=10,
direction::Union{<:DirectionUpdateRule,ManifoldDefaultsFactory}=Gradient(),
gradients=[zero_vector(M, p) for _ in 1:n],
vector_transport_method::VTM=default_vector_transport_method(M, typeof(p)),
) where {P,VTM}
dir = _produce_type(direction, M)
return AverageGradientRule{P,eltype(gradients),typeof(dir),VTM}(
gradients, copy(M, p), dir, vector_transport_method
)
end
function (a::AverageGradientRule)(
mp::AbstractManoptProblem, s::AbstractGradientSolverState, k
)
# remove oldest/last
pop!(a.gradients)
M = get_manifold(mp)
p = get_iterate(s)
_, d = a.direction(mp, s, k) #get inner gradient and step
for g in a.gradients
vector_transport_to!(M, g, a.last_iterate, g, p, a.vector_transport_method)
end
pushfirst!(a.gradients, copy(M, p, d))
copyto!(M, a.last_iterate, p)
return 1.0, 1 / length(a.gradients) .* sum(a.gradients)
end
"""
AverageGradient(; kwargs...)
AverageGradient(M::AbstractManifold; kwargs...)
Add an average of gradients to a gradient processor. A set of previous directions (from the
inner processor) and the last iterate are stored, average is taken after vector transporting
them to the current iterates tangent space.
# Input
$(_var(:Argument, :M; type=true)) (optional)
# Keyword arguments
$(_var(:Keyword, :p; add=:as_Initial))
* `direction=`[`IdentityUpdateRule`](@ref) preprocess the actual gradient before adding momentum
* `gradients=[zero_vector(M, p) for _ in 1:n]` how to initialise the internal storage
* `n=10` number of gradient evaluations to take the mean over
$(_var(:Keyword, :X))
$(_var(:Keyword, :vector_transport_method))
$(_note(:ManifoldDefaultFactory, "AverageGradientRule"))
"""
AverageGradient(args...; kwargs...) =
ManifoldDefaultsFactory(Manopt.AverageGradientRule, args...; kwargs...)
@doc """
NesterovRule <: DirectionUpdateRule
Compute a Nesterov inspired direction update rule.
See [`Nesterov`](@ref) for details
# Fields
* `γ::Real`, `μ::Real`: coefficients from the last iterate
* `v::P`: an interim point to compute the next gradient evaluation point `y_k`
* `shrinkage`: a function `k -> ...` to compute the shrinkage ``β_k`` per iterate `k``.
$(_var(:Keyword, :inverse_retraction_method))
# Constructor
NesterovRule(M::AbstractManifold; kwargs...)
## Keyword arguments
$(_var(:Keyword, :p; add=:as_Initial))
* `γ=0.001``
* `μ=0.9``
* `shrinkage = k -> 0.8`
$(_var(:Keyword, :inverse_retraction_method))
# See also
[`Nesterov`](@ref)
"""
mutable struct NesterovRule{P,R<:Real} <: DirectionUpdateRule
γ::R
μ::R
v::P
shrinkage::Function
inverse_retraction_method::AbstractInverseRetractionMethod
end
function NesterovRule(
M::AbstractManifold;
p::P=rand(M),
γ::T=0.001,
μ::T=0.9,
shrinkage::Function=i -> 0.8,
inverse_retraction_method::AbstractInverseRetractionMethod=default_inverse_retraction_method(
M, typeof(p)
),
) where {P,T}
p_ = _ensure_mutating_variable(p)
return NesterovRule{typeof(p_),T}(
γ, μ, copy(M, p_), shrinkage, inverse_retraction_method
)
end
function (n::NesterovRule)(mp::AbstractManoptProblem, s::AbstractGradientSolverState, k)
M = get_manifold(mp)
h = get_stepsize(mp, s, k)
p = get_iterate(s)
α = (h * (n.γ - n.μ) + sqrt(h^2 * (n.γ - n.μ)^2 + 4 * h * n.γ)) / 2
γbar = (1 - α) * n.γ + α * n.μ
y = retract(
M,
p,
((α * n.γ) / (n.γ + α * n.μ)) *
inverse_retract(M, p, n.v, n.inverse_retraction_method),
)
gradf_yk = get_gradient(mp, y)
xn = retract(M, y, -h * gradf_yk)
d =
(((1 - α) * n.γ) / γbar) * inverse_retract(M, y, n.v, n.inverse_retraction_method) -
(α / γbar) * gradf_yk
n.v = retract(M, y, d, s.retraction_method)
n.γ = 1 / (1 + n.shrinkage(k)) * γbar
return h, (-1 / h) * inverse_retract(M, p, xn, n.inverse_retraction_method) # outer update
end
@doc """
Nesterov(; kwargs...)
Nesterov(M::AbstractManifold; kwargs...)
Assume ``f`` is ``L``-Lipschitz and ``μ``-strongly convex. Given
* a step size ``h_k<$(_tex(:frac, "1", "L"))`` (from the [`GradientDescentState`](@ref)
* a `shrinkage` parameter ``β_k``
* and a current iterate ``p_k``
* as well as the interim values ``γ_k`` and ``v_k`` from the previous iterate.
This compute a Nesterov type update using the following steps, see [ZhangSra:2018](@cite)
1. Compute the positive root ``α_k∈(0,1)`` of ``α^2 = h_k$(_tex(:bigl))((1-α_k)γ_k+α_k μ$(_tex(:bigr)))``.
2. Set ``$(_tex(:bar, "γ"))_k+1 = (1-α_k)γ_k + α_kμ``
3. ``y_k = $(_tex(:retr))_{p_k}\\Bigl(\\frac{α_kγ_k}{γ_k + α_kμ}$(_tex(:retr))^{-1}_{p_k}v_k \\Bigr)``
4. ``x_{k+1} = $(_tex(:retr))_{y_k}(-h_k $(_tex(:grad))f(y_k))``
5. ``v_{k+1} = $(_tex(:retr))_{y_k}\\Bigl(\\frac{(1-α_k)γ_k}{$(_tex(:bar, "γ"))_k}$(_tex(:retr))_{y_k}^{-1}(v_k) - \\frac{α_k}{$(_tex(:bar, "γ"))_{k+1}}$(_tex(:grad))f(y_k) \\Bigr)``
6. ``γ_{k+1} = \\frac{1}{1+β_k}$(_tex(:bar, "γ"))_{k+1}``
Then the direction from ``p_k`` to ``p_k+1`` by ``d = $(_tex(:invretr))_{p_k}p_{k+1}`` is returned.
# Input
$(_var(:Argument, :M; type=true)) (optional)
# Keyword arguments
$(_var(:Keyword, :p; add=:as_Initial))
* `γ=0.001`
* `μ=0.9`
* `shrinkage = k -> 0.8`
$(_var(:Keyword, :inverse_retraction_method))
$(_note(:ManifoldDefaultFactory, "NesterovRule"))
"""
function Nesterov(args...; kwargs...)
return ManifoldDefaultsFactory(Manopt.NesterovRule, args...; kwargs...)
end
"""
PreconditionedDirectionRule{E<:AbstractEvaluationType} <: DirectionUpdateRule
Add a preconditioning as gradient processor, see [`PreconditionedDirection`](@ref)
for more mathematical background.
# Fields
* `direction`: internal [`DirectionUpdateRule`](@ref) to determine directions to apply the preconditioning to
* `preconditioner`: the preconditioner function
# Constructors
PreconditionedDirectionRule(
M::AbstractManifold,
preconditioner;
direction::Union{<:DirectionUpdateRule,ManifoldDefaultsFactory}=IdentityUpdateRule(),
evaluation::AbstractEvaluationType=AllocatingEvaluation() n::Int=10
)
Add preconditioning to a gradient problem.
# Input
$(_var(:Argument, :M; type=true))
* `preconditioner`: preconditioner function, either as a `(M, p, X)` -> Y` allocating or `(M, Y, p, X) -> Y` mutating function
# Keyword arguments
$(_var(:Keyword, :evaluation))
* `direction=`[`IdentityUpdateRule`](@ref) internal [`DirectionUpdateRule`](@ref) to determine the gradients to store or a [`ManifoldDefaultsFactory`](@ref) generating one
"""
mutable struct PreconditionedDirectionRule{
E<:AbstractEvaluationType,D<:DirectionUpdateRule,F
} <: DirectionUpdateRule
preconditioner::F
direction::D
end
function PreconditionedDirectionRule(
M::AbstractManifold,
preconditioner::F;
direction::Union{<:DirectionUpdateRule,ManifoldDefaultsFactory}=Gradient(),
evaluation::E=AllocatingEvaluation(),
) where {E<:AbstractEvaluationType,F}
dir = _produce_type(direction, M)
return PreconditionedDirectionRule{E,typeof(dir),F}(preconditioner, dir)
end
function (pg::PreconditionedDirectionRule{AllocatingEvaluation})(
mp::AbstractManoptProblem, s::AbstractGradientSolverState, k
)
M = get_manifold(mp)
p = get_iterate(s)
# get inner direction and step size
step, dir = pg.direction(mp, s, k)
# precondition and set as gradient
set_gradient!(s, M, p, pg.preconditioner(M, p, dir))
return step, get_gradient(s)
end
function (pg::PreconditionedDirectionRule{InplaceEvaluation})(
mp::AbstractManoptProblem, s::AbstractGradientSolverState, k
)
M = get_manifold(mp)
p = get_iterate(s)
step, dir = pg.direction(mp, s, k) # get inner direction and step size
pg.preconditioner(M, dir, p, dir)
return step, dir
end
"""
PreconditionedDirection(preconditioner; kwargs...)
PreconditionedDirection(M::AbstractManifold, preconditioner; kwargs...)
Add a preconditioner to a gradient processor following the [motivation for optimization](https://en.wikipedia.org/wiki/Preconditioner#Preconditioning_in_optimization),
as a linear invertible map ``P: $(_math(:TpM)) → $(_math(:TpM))`` that usually should be
* symmetric: ``⟨X, P(Y)⟩ = ⟨P(X), Y⟩``
* positive definite ``⟨X, P(X)⟩ > 0`` for ``X`` not the zero-vector
The gradient is then preconditioned as ``P(X)``, where ``X`` is either the
gradient of the objective or the result of a previous (internally stored) gradient processor.
For example if you provide as the preconditioner the inverse of the Hessian ``$(_tex(:Hess))^{-1} f``,
you turn a gradient descent into a Newton method.
# Arguments
$(_var(:Argument, :M; type=true)) (optional)
* `preconditioner`: preconditioner function, either as a `(M, p, X) -> Y` allocating or `(M, Y, p, X) -> Y` mutating function
# Keyword arguments
* `direction=`[`IdentityUpdateRule`](@ref) internal [`DirectionUpdateRule`](@ref) to determine the gradients to store or a [`ManifoldDefaultsFactory`](@ref) generating one
$(_var(:Keyword, :evaluation))
$(_note(:ManifoldDefaultFactory, "PreconditionedDirectionRule"))
"""
PreconditionedDirection(args...; kwargs...) =
ManifoldDefaultsFactory(Manopt.PreconditionedDirectionRule, args...; kwargs...)
@doc raw"""
DebugGradient <: DebugAction
debug for the gradient evaluated at the current iterate
# Constructors
DebugGradient(; long=false, prefix= , format= "$prefix%s", io=stdout)
display the short (`false`) or long (`true`) default text for the gradient,
or set the `prefix` manually. Alternatively the complete format can be set.
"""
mutable struct DebugGradient <: DebugAction
io::IO
format::String
function DebugGradient(;
long::Bool=false,
prefix=long ? "Gradient: " : "grad f(p):",
format="$prefix%s",
io::IO=stdout,
)
return new(io, format)
end
end
function (d::DebugGradient)(::AbstractManoptProblem, s::AbstractManoptSolverState, k::Int)
(k < 1) && return nothing
Printf.format(d.io, Printf.Format(d.format), get_gradient(s))
return nothing
end
function show(io::IO, dg::DebugGradient)
return print(io, "DebugGradient(; format=\"$(dg.format)\")")
end
status_summary(dg::DebugGradient) = "(:Gradient, \"$(dg.format)\")"
@doc raw"""
DebugGradientNorm <: DebugAction
debug for gradient evaluated at the current iterate.
# Constructors
DebugGradientNorm([long=false,p=print])
display the short (`false`) or long (`true`) default text for the gradient norm.
DebugGradientNorm(prefix[, p=print])
display the a `prefix` in front of the gradient norm.
"""
mutable struct DebugGradientNorm <: DebugAction
io::IO
format::String
function DebugGradientNorm(;
long::Bool=false,
prefix=long ? "Norm of the Gradient: " : "|grad f(p)|:",
format="$prefix%s",
io::IO=stdout,
)
return new(io, format)
end
end
function (d::DebugGradientNorm)(
mp::AbstractManoptProblem, s::AbstractManoptSolverState, k::Int
)
(k < 1) && return nothing
Printf.format(
d.io,
Printf.Format(d.format),
norm(get_manifold(mp), get_iterate(s), get_gradient(s)),
)
return nothing
end
function show(io::IO, dgn::DebugGradientNorm)
return print(io, "DebugGradientNorm(; format=\"$(dgn.format)\")")
end
status_summary(dgn::DebugGradientNorm) = "(:GradientNorm, \"$(dgn.format)\")"
@doc raw"""
DebugStepsize <: DebugAction
debug for the current step size.
# Constructors
DebugStepsize(;long=false,prefix="step size:", format="$prefix%s", io=stdout)
display the a `prefix` in front of the step size.
"""
mutable struct DebugStepsize <: DebugAction
io::IO
format::String
function DebugStepsize(;
long::Bool=false,
io::IO=stdout,
prefix=long ? "step size:" : "s:",
format="$prefix%s",
)
return new(io, format)
end
end
function (d::DebugStepsize)(
p::P, s::O, k::Int
) where {P<:AbstractManoptProblem,O<:AbstractGradientSolverState}
(k < 1) && return nothing
Printf.format(d.io, Printf.Format(d.format), get_last_stepsize(p, s, k))
return nothing
end
function show(io::IO, ds::DebugStepsize)
return print(io, "DebugStepsize(; format=\"$(ds.format)\")")
end
status_summary(ds::DebugStepsize) = "(:Stepsize, \"$(ds.format)\")"
#
# Records
#
@doc raw"""
RecordGradient <: RecordAction
record the gradient evaluated at the current iterate
# Constructors
RecordGradient(ξ)
initialize the [`RecordAction`](@ref) to the corresponding type of the tangent vector.
"""
mutable struct RecordGradient{T} <: RecordAction
recorded_values::Array{T,1}
RecordGradient{T}() where {T} = new(Array{T,1}())
end
RecordGradient(::T) where {T} = RecordGradient{T}()
function (r::RecordGradient{T})(
::AbstractManoptProblem, s::AbstractManoptSolverState, k::Int
) where {T}
return record_or_reset!(r, get_gradient(s), k)
end
show(io::IO, ::RecordGradient{T}) where {T} = print(io, "RecordGradient{$T}()")
@doc raw"""
RecordGradientNorm <: RecordAction
record the norm of the current gradient
"""
mutable struct RecordGradientNorm <: RecordAction
recorded_values::Array{Float64,1}
RecordGradientNorm() = new(Array{Float64,1}())
end
function (r::RecordGradientNorm)(
mp::AbstractManoptProblem, ast::AbstractManoptSolverState, k::Int
)
M = get_manifold(mp)
return record_or_reset!(r, norm(M, get_iterate(ast), get_gradient(ast)), k)
end
show(io::IO, ::RecordGradientNorm) = print(io, "RecordGradientNorm()")
@doc raw"""
RecordStepsize <: RecordAction
record the step size
"""
mutable struct RecordStepsize <: RecordAction
recorded_values::Array{Float64,1}
RecordStepsize() = new(Array{Float64,1}())
end
function (r::RecordStepsize)(p::AbstractManoptProblem, s::AbstractGradientSolverState, k)
return record_or_reset!(r, get_last_stepsize(p, s, k), k)
end