Linesearch API
The line search problem an optimizer hands to SimpleSolvers, the decaying fixed step, and AdamOptimizerWithDecay, which pairs that step with Adam.
GeometricOptimizers._manifold_αmax — Method
_manifold_αmax(solution, direction, c)The ceiling a step ceiling of c imposes on a solution made of several blocks: the smallest of the per-block step_αmax over the blocks that live on a Manifold, and Inf where none does.
One \alpha is applied to every block, so each manifold block imposes its own step_αmax(c, δᵢ) (2πc / ‖δᵢ‖ on compact orthogonal lifts, c / ρ(δᵢ) on symplectic lifts) and the binding one is the smallest of them. A block that is an ordinary array contributes nothing: a vector space has no geometric step scale of its own, so it must neither impose a ceiling nor inflate the norm that sets one for its neighbours. A ceiling of step_αmax(c, direction(cache)) over the whole direction would do both: its l2norm combines the blocks in quadrature.
Implementation
NeuralNetworkParameters.foldparameters over the two trees in lockstep keeps a heterogeneous NamedTuple — a StiefelManifold beside a Matrix beside a Vector — inferable, and its compile time linear in the number of blocks. This function is on the per-iteration path, through linesearch_parameters.
foldparameters and not foldstorage: _block_αmax dispatches on the whole leaf, because a Manifold is the only kind that imposes a ceiling. A descent into the numbers that a leaf stores would make every manifold block an ordinary array and give back Inf.
min is associative and commutative, so the direction of the fold does not matter. A set with no manifold in it gives Inf through the _block_αmax(::Any, …) method, and a set with no blocks through the initial value T(Inf).
The fold pairs the blocks by key and checks the widths: δ follows the solution block by block, but not always as the same kind of branch, because a container solution can be paired with the plain NamedTuple of its GlobalSection tree.
The signature takes y::NetworkParameters and not an unconstrained y. Upstream pairs the children of a keyed branch by key, and the children of a Tuple branch positionally, so _manifold_αmax(values(sol), values(δ), c) would fold crossed keys without an error. Every caller passes a whole set of parameters.
GeometricOptimizers.linesearch_parameters — Method
linesearch_parameters(cache, x, state, c)The parameters solver_step! hands the line search: the iterate x, the state whose section the trial steps retract from, and — where a manifold supplies a scale — the step ceiling αmax.
Implementation
Three methods, chosen on the type of the solution, as trial_iterate!'s two are.
For a Manifold, αmax is step_αmax(c, direction(cache)). It is built again at every solver step, because $\|\delta\|$ changes at every solver step — also between the two searches of one solver_step!, where the second runs on the direction that steepest_descent! has replaced.
For a NamedTuple it is _manifold_αmax over the blocks, i.e. the same quantity derived per block and minimised over the manifold ones. A NamedTuple carrying no manifold block therefore gets Inf, which is what upstream reads as "the caller has no scale of its own" (SimpleSolvers.linesearch_αmax) and is equivalent to the omission below.
For an AbstractVector the field is omitted. Upstream reads it through a hasproperty guard that constant-folds, so a caller that supplies nothing pays nothing. A Euclidean $f(x + \alpha{}p)$ grows with $\alpha$, so the sufficient-decrease test of the search rejects an over-long step. There is no geometric scale to supply, and the method's own SimpleSolvers.DEFAULT_LINESEARCH_αmax is the whole of the bound.
Every branch returns a concrete NamedTuple, so the merit closures stay type-stable. That is why the manifold-free NamedTuple passes Inf rather than joining the AbstractVector branch: deciding the shape of the parameters on the block types makes the return type a Union of two NamedTuples, which the merit closures then pay for on every evaluation.
GeometricOptimizers.step_αmax — Method
step_αmax(c, δ)The ceiling on the line search's $\alpha$ that a step ceiling of c imposes on the direction δ: $c\,2\pi/\|\delta\|$ for an orthogonal lift (StiefelLieAlgHorMatrix and GrassmannLieAlgHorMatrix) and $c/\rho(\delta)$ for a SymplecticLieAlgHorMatrix and a BiorthogonalLieAlgHorMatrix, with $\rho$ the spectral radius.
c is in multiples of the scale the geometry of the lift supplies:
- Retracting an orthogonal horizontal lift is a rotation, so $\alpha\mapsto\mathrm{retract}(\alpha\bar{B})$ has nothing left to reach once $\|\alpha\bar{B}\|$ passes $2\pi$ —
Cayleyhas converged to a fixed rotation by then andGeodesicis periodic. Everything past it is round-off, and on the SVD problem enough of it to leave the manifold; seeDEFAULT_STEP_CEILING. - Retracting a
SymplecticLieAlgHorMatrixacts in the non-compact group $Sp(2N)$, where a Hamiltonian lift $B$ can have real eigenvalues.Cayleyhas a real pole $\det(\mathbb{I} - \frac{\alpha}{2}B) = 0$ at $\alpha = 2/\mu$ for every real eigenvalue $\mu > 0$ of $B$, so at no $\alpha < 2/\rho(B)$, andGeodesicstretches a mode of $B$ by at most $\exp(\alpha\rho(B))$. Any $c < 2$ keeps everyCayleytrial step short of the pole, and $c = 1$ stops at half the distance to the nearest pole that $\rho(B)$ allows, with a stretch of a mode by at most $e$. The scale is $\rho(B)$ and not $\|\delta\|$: a change of the section $\lambda\mapsto\lambda h$ takes the lift to $h^{-1}Bh$, which has the eigenvalues of $B$ but another norm, so a ceiling in $\|\delta\|$ would make the path of the optimizer depend on the section. $\rho(B) \leq \|B\|_F/\sqrt{2} \leq \|\delta\|$, so this ceiling is never lower than $c/\|\delta\|$. A nonzero nilpotent lift has $\rho(B) = 0$ and no ceiling: its retraction is polynomial in $\alpha$ and has no pole. - Retracting a
BiorthogonalLieAlgHorMatrixacts in $GL(N)$, which is not compact either, and the reasons above carry over: a real eigenvalue $\mu$ of the lift puts a pole of $\mathrm{Cayley}(\alpha{}B)$ at $\alpha = 2/\mu$ for $\mu > 0$ and of its inverse $\mathrm{Cayley}(-\alpha{}B)$ at $\alpha = 2/|\mu|$ for $\mu < 0$, and a change of the section $\lambda\mapsto\lambda\,\mathrm{diag}(\mathbb{I}, h)$, $h\in{}GL(N - n)$, takes the lift to $\mathrm{diag}(\mathbb{I}, h)^{-1}B\,\mathrm{diag}(\mathbb{I}, h)$, which keeps its eigenvalues. Here $\rho(B)$ is bounded by the Frobenius norm of the dense lift, which is $\|\delta\|$.
δ is one direction and not a NamedTuple of them: a solution built of several blocks needs one ceiling per block and the smallest of them, which is _manifold_αmax's job. See linesearch_parameters.
Implementation
A direction whose norm is zero or not finite yields Inf, which SimpleSolvers.linesearch_αmax reads as "the caller has no scale of its own" and which leaves the method's own ceiling standing. That is a guard and not a formality: upstream raises an ArgumentError on a NaN or non-positive params.αmax before it evaluates the merit — correctly, since silently ignoring one would hand back exactly the unbounded step the caller was ruling out — so a vanishing direction has to be special-cased here rather than there.
GeometricOptimizers.trial_iterate! — Function
trial_iterate!(cache, params, α, retraction, workspace = nothing, scaled = <a new buffer>)Write the iterate a step of length α along the current direction would produce into solution(cache), and return it. This is what the merit function of the line search evaluates. On a manifold the scaled direction is written into scaled, which linesearch_problem builds once per search; a call without it allocates one.
Implementation
There are two methods, chosen on the type of the solution.
For an AbstractVector the iterate is $x_k + \alpha{}p_k$, i.e. SimpleSolvers.compute_new_iterate!.
For a Manifold — or a NamedTuple that contains one — that is both undefined and wrong: adding $\alpha{}p_k$ to a point of $St(N, n)$ leaves the manifold, and the direction is an AbstractLieAlgHorMatrix of a different shape than the point to begin with. The step has to go through the retraction, exactly as solver_step! does once the line search has picked its α, so this reproduces that: scale the direction, push the state's section through the retraction into the cache's section, and read the point back out.
section(params.state) is the base and has to survive unchanged across the trial steps of one line search; section(cache) is scratch. solver_step! uses the same pair the same way, both in its NaN loop and for the accepted step, so the line search leaves nothing behind that it does not overwrite itself.
GeometricOptimizers.trial_slope — Function
trial_slope(gradient_instance, cache, retraction, α, differential, workspace,
lift = <a new buffer>)Return $\varphi'(\alpha) = \langle\nabla{}f(x(\alpha)), D(\alpha)\rangle$ at the iterate currently held in solution(cache), for the derivative of the line search's merit. $D(\alpha)$ is retraction_differential(retraction, direction(cache), α), written into differential by retraction_differential! in workspace, and the gradient is lifted into lift; linesearch_problem builds both once, in the shape of the direction, and a call without lift allocates one. On a vector the pairing is with the direction itself, and neither buffer is read.
Implementation
On a Manifold the gradient that gradient_instance produces lives in the tangent space of the embedding while direction(cache) is a horizontal lift, so the two are not even the same shape and have to be brought together by GeometricOptimizers.gradient_rep! first. That is the map update!(::GradientCache, …) applies to the gradient before storing it, and it is global_rep except on a SymplecticStiefelManifold and a BiorthogonalManifold.
The pairing is then _dot and not dot: the α of the line search parameterizes a curve in the intrinsic coordinates of the lift, and dot on a lift is the ambient Frobenius product, which is twice that. With dot the slope is 2\varphi'(\alpha), which a curvature condition or a quadratic fit acts on.
The direction the gradient is paired with is retraction_differential(retraction, B, α) and not B itself. $\varphi'(\alpha) = \langle\nabla{}f(x(\alpha)), B\rangle$ holds only where $\alpha \mapsto \mathrm{retract}(\alpha{}B)$ is a one-parameter subgroup, which Geodesic is and Cayley is not; under Cayley the generator of the curve's velocity turns with the step, and the differential supplies it. The slope agrees with a central difference of the merit for both retractions at every $\alpha$.
At α = 0 the differential is B, copied into differential, so the Backtracking default pays a copy and no solve for it.
SimpleSolvers.linesearch_problem — Method
linesearch_problem(problem, gradient, cache, retraction, observer = NoStepObserver(),
workspace = nothing)Create a SimpleSolvers.LinesearchProblem for the linesearch algorithm.
The variable on which this problem depends is $\alpha$. The trial iterate is built by trial_iterate!, which is what makes this work for manifold parameters and not only for AbstractVectors.
Example
julia> x = [1, 0., 0.]
3-element Vector{Float64}:
1.0
0.0
0.0
julia> f = x -> sum(x .^ 3 / 6 + x .^ 2 / 2);
julia> obj = OptimizerProblem(f, x);
julia> grad = GradientAutodiff{Float64}(obj.F, length(x));
julia> hess = HessianAutodiff{Float64}(obj.F, length(x));
julia> cache = NewtonOptimizerCache(x);
julia> state = NewtonState(x); update!(state, grad, x);
julia> params = (x = state.x, state = state);
julia> update!(cache, state, grad, hess, x);
julia> ls_obj = linesearch_problem(obj, grad, cache, Cayley());
julia> ls_obj.F(0., params)
0.6666666666666666
julia> ls_obj.D(0., params)
-1.125
Note that in the example above calling update! on the NewtonOptimizerCache requires a SimpleSolvers.Hessian.
GeometricOptimizers.DecayingStatic — Type
DecayingStatic(; η₁, η₂, n)A SimpleSolvers.LinesearchMethod that takes no search at all, like SimpleSolvers.Static, but whose step size decays geometrically with the iteration number:
\[\alpha(t) = \gamma^t\eta_1, \qquad \gamma = \exp(\log(\eta_2/\eta_1)/n),\]
so that $\alpha(0) = \eta_1$ and $\alpha(n) = \eta_2$. It keeps decaying past n; the decay is not floored at $\eta_2$, because a floor is exactly what stops the iteration from converging (see below).
$\alpha(0)$ is a limit of the schedule and not a step a solve takes: solve! calls increase_iteration_number! before solver_step!, so the first step is $\alpha(1) = \gamma\eta_1$, the $k$-th is $\alpha(k)$, and the horizon is reached exactly at iteration n rather than one before it. $\eta_1$ is therefore an upper bound on the step rather than the first one — by a factor of $\gamma$, which for the defaults is 0.9908. The offset is deliberate: it is also how GeometricMachineLearning's AdamOptimizerWithDecay counts, which is what makes AdamOptimizerWithDecay reproduce it step for step (test/optimizers/decaying_static.jl asserts this against a live solve).
Why this exists
Adam produces a direction of magnitude $\approx{}1$ per component whatever the gradient is — that scale-freeness is the point of it. With a constant step $\alpha$ the iterate therefore does not converge to the minimizer but circles it at a distance of order $\alpha$, so no gradient-based stopping criterion is ever met and the solve runs to max_iterations. A step size that goes to zero is what turns it back into a convergent iteration.
The step size is the business of the line search, not of the OptimizerMethod, so the schedule is a LinesearchMethod; it composes with GradientMethod and MomentumMethod as well.
Examples
ls = DecayingStatic(; η₁ = 1e-2, η₂ = 1e-6, n = 1000)
# output
DecayingStatic from α = 0.01 to α = 1.0e-6 over 1000 iterations.The schedule carries no element type of the parameters: Optimizer and TrainingOptimizer convert it once, with change_precision, which computes $\gamma$ in the element type of the parameters.
solve_with_status reads iteration_number(params.state), which solver_step! puts into the line search's parameters. A DecayingStatic handed a params without a state therefore cannot work, and says so.
Like SimpleSolvers.Static, this has no αmax field of its own — the schedule is the caller's to fix — but a params.αmax clamps the step it hands back. On a manifold solver_step! supplies one; see linesearch_parameters.
GeometricOptimizers.AdamOptimizerWithDecay — Method
AdamOptimizerWithDecay(n_epochs; η₁, η₂, kwargs...)Adam paired with a DecayingStatic line search whose learning rate decays geometrically from η₁ to η₂ over n_epochs, returned as a NamedTuple to splat into Optimizer or TrainingOptimizer:
method = AdamOptimizerWithDecay(1000)
opt = Optimizer(x, problem; method...)
solve!(x, OptimizerState(method.algorithm, x), opt)The state has to be built from method.algorithm, so the pairing is worth binding to a name rather than splatting it twice.
There is no new type here and no schedule of its own: this is exactly Adam() and DecayingStatic(; η₁, η₂, n = n_epochs), under the name of GeometricMachineLearning's pairing. Neither carries the element type of the parameters; the optimizer converts both. The schedule advances once per optimization_step!.
Why it exists
The name comes from GeometricMachineLearning, for Adam's ρ₁, ρ₂, δ together with a learning-rate schedule η₁, η₂, n_epochs and the same $\gamma = \exp(\log(\eta_2/\eta_1)/n)$. Here the two halves live in different places — the direction in an OptimizerMethod, the step size in a SimpleSolvers.LinesearchMethod — and this function is the one name that holds both.
Despite the shared word, this has nothing to do with AdamWithEuclideanDecay. That one decays the weights, by $\lambda{}x$, and leaves the learning rate alone; this one decays the learning rate and never touches a weight. They compose, and neither implies the other — see the Two unrelated decays section of the weight-decay page.
Arguments
η₁, η₂ and n_epochs go to the line search; everything else is forwarded to Adam, so β₁, β₂ and δ — GML's ρ₁, ρ₂ and δ — keep Adam's own defaults rather than a second copy of them.
GML's signature is AdamOptimizerWithDecay(n_epochs, η₁ = 1f-2, η₂ = 1f-6, ρ₁ = 9f-1, ρ₂ = 9.9f-1, δ = 1f-8; T = typeof(η₁)). The step sizes and moment coefficients are keywords here, not positional arguments, and the coefficients are spelled β₁, β₂ as everywhere else in this package. GML's AdamOptimizerWithDecay(1000, 1f-3, 1f-8) becomes AdamOptimizerWithDecay(1000; η₁ = 1e-3, η₂ = 1e-8).
Examples
AdamOptimizerWithDecay(1000).linesearch
# output
DecayingStatic from α = 0.01 to α = 1.0e-6 over 1000 iterations.GeometricOptimizers.step_size — Method
step_size(schedule, t)The step size a training step takes in iteration t, where t is the iteration number the state reports — so t = 1 for the first step.
For a SimpleSolvers.Static this is its α at every t. For a DecayingStatic it is $\gamma^t\eta_1$, and t = 0 evaluates the schedule at a point no solve asks for (see the remark on $\alpha(0)$ there).