# How to Record Data during the Iterations

The recording and debug possiblities make it possible to record nearly any data during the iterations. This tutorial illustrates how to

• record one value during the iterations

• record multiple values during the iterations and access them afterwards

• define an own RecordAction to perform individual recordings.

Several predefined recordings exist, for example RecordCost() or RecordGradient(), depending on the solver used. For fields of the Options this can be directly done using the [RecordEntry(:field) For other recordings, for example more advanced computations before storing a value, an ownRecordAction can be defined.

We illustrate these using the gradient descent from the mean computation tutorial.

using Manopt, Manifolds, Random
begin
Random.seed!(42)
m = 30
M = Sphere(m)
n = 800
σ = π / 8
x = zeros(Float64, m + 1)
x[2] = 1.0
data = [exp(M, x, random_tangent(M, x, Val(:Gaussian), σ)) for i in 1:n]
end
800-element Vector{Vector{Float64}}:
[-0.054658825167894595, -0.5592077846510423, -0.04738273828111257, -0.04682080720921302, 0.12279468849667038, 0.07171438895366239, -0.12930045409417057, -0.22102081626380404, -0.31805333254577767, 0.0065859500152017645  …  -0.21999168261518043, 0.19570142227077295, 0.340909965798364, -0.0310802190082894, -0.04674431076254687, -0.006088297671169996, 0.01576037011323387, -0.14523596850249543, 0.14526158060820338, 0.1972125856685378]
[-0.08192376929745251, -0.5097715132187672, -0.008339904915541008, 0.07289741328038679, 0.11422036270613802, -0.11546739299835752, 0.22969969326284728, 0.14904671708359585, -0.11124820565850368, -0.11790721606521787  …  -0.1642124963047035, -0.24505758444677161, -0.07570080850379846, -0.07426218324072494, -0.026520181327346348, 0.1155534120525021, -0.029295576236512112, -0.09012096853677579, -0.23470556634911582, -0.026214242996704024]
[-0.22951484264859257, -0.6083825348640186, 0.14273766477054015, -0.11947823367023377, 0.05984293499234536, 0.058820835498203126, 0.07577331705863266, 0.1632847202946857, 0.20244385489915745, 0.04389826920203656  …  0.3222365119325929, 0.009728730325524067, -0.12094785371632395, -0.36322323926212824, -0.0689253407939657, 0.23356953371702974, 0.23489531397909744, 0.078303336494718, -0.14272984135578806, 0.07844539956202407]
[-0.0012588500237817606, -0.29958740415089763, 0.036738459489123514, 0.20567651907595125, -0.1131046432541904, -0.06032435985370224, 0.3366633723165895, -0.1694687746143405, -0.001987171245125281, 0.04933779858684409  …  -0.2399584473006256, 0.19889267065775063, 0.22468755918787048, 0.1780090580180643, 0.023703860700539356, -0.10212737517121755, 0.03807004103115319, -0.20569120952458983, -0.03257704254233959, 0.06925473452536687]
[-0.035534309946938375, -0.06645560787329002, 0.14823972268208874, -0.23913346587232426, 0.038347027875883496, 0.10453333143286662, 0.050933995140290705, -0.12319549375687473, 0.12956684644537844, -0.23540367869989412  …  -0.41471772859912864, -0.1418984610380257, 0.0038321446836859334, 0.23655566917750157, -0.17500681300994742, -0.039189751036839374, -0.08687860620942896, -0.11509948162959047, 0.11378233994840942, 0.38739450723013735]
[-0.31225399124694375, -0.3101935557860298, 0.17331136291070054, 0.0896859361620935, -0.18363442613679615, -0.06480023695256801, 0.18165070013886542, 0.1961827576799212, -0.07956460275570057, 0.032599735465655096  …  0.28454924187677677, 0.1740645587072168, -0.0531012303715687, -0.13820828129816268, 0.005830071475508363, 0.16739264037923052, 0.03436581437499533, 0.09107702398753295, -0.18772504287004085, 0.051164948978069225]
[-0.04159442361185588, -0.7768029783272633, 0.06303616666722486, 0.08070518925253539, -0.07396265237309446, -0.06008109299719321, 0.07977141629715745, 0.019511027129056415, 0.08629917589924847, -0.11156298867318722  …  0.0792587504128044, -0.016444383900170008, -0.181746064577005, -0.01888129512990984, -0.13523922089388968, 0.11358102175659832, 0.07929049608459493, 0.1689565359083833, 0.07673657951723721, -0.1128480905648813]
⋮
[-0.19830349374441875, -0.6086693423968884, 0.08552341811170468, 0.35781519334042255, 0.15790663648524367, 0.02712571268324985, 0.09855601327331667, -0.05840653973421127, -0.09546429767790429, -0.13414717696055448  …  -0.0430935804718714, 0.2678584478951765, 0.08780994289014614, 0.01613469379498457, 0.0516187906322884, -0.07383067566731401, -0.1481272738354552, -0.010532317187265649, 0.06555344745952187, -0.1506167863762911]
[-0.043475241251977756, -0.6327981074196991, -0.22111668003519114, 0.02822074679404562, -0.08550248815229336, 0.12821801740178354, 0.1779499563280025, -0.10247384887512372, 0.03964324641001163, -0.05825803381126274  …  0.12538932070835737, 0.0962820226976477, 0.31652954739473566, -0.14915034201394842, -0.13767278678177727, -0.004153096613530295, 0.09277957650773745, 0.059172645540316274, -0.12230262590034516, -0.19655728521529928]
[-0.10173946348675116, -0.6475660153977272, 0.1260284619729566, -0.11933160462857616, -0.04774310633937567, 0.09093928358804217, 0.041662676324043114, -0.1264739543938265, 0.09605293126911392, -0.16790474428001648  …  -0.04056684573478108, 0.09351665120940456, 0.15259195558799882, 0.0009949298312580497, 0.09461980828206303, 0.3067004514287283, 0.16129258773733715, -0.18893664085007542, -0.1806865244492513, 0.029319680436405825]
[-0.25178095432005304, -0.39147463259941434, -0.24359579328578632, 0.3017930975766573, 0.21658893985206487, 0.12304585275893234, 0.2828113308645171, 0.029187615341955332, 0.03616243507191925, 0.02937558890997916  …  -0.08071746662465405, -0.21761019282586586, 0.2094468492117083, 0.04303327342535272, -0.04050554246085358, 0.179355961490792, -0.08454569418519974, 0.054594159703393215, 0.12471741052450101, -0.24314124407858334]
[0.28156471341150974, -0.6708572780452595, -0.1410302363738465, -0.08322589397277698, -0.022772599832907418, -0.04447265789199677, -0.016448068022011157, -0.07490911512503738, 0.2778432295769144, -0.10191899088372378  …  -0.057272155080983836, 0.12817478092201395, 0.04623814480781884, -0.12184190164369117, 0.1987855635987229, -0.14533603246124993, -0.16334072868597016, -0.052369977381939437, 0.014904286931394959, -0.2440882678882144]
[0.12108727495744157, -0.714787344982596, 0.01632521838262752, 0.04437570556908449, -0.041199280304144284, 0.052984488452616, 0.03796520200156107, 0.2791785910964288, 0.11530429924056099, 0.12178223160398421  …  -0.07621847481721669, 0.18353870423743013, -0.19066653731436745, -0.09423224997242206, 0.14596847781388494, -0.09747986927777111, 0.16041150122587072, -0.02296513951256738, 0.06786878373578588, 0.15296635978447756]
F(M, y) = sum(1 / (2 * n) * distance.(Ref(M), Ref(y), data) .^ 2)
F (generic function with 1 method)
gradF(M, y) = sum(1 / n * grad_distance.(Ref(M), data, Ref(y)))
gradF (generic function with 1 method)

## Plain examples

For the high level interfaces of the solvers, like gradient_descent we have to set return_options to true to obtain the whole options structure and not only the resulting minimizer.

Then we can easily use the record= option to add recorded values. This kesword accepts RecordActions as well as several Symbols as shortcuts, for example :Cost to record the cost or if your options have a field f :f would record that entry.

R = gradient_descent(M, F, gradF, data[1]; record=:Cost, return_options=true)
RecordOptions{DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}, NamedTuple{(:Iteration,), Tuple{RecordCost}}}(DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}(GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([0.003348564074584566, -0.9989177042553862, 0.012603956508767278, 3.72419757281138e-5, 0.003476886502616871, 0.007393312784092389, 0.001513138638228341, -0.020957965195609864, 0.014862725166671122, -0.007213435090434026  …  -0.003341405125491461, 0.004841935449164263, -0.010592700164882451, -0.013017126496751096, 0.0033263346529508065, -0.0045300043949861495, 0.0030077996868381985, -0.01561032048437369, -0.0016794415617269005, -0.0025375720454376464], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-8, "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n")), "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n"), ConstantStepsize{Float64}(1.5707963267948966), [-1.3671120211908826e-10, 6.4628722295720364e-12, -7.622632933758616e-11, 4.491146553774455e-11, 4.209376436688705e-11, 5.201076857608636e-11, -9.267081294505285e-11, -1.8272699792067776e-10, -3.065679526101199e-10, -5.0313166989873065e-12  …  -6.598302941084998e-11, 1.740396233162675e-10, 5.369744942207606e-10, -1.1733961689124918e-10, -5.650580524318993e-11, 3.218762910828406e-11, 1.6751892570592702e-11, -1.8880139617842768e-10, 5.099352671622103e-11, 3.86580803257491e-10], ExponentialRetraction()), Dict{Symbol, DebugGroup}(:All => DebugGroup(DebugAction[DebugWarnIfCostIncreases(:Once, 0.5322977905736683, 1.0e-13)]))), (Iteration = RecordCost([0.5338138923660324, 0.532299080075227, 0.5322977917333718, 0.5322977905747528, 0.5322977905736697, 0.5322977905736683, 0.5322977905736687]),))

From the returned options, we see that the Options are encapsulated (decorated) with RecordOptions.

You can attach different recorders to some operations (:Start. :Stop, :Iteration at time of writing), where :Iteration is the default, so the following is the same as get_record(R, :Iteation). We get

get_record(R)
7-element Vector{Float64}:
0.5338138923660324
0.532299080075227
0.5322977917333718
0.5322977905747528
0.5322977905736697
0.5322977905736683
0.5322977905736687

To record more than one value, you can pass a array of a mix of symbols and RecordAction which formally introduces RecordGroup. Such a Group records a tuple of values in every iteration.

R2 = gradient_descent(M, F, gradF, data[1]; record=[:Iteration, :Cost], return_options=true)
RecordOptions{DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}, NamedTuple{(:Iteration,), Tuple{RecordGroup}}}(DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}(GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([0.003348564074584566, -0.9989177042553862, 0.012603956508767278, 3.72419757281138e-5, 0.003476886502616871, 0.007393312784092389, 0.001513138638228341, -0.020957965195609864, 0.014862725166671122, -0.007213435090434026  …  -0.003341405125491461, 0.004841935449164263, -0.010592700164882451, -0.013017126496751096, 0.0033263346529508065, -0.0045300043949861495, 0.0030077996868381985, -0.01561032048437369, -0.0016794415617269005, -0.0025375720454376464], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-8, "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n")), "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n"), ConstantStepsize{Float64}(1.5707963267948966), [-1.3671120211908826e-10, 6.4628722295720364e-12, -7.622632933758616e-11, 4.491146553774455e-11, 4.209376436688705e-11, 5.201076857608636e-11, -9.267081294505285e-11, -1.8272699792067776e-10, -3.065679526101199e-10, -5.0313166989873065e-12  …  -6.598302941084998e-11, 1.740396233162675e-10, 5.369744942207606e-10, -1.1733961689124918e-10, -5.650580524318993e-11, 3.218762910828406e-11, 1.6751892570592702e-11, -1.8880139617842768e-10, 5.099352671622103e-11, 3.86580803257491e-10], ExponentialRetraction()), Dict{Symbol, DebugGroup}(:All => DebugGroup(DebugAction[DebugWarnIfCostIncreases(:Once, 0.5322977905736683, 1.0e-13)]))), (Iteration = RecordGroup(RecordAction[RecordIteration([1, 2, 3, 4, 5, 6, 7]), RecordCost([0.5338138923660324, 0.532299080075227, 0.5322977917333718, 0.5322977905747528, 0.5322977905736697, 0.5322977905736683, 0.5322977905736687])], Dict(:Iteration => 1, :Cost => 2)),))

Here, the Symbol :Cost is mapped to using the RecordCost action. The same holds for :Iteration and :Iterate and any member field of the current Options. To access these you can first extract the group of records (that is where the :Iterations are recorded – note the Pluraö) and then access the :Cost

get_record_action(R2, :Iteration)
RecordGroup(RecordAction[RecordIteration([1, 2, 3, 4, 5, 6, 7]), RecordCost([0.5338138923660324, 0.532299080075227, 0.5322977917333718, 0.5322977905747528, 0.5322977905736697, 0.5322977905736683, 0.5322977905736687])], Dict(:Iteration => 1, :Cost => 2))

:Iteration is the default here, i.e. something recorded through the iterations – and we can access the recorded data the same way as we specify them in the record= keyword, that is, using the indexing operation.

get_record_action(R2)[:Cost]
7-element Vector{Float64}:
0.5338138923660324
0.532299080075227
0.5322977917333718
0.5322977905747528
0.5322977905736697
0.5322977905736683
0.5322977905736687

This can be also done by using a the high level interface get_record.

get_record(R2, :Iteration, :Cost)
7-element Vector{Float64}:
0.5338138923660324
0.532299080075227
0.5322977917333718
0.5322977905747528
0.5322977905736697
0.5322977905736683
0.5322977905736687

Note that the first symbol again refers to the point where we record (not to the thing we record). We can also pass a Tuple as second argument to have our own order (not that now the second :Iteration refers to the recorded iteratons)

get_record(R2, :Iteration, (:Iteration, :Cost))
7-element Vector{Tuple{Int64, Float64}}:
(1, 0.5338138923660324)
(2, 0.532299080075227)
(3, 0.5322977917333718)
(4, 0.5322977905747528)
(5, 0.5322977905736697)
(6, 0.5322977905736683)
(7, 0.5322977905736687)

## A more complex example

To illustrate a complicated example let's record

• the iteration number, cost and gradient field, but only every sixth iteration

• the iteration at which we stop

We first generate the problem and the options, to also illustrate the low-level works when not using gradient_descent.

p = GradientProblem(M, F, gradF)
GradientProblem{AllocatingEvaluation, Sphere{30, ℝ}, typeof(F), typeof(gradF)}(Sphere(30, ℝ), Main.workspace#13.F, Main.workspace#13.gradF)
o = GradientDescentOptions(
M,
copy(data[1]);
)
GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([-0.054658825167894595, -0.5592077846510423, -0.04738273828111257, -0.04682080720921302, 0.12279468849667038, 0.07171438895366239, -0.12930045409417057, -0.22102081626380404, -0.31805333254577767, 0.0065859500152017645  …  -0.21999168261518043, 0.19570142227077295, 0.340909965798364, -0.0310802190082894, -0.04674431076254687, -0.006088297671169996, 0.01576037011323387, -0.14523596850249543, 0.14526158060820338, 0.1972125856685378], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-9, "")), #undef), ConstantStepsize{Float64}(1.0), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], ExponentialRetraction())

We first build a RecordGroup to group the three entries we want to record per iteration. We then put this into a RecordEvery to only record this every 6th iteration

rI = RecordEvery(
RecordGroup([
:Iteration => RecordIteration(),
:Cost => RecordCost(),
]),
6,
)
RecordEvery(RecordGroup(RecordAction[RecordIteration(Int64[]), RecordCost(Float64[]), RecordEntry{Vector{Float64}}(Vector{Float64}[], :gradient)], Dict(:Iteration => 1, :Gradient => 3, :Cost => 2)), 6, true)

and a small option to record iterations

sI = RecordIteration()
RecordIteration(Int64[])

We now combine both into the RecordOptions decorator. It acts completely the same as an Option but records something in every iteration additionslly. This is stored in a dictionary of RecordActions, where :Iteration is the action (here the only every 6th iteration group) and the sI which is executed at stop.

Note that the keyword record= (in the high level interface gradient_descent only would fill the :Iteration symbol).

r = RecordOptions(o, Dict(:Iteration => rI, :Stop => sI))
RecordOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}, NamedTuple{(:Iteration, :Stop), Tuple{RecordEvery, RecordIteration}}}(GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([-0.054658825167894595, -0.5592077846510423, -0.04738273828111257, -0.04682080720921302, 0.12279468849667038, 0.07171438895366239, -0.12930045409417057, -0.22102081626380404, -0.31805333254577767, 0.0065859500152017645  …  -0.21999168261518043, 0.19570142227077295, 0.340909965798364, -0.0310802190082894, -0.04674431076254687, -0.006088297671169996, 0.01576037011323387, -0.14523596850249543, 0.14526158060820338, 0.1972125856685378], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-9, "")), #undef), ConstantStepsize{Float64}(1.0), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], ExponentialRetraction()), (Iteration = RecordEvery(RecordGroup(RecordAction[RecordIteration(Int64[]), RecordCost(Float64[]), RecordEntry{Vector{Float64}}(Vector{Float64}[], :gradient)], Dict(:Iteration => 1, :Gradient => 3, :Cost => 2)), 6, true), Stop = RecordIteration(Int64[])))

We now call the solver

res = solve(p, r)
RecordOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}, NamedTuple{(:Iteration, :Stop), Tuple{RecordEvery, RecordIteration}}}(GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([0.0033485640598485598, -0.9989177042539491, 0.012603956493466075, 3.7241967444740575e-5, 0.0034768865349409706, 0.007393312802598417, 0.001513138607714874, -0.020957965245003006, 0.01486272508600493, -0.007213435086688199  …  -0.003341405180698119, 0.004841935496011185, -0.010592700080354562, -0.013017126499757148, 0.003326334644726621, -0.004530004393003033, 0.0030077996912374547, -0.015610320516095559, -0.0016794415289985544, -0.0025375719951008136], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-9, "The algorithm reached approximately critical point after 23 iterations; the gradient norm (4.1219635299363995e-10) is less than 1.0e-9.\n")), "The algorithm reached approximately critical point after 23 iterations; the gradient norm (4.1219635299363995e-10) is less than 1.0e-9.\n"), ConstantStepsize{Float64}(1.0), [-3.5044304535093024e-11, 2.877183696201296e-12, -3.048456771709935e-11, -1.0315280612105057e-11, 5.397456626473262e-11, 3.322652209251063e-11, -5.709966847895808e-11, -9.534786886899668e-11, -1.5422092219268858e-10, 4.771788595769183e-12  …  -9.270955611008751e-11, 8.982541902131935e-11, 1.8128432929072e-10, -1.591651394288341e-11, -1.9069035060844883e-11, 5.18406517257923e-12, 8.188572568493604e-12, -6.746130957934675e-11, 5.758341064176706e-11, 1.1318273152472608e-10], ExponentialRetraction()), (Iteration = RecordEvery(RecordGroup(RecordAction[RecordIteration([6, 12, 18]), RecordCost([0.5323010518577256, 0.5322977906049865, 0.5322977905736688]), RecordEntry{Vector{Float64}}([[-0.0003815871897816863, 8.243775455566553e-5, -0.0003580924733205367, -0.00025905876141471985, 0.0007619214859574309, 0.00042771503965277955, -0.0008076042524146077, -0.001301005673661614, -0.002067096714886248, 6.95591037894914e-5  …  -0.0013504353686798849, 0.0012094473304958176, 0.002242981930382096, -0.00016380134227963496, -0.00029940162921400943, -7.142360524207785e-6, 9.12491757736006e-5, -0.0008577966654541865, 0.0008956067961453918, 0.0013124151032857284], [-1.257809885540879e-6, 1.161830737771711e-7, -1.1492279085900465e-6, -6.524413245189825e-7, 2.2877343291773742e-6, 1.3267058701930327e-6, -2.4124385063482878e-6, -3.9393828444976445e-6, -6.298004499543411e-6, 2.0690731769080518e-7  …  -4.005152410773482e-6, 3.6787948340169237e-6, 7.0274413921097645e-6, -5.44530075366725e-7, -8.555011713754683e-7, 6.735388962414587e-8, 2.999482926471439e-7, -2.660651049509654e-6, 2.5913354877699053e-6, 4.218349473221472e-6], [-4.128322535305049e-9, 3.5608960924738504e-10, -3.6735693483751306e-9, -1.5970797735920207e-9, 6.853233819771999e-9, 4.105108633502708e-9, -7.225021167723254e-9, -1.1944745391357856e-8, -1.9223517714293814e-8, 6.141461665663903e-10  …  -1.1867770369406762e-8, 1.1209332635383519e-8, 2.206437282642644e-8, -1.8248808981405525e-9, -2.470219776450587e-9, 4.557867598967147e-10, 9.752551415097635e-10, -8.27197111830219e-9, 7.503390021102287e-9, 1.3546235653268297e-8]], :gradient)], Dict(:Iteration => 1, :Gradient => 3, :Cost => 2)), 6, true), Stop = RecordIteration([23])))

And we can check the recorded value at :Stop to see how many iterations were performed

get_record(res, :Stop)
1-element Vector{Int64}:
23

and the other values during the iterations are

get_record(res, :Iteration, (:Iteration, :Cost))
3-element Vector{Tuple{Int64, Float64}}:
(6, 0.5323010518577256)
(12, 0.5322977906049865)
(18, 0.5322977905736688)

## Writing an own RecordActions

Let's investigate where we want to count the number of function evaluations, again just to illustrate, since for the gradient this is just one evaluation per iteration. We first define a cost, that counts it's own calls.

begin
mutable struct MyCost{T}
data::T
count::Int
end
MyCost(data::T) where {T} = MyCost{T}(data, 0)
function (c::MyCost)(M, x)
c.count += 1
return sum(1 / (2 * length(c.data)) * distance.(Ref(M), Ref(x), c.data) .^ 2)
end
end

and we define the following RecordAction, which is a Functor, i.e. a struct that is also a function. The function we have to implement is similar to a single solver step in signature, since it might get called every iteration:

begin
mutable struct RecordCount <: RecordAction
recorded_values::Vector{Int}
RecordCount() = new(Vector{Int}())
end
function (r::RecordCount)(p::Problem, ::Options, i)
if i > 0
push!(r.recorded_values, p.cost.count)
elseif i < 0 # reset if negative
r.recorded_values = Vector{Int}()
end
end
end

Now we can initialize the new cost and call the gradient descent. Note that this illustrates also the last use case – you can pass symbol-Action pairs into the record=array.

F2 = MyCost(data)
MyCost{Vector{Vector{Float64}}}([[-0.054658825167894595, -0.5592077846510423, -0.04738273828111257, -0.04682080720921302, 0.12279468849667038, 0.07171438895366239, -0.12930045409417057, -0.22102081626380404, -0.31805333254577767, 0.0065859500152017645  …  -0.21999168261518043, 0.19570142227077295, 0.340909965798364, -0.0310802190082894, -0.04674431076254687, -0.006088297671169996, 0.01576037011323387, -0.14523596850249543, 0.14526158060820338, 0.1972125856685378], [-0.08192376929745251, -0.5097715132187672, -0.008339904915541008, 0.07289741328038679, 0.11422036270613802, -0.11546739299835752, 0.22969969326284728, 0.14904671708359585, -0.11124820565850368, -0.11790721606521787  …  -0.1642124963047035, -0.24505758444677161, -0.07570080850379846, -0.07426218324072494, -0.026520181327346348, 0.1155534120525021, -0.029295576236512112, -0.09012096853677579, -0.23470556634911582, -0.026214242996704024], [-0.22951484264859257, -0.6083825348640186, 0.14273766477054015, -0.11947823367023377, 0.05984293499234536, 0.058820835498203126, 0.07577331705863266, 0.1632847202946857, 0.20244385489915745, 0.04389826920203656  …  0.3222365119325929, 0.009728730325524067, -0.12094785371632395, -0.36322323926212824, -0.0689253407939657, 0.23356953371702974, 0.23489531397909744, 0.078303336494718, -0.14272984135578806, 0.07844539956202407], [-0.0012588500237817606, -0.29958740415089763, 0.036738459489123514, 0.20567651907595125, -0.1131046432541904, -0.06032435985370224, 0.3366633723165895, -0.1694687746143405, -0.001987171245125281, 0.04933779858684409  …  -0.2399584473006256, 0.19889267065775063, 0.22468755918787048, 0.1780090580180643, 0.023703860700539356, -0.10212737517121755, 0.03807004103115319, -0.20569120952458983, -0.03257704254233959, 0.06925473452536687], [-0.035534309946938375, -0.06645560787329002, 0.14823972268208874, -0.23913346587232426, 0.038347027875883496, 0.10453333143286662, 0.050933995140290705, -0.12319549375687473, 0.12956684644537844, -0.23540367869989412  …  -0.41471772859912864, -0.1418984610380257, 0.0038321446836859334, 0.23655566917750157, -0.17500681300994742, -0.039189751036839374, -0.08687860620942896, -0.11509948162959047, 0.11378233994840942, 0.38739450723013735], [-0.31225399124694375, -0.3101935557860298, 0.17331136291070054, 0.0896859361620935, -0.18363442613679615, -0.06480023695256801, 0.18165070013886542, 0.1961827576799212, -0.07956460275570057, 0.032599735465655096  …  0.28454924187677677, 0.1740645587072168, -0.0531012303715687, -0.13820828129816268, 0.005830071475508363, 0.16739264037923052, 0.03436581437499533, 0.09107702398753295, -0.18772504287004085, 0.051164948978069225], [-0.04159442361185588, -0.7768029783272633, 0.06303616666722486, 0.08070518925253539, -0.07396265237309446, -0.06008109299719321, 0.07977141629715745, 0.019511027129056415, 0.08629917589924847, -0.11156298867318722  …  0.0792587504128044, -0.016444383900170008, -0.181746064577005, -0.01888129512990984, -0.13523922089388968, 0.11358102175659832, 0.07929049608459493, 0.1689565359083833, 0.07673657951723721, -0.1128480905648813], [-0.21221814304651335, -0.5031823821503253, 0.010326342133992458, -0.12438192100961257, 0.04004758695231872, 0.2280527500843805, -0.2096243232022162, -0.16564828762420294, -0.28325749481138984, 0.17033534605245823  …  -0.13599096505924074, 0.28437770540525625, 0.08424426798544583, -0.1266207606984139, 0.04917635557603396, -0.00012608938533809706, -0.04283220254770056, -0.08771365647566572, 0.14750169103093985, 0.11601120086036351], [0.10683290707435533, -0.17680836277740178, 0.237674583018994, 0.12011180867097296, -0.029404774462600147, 0.11522028383799932, -0.33181744809745184, -0.17859266746938368, 0.04352373642537758, 0.25303828026679875  …  0.08879861736692071, -0.0044125069878017285, 0.19786810509925892, -0.13971046827270436, 0.09482328498485092, 0.05108149065160892, -0.1457834350695163, 0.31674797726604376, 0.1042267316918273, 0.21573150015891307], [-0.024895624707466164, -0.7473912016432697, -0.1392537238944721, -0.14948896791465557, -0.09765393283580377, 0.04413059403279867, -0.13865379004720355, -0.071032040283992, 0.15604054722246585, -0.10744260463413555  …  -0.14748067081342833, -0.14743635071251024, 0.0643591937981352, 0.16138827697852615, -0.12656652133603935, -0.06463635704869083, 0.14329582429103488, -0.01113113793821713, 0.29295387893749997, 0.06774523575259782]  …  [0.011874845316569958, -0.6910596618389591, 0.2127574143947781, -0.014042545524367428, -0.07883613103495009, -0.002190096669624676, -0.03383643046422047, 0.2925813113264833, -0.04718187201980004, 0.03949680289730033  …  0.08677365866032935, 0.040468251005154374, -0.2477981384858724, -0.2863151460287713, -0.07211767532456784, -0.15072898498180462, 0.01785592362182673, -0.09795357710255247, -0.14755229203084913, 0.1305005778855435], [0.013457629515450426, -0.3750353654626534, 0.12349883726772073, 0.3521803555005319, 0.2475921439420274, 0.006088649842999206, 0.31203183112392907, -0.036869203979483754, -0.07475746464056504, -0.029297797064479717  …  0.16867368684091563, -0.09450564983271922, -0.0587273302122711, -0.1326667940553803, -0.25530237980444614, 0.37556905374043376, 0.04922612067677609, 0.2605362549983866, -0.21871556587505667, -0.22915883767386164], [0.03295085436260177, -0.971861604433394, 0.034748713521512035, -0.0494065013245799, -0.01767479281403355, 0.0465459739459587, 0.007470494722096038, 0.003227960072276129, 0.0058328596338402365, -0.037591237446692356  …  0.03205152122876297, 0.11331109854742015, 0.03044900529526686, 0.017971704993311105, -0.009329252062960229, -0.02939354719650879, 0.022088835776251863, -0.02546111553658854, -0.0026257225461427582, 0.005702111697172774], [0.06968243992532257, -0.7119502191435176, -0.18136614593117445, -0.1695926215673451, 0.01725015359973796, -0.00694164951158388, -0.34621134287344574, 0.024709256792651912, -0.1632255805999673, -0.2158226433583082  …  -0.14153772108081458, -0.11256850346909901, 0.045109821764180706, -0.1162754336222613, -0.13221711766357983, 0.005365354776191061, 0.012750671705879105, -0.018208207549835407, 0.12458753932455452, -0.31843587960340897], [-0.19830349374441875, -0.6086693423968884, 0.08552341811170468, 0.35781519334042255, 0.15790663648524367, 0.02712571268324985, 0.09855601327331667, -0.05840653973421127, -0.09546429767790429, -0.13414717696055448  …  -0.0430935804718714, 0.2678584478951765, 0.08780994289014614, 0.01613469379498457, 0.0516187906322884, -0.07383067566731401, -0.1481272738354552, -0.010532317187265649, 0.06555344745952187, -0.1506167863762911], [-0.043475241251977756, -0.6327981074196991, -0.22111668003519114, 0.02822074679404562, -0.08550248815229336, 0.12821801740178354, 0.1779499563280025, -0.10247384887512372, 0.03964324641001163, -0.05825803381126274  …  0.12538932070835737, 0.0962820226976477, 0.31652954739473566, -0.14915034201394842, -0.13767278678177727, -0.004153096613530295, 0.09277957650773745, 0.059172645540316274, -0.12230262590034516, -0.19655728521529928], [-0.10173946348675116, -0.6475660153977272, 0.1260284619729566, -0.11933160462857616, -0.04774310633937567, 0.09093928358804217, 0.041662676324043114, -0.1264739543938265, 0.09605293126911392, -0.16790474428001648  …  -0.04056684573478108, 0.09351665120940456, 0.15259195558799882, 0.0009949298312580497, 0.09461980828206303, 0.3067004514287283, 0.16129258773733715, -0.18893664085007542, -0.1806865244492513, 0.029319680436405825], [-0.25178095432005304, -0.39147463259941434, -0.24359579328578632, 0.3017930975766573, 0.21658893985206487, 0.12304585275893234, 0.2828113308645171, 0.029187615341955332, 0.03616243507191925, 0.02937558890997916  …  -0.08071746662465405, -0.21761019282586586, 0.2094468492117083, 0.04303327342535272, -0.04050554246085358, 0.179355961490792, -0.08454569418519974, 0.054594159703393215, 0.12471741052450101, -0.24314124407858334], [0.28156471341150974, -0.6708572780452595, -0.1410302363738465, -0.08322589397277698, -0.022772599832907418, -0.04447265789199677, -0.016448068022011157, -0.07490911512503738, 0.2778432295769144, -0.10191899088372378  …  -0.057272155080983836, 0.12817478092201395, 0.04623814480781884, -0.12184190164369117, 0.1987855635987229, -0.14533603246124993, -0.16334072868597016, -0.052369977381939437, 0.014904286931394959, -0.2440882678882144], [0.12108727495744157, -0.714787344982596, 0.01632521838262752, 0.04437570556908449, -0.041199280304144284, 0.052984488452616, 0.03796520200156107, 0.2791785910964288, 0.11530429924056099, 0.12178223160398421  …  -0.07621847481721669, 0.18353870423743013, -0.19066653731436745, -0.09423224997242206, 0.14596847781388494, -0.09747986927777111, 0.16041150122587072, -0.02296513951256738, 0.06786878373578588, 0.15296635978447756]], 0)
R3 = gradient_descent(
M,
F2,
data[1];
record=[:Iteration, :Count => RecordCount(), :Cost],
return_options=true,
)
RecordOptions{DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}, NamedTuple{(:Iteration,), Tuple{RecordGroup}}}(DebugOptions{GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}}(GradientDescentOptions{Vector{Float64}, Vector{Float64}, StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}, ConstantStepsize{Float64}, ExponentialRetraction}([0.003348564074584566, -0.9989177042553862, 0.012603956508767278, 3.72419757281138e-5, 0.003476886502616871, 0.007393312784092389, 0.001513138638228341, -0.020957965195609864, 0.014862725166671122, -0.007213435090434026  …  -0.003341405125491461, 0.004841935449164263, -0.010592700164882451, -0.013017126496751096, 0.0033263346529508065, -0.0045300043949861495, 0.0030077996868381985, -0.01561032048437369, -0.0016794415617269005, -0.0025375720454376464], IdentityUpdateRule(), StopWhenAny{Tuple{StopAfterIteration, StopWhenGradientNormLess}}((StopAfterIteration(200, ""), StopWhenGradientNormLess(1.0e-8, "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n")), "The algorithm reached approximately critical point after 7 iterations; the gradient norm (1.124221389254571e-9) is less than 1.0e-8.\n"), ConstantStepsize{Float64}(1.5707963267948966), [-1.3671120211908826e-10, 6.4628722295720364e-12, -7.622632933758616e-11, 4.491146553774455e-11, 4.209376436688705e-11, 5.201076857608636e-11, -9.267081294505285e-11, -1.8272699792067776e-10, -3.065679526101199e-10, -5.0313166989873065e-12  …  -6.598302941084998e-11, 1.740396233162675e-10, 5.369744942207606e-10, -1.1733961689124918e-10, -5.650580524318993e-11, 3.218762910828406e-11, 1.6751892570592702e-11, -1.8880139617842768e-10, 5.099352671622103e-11, 3.86580803257491e-10], ExponentialRetraction()), Dict{Symbol, DebugGroup}(:All => DebugGroup(DebugAction[DebugWarnIfCostIncreases(:Once, 0.5322977905736683, 1.0e-13)]))), (Iteration = RecordGroup(RecordAction[RecordIteration([1, 2, 3, 4, 5, 6, 7]), RecordCount([2, 4, 6, 8, 10, 12, 14]), RecordCost([0.5338138923660324, 0.532299080075227, 0.5322977917333718, 0.5322977905747528, 0.5322977905736697, 0.5322977905736683, 0.5322977905736687])], Dict(:Iteration => 1, :Count => 2, :Cost => 3)),))

For :Cost we already learned how to access them, the :Count => introduces the following action to obtain the :Count. We can again access the whole sets of records

get_record(R3)
7-element Vector{Tuple{Int64, Int64, Float64}}:
(1, 2, 0.5338138923660324)
(2, 4, 0.532299080075227)
(3, 6, 0.5322977917333718)
(4, 8, 0.5322977905747528)
(5, 10, 0.5322977905736697)
(6, 12, 0.5322977905736683)
(7, 14, 0.5322977905736687)

this is equivalent to calling R[:Iteration]. Note that since we introduced :Count we can also access a single recorded value using

R3[:Iteration, :Count]
7-element Vector{Int64}:
2
4
6
8
10
12
14`

And we see that the cost function is called once per iteration.