/
stochastic_gradient_plan.jl
356 lines (328 loc) · 11.3 KB
/
stochastic_gradient_plan.jl
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
@doc raw"""
ManifoldStochasticGradientObjective{T<:AbstractEvaluationType} <: AbstractManifoldGradientObjective{T}
A stochastic gradient objective consists of
* a(n optional) cost function ``f(p) = \displaystyle\sum_{i=1}^n f_i(p)
* an array of gradients, ``\operatorname{grad}f_i(p), i=1,\ldots,n`` which can be given in two forms
* as one single function ``(\mathcal M, p) ↦ (X_1,…,X_n) ∈ (T_p\mathcal M)^n``
* as a vector of functions ``\bigl( (\mathcal M, p) ↦ X_1, …, (\mathcal M, p) ↦ X_n\bigr)``.
Where both variants can also be provided as [`InplaceEvaluation`](@ref) functions
`(M, X, p) -> X`, where `X` is the vector of `X1,...Xn` and `(M, X1, p) -> X1, ..., (M, Xn, p) -> Xn`,
respectively.
# Constructors
ManifoldStochasticGradientObjective(
grad_f::Function;
cost=Missing(),
evaluation=AllocatingEvaluation()
)
ManifoldStochasticGradientObjective(
grad_f::AbstractVector{<:Function};
cost=Missing(), evaluation=AllocatingEvaluation()
)
Create a Stochastic gradient problem with the gradient either as one
function (returning an array of tangent vectors) or a vector of functions (each returning one tangent vector).
The optional cost can also be given as either a single function (returning a number)
pr a vector of functions, each returning a value.
# Used with
[`stochastic_gradient_descent`](@ref)
Note that this can also be used with a [`gradient_descent`](@ref), since the (complete) gradient
is just the sums of the single gradients.
"""
struct ManifoldStochasticGradientObjective{T<:AbstractEvaluationType,TCost,TGradient} <:
AbstractManifoldGradientObjective{T,TCost,TGradient}
cost::TCost
gradient!!::TGradient
end
function ManifoldStochasticGradientObjective(
grad_f!!::G; cost::C=Missing(), evaluation::E=AllocatingEvaluation()
) where {
E<:AbstractEvaluationType,
G<:Union{Function,AbstractVector{<:Function}},
C<:Union{Function,AbstractVector{<:Function},Missing},
}
return ManifoldStochasticGradientObjective{E,C,G}(cost, grad_f!!)
end
function get_cost(
M::AbstractManifold, sgo::ManifoldStochasticGradientObjective{E,C}, p
) where {E<:AbstractEvaluationType,C<:AbstractVector{<:Function}}
return sum(f(M, p) for f in sgo.cost)
end
@doc raw"""
get_cost(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, p, i)
Evaluate the `i`th summand of the cost.
If you use a single function for the stochastic cost, then only the index `ì=1`` is available
to evaluate the whole cost.
"""
function get_cost(
M::AbstractManifold, sgo::ManifoldStochasticGradientObjective{E,C}, p, i
) where {E<:AbstractEvaluationType,C<:AbstractVector{<:Function}}
return sgo.cost[i](M, p)
end
function get_cost(
M::AbstractManifold, sgo::ManifoldStochasticGradientObjective{E,C}, p, i
) where {E<:AbstractEvaluationType,C<:Function}
(i == 1) && return sgo.cost(M, p)
return error(
"The cost is implemented as a single function and can not be accessed element wise at $i since the index is larger than 1.",
)
end
@doc raw"""
get_gradients(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, p)
get_gradients!(M::AbstractManifold, X, sgo::ManifoldStochasticGradientObjective, p)
Evaluate all summands gradients ``\{\operatorname{grad}f_i\}_{i=1}^n`` at `p` (in place of `X`).
If you use a single function for the stochastic gradient, that works in-place, then `get_gradient` is not available,
since the length (or number of elements of the gradient) can not be determined.
"""
function get_gradients(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:Function},
p,
) where {TC}
return sgo.gradient!!(M, p)
end
function get_gradients(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
) where {TC}
return [grad_i(M, p) for grad_i in sgo.gradient!!]
end
function get_gradients(M::AbstractManifold, admo::AbstractDecoratedManifoldObjective, p)
return get_gradients(M, get_objective(admo, false), p)
end
function get_gradients!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:Function},
p,
) where {TC}
copyto!(M, X, sgo.gradient!!(M, p))
return X
end
function get_gradients!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
) where {TC}
copyto!.(Ref(M), X, [grad_i(M, p) for grad_i in sgo.gradient!!])
return X
end
function get_gradients!(M::AbstractManifold, X, admo::AbstractDecoratedManifoldObjective, p)
return get_gradients!(M, X, get_objective(admo, false), p)
end
function get_gradients(
::AbstractManifold,
::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:Function},
::Any,
) where {TC}
return error(
"For a mutating function type stochastic gradient, the allocating variant is not possible.",
)
end
function get_gradients(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:AbstractVector},
p,
) where {TC}
X = [zero_vector(M, p) for _ in sgo.gradient!!]
get_gradients!(M, X, sgo, p)
return X
end
function get_gradients!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:Function},
p,
) where {TC}
sgo.gradient!!(M, X, p)
return X
end
function get_gradients!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:AbstractVector},
p,
) where {TC}
for (Xi, grad_i) in zip(X, sgo.gradient!!)
grad_i(M, Xi, p)
end
return X
end
# Pass down from problem
function get_gradients(mp::AbstractManoptProblem, p)
return get_gradients(get_manifold(mp), get_objective(mp), p)
end
function get_gradients!(mp::AbstractManoptProblem, X, p)
return get_gradients!(get_manifold(mp), X, get_objective(mp), p)
end
@doc raw"""
get_gradient(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, p, k)
get_gradient!(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, Y, p, k)
Evaluate one of the summands gradients ``\operatorname{grad}f_k``, ``k∈\{1,…,n\}``, at `x` (in place of `Y`).
If you use a single function for the stochastic gradient, that works in-place, then `get_gradient` is not available,
since the length (or number of elements of the gradient required for allocation) can not be determined.
"""
function get_gradient(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:Function},
p,
k,
) where {TC}
return sgo.gradient!!(M, p)[k]
end
function get_gradient(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
k,
) where {TC}
return sgo.gradient!![k](M, p)
end
function get_gradient(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC},
p,
k,
) where {TC}
X = zero_vector(M, p)
return get_gradient!(M, X, sgo, p, k)
end
function get_gradient(M::AbstractManifold, admo::AbstractDecoratedManifoldObjective, p, k)
return get_gradient(M, get_objective(admo, false), p, k)
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:Function},
p,
k,
) where {TC}
copyto!(M, X, sgo.gradient!!(M, p)[k])
return X
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
k,
) where {TC}
copyto!(M, X, sgo.gradient!![k](M, p))
return X
end
function get_gradient!(
::AbstractManifold,
::Any,
::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:Function},
::Any,
::Any,
) where {TC}
return error(
"An in-place variant for single entries of the stochastic gradient as a single function is not implemented, since the size can not be determined.",
)
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:AbstractVector},
p,
k,
) where {TC}
return sgo.gradient!![k](M, X, p)
end
function get_gradient!(
M::AbstractManifold, X, admo::AbstractDecoratedManifoldObjective, p, k
)
return get_gradient!(M, X, get_objective(admo, false), p, k)
end
# Pass down from problem
function get_gradient(mp::AbstractManoptProblem, p, k)
return get_gradient(get_manifold(mp), get_objective(mp), p, k)
end
function get_gradient!(mp::AbstractManoptProblem, X, p, k)
return get_gradient!(get_manifold(mp), X, get_objective(mp), p, k)
end
@doc raw"""
get_gradient(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, p)
get_gradient!(M::AbstractManifold, sgo::ManifoldStochasticGradientObjective, X, p)
Evaluate the complete gradient ``\operatorname{grad} f = \displaystyle\sum_{i=1}^n \operatorname{grad} f_i(p)`` at `p` (in place of `X`).
If you use a single function for the stochastic gradient, that works in-place, then `get_gradient` is not available,
since the length (or number of elements of the gradient required for allocation) can not be determined.
"""
function get_gradient(
M::AbstractManifold, sgo::ManifoldStochasticGradientObjective{T,TC,<:Function}, p
) where {T<:AbstractEvaluationType,TC}
# even if the function is in-place, allocation of the full vector of tangent vectors still required
return sum(get_gradients(M, sgo, p))
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:Function},
p,
) where {TC}
zero_vector!(M, X, p)
for Xi in sgo.gradient!!(M, p)
X += Xi
end
return X
end
function get_gradient!(
::AbstractManifold,
::Any,
::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:Function},
::Any,
) where {TC}
return error(
"An in-place variant for (sum of) the stochastic gradient as a single function is not implemented, since the size can not be determined.",
)
end
function get_gradient(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
) where {TC}
X = zero_vector(M, p)
get_gradient!(M, X, sgo, p)
return X
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{AllocatingEvaluation,TC,<:AbstractVector},
p,
) where {TC}
zero_vector!(M, X, p)
for k in 1:length(sgo.gradient!!)
X += get_gradient(M, sgo, p, k)
end
return X
end
function get_gradient(
M::AbstractManifold,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:AbstractVector},
p,
) where {TC}
X = zero_vector(M, p)
get_gradient!(M, X, sgo, p)
return X
end
function get_gradient!(
M::AbstractManifold,
X,
sgo::ManifoldStochasticGradientObjective{InplaceEvaluation,TC,<:AbstractVector},
p,
) where {TC}
zero_vector!(M, X, p)
Y = copy(M, p, X)
for grad_i in sgo.gradient!!
grad_i(M, Y, p)
X += Y
end
return X
end
"""
AbstractStochasticGradientDescentSolverState <: AbstractManoptSolverState
A generic type for all options related to stochastic gradient descent methods
"""
abstract type AbstractGradientGroupProcessor <: DirectionUpdateRule end