Inferring shape of optimization surface from shape of loss curve​
​by Yaroslav Bulatov
Thu 9 Sep 2021 00:34:58
https://mathematica.stackexchange.com/a/255374/217
Suppose we do gradient descent on a quadratic centered at 0. Can we infer the shape of the quadratic by looking at the sequence of observed losses?
​
It turns out that when we have a formula for average loss at step
t
, we can obtain formula for
x
th largest eigenvalue by using a combination of InverseLaplaceTransform and DSolve, a connection first elucidated by yarchik on Mathematica Stack Exchange.
​
The notebook below works through the steps of obtaining
1/
2
x
spectrum of a quadratic from the formula for average loss after
t
steps of gradient descent with learning rate 1 on this quadratic.
​
It would be quite interesting to discover a numeric analogue, because this would allow one to guess the shape of optimization problem, say, when training a neural network, by looking at an observed sequence of losses.
​
A single loss curve works as a substitute for average loss curve because in high dimensions almost all loss curves concentrate around average (rigorous derivation for another special case)
​
Suppose g(x) represents x’th largest eigenvalue of quadratic Q and f(t) is proportional to the average loss of gradient descent on Q at step t when using learning rate α and starting from random position on a sphere. We can write​
f(t)=
∞
∫
0
g(x)Exp(-2αg(x))
dx​For derivation of this equation, see this note​​Recipe below shows how to write f(t) in terms of Laplace transform of g(x), and how to obtain g(x) from f(t) using inverse Laplace transform

Forward transformation from Laplace transform

Given eigenvalues decaying as
1
2
x
obtain expression for loss after t gradient descent steps using Laplace transform, compare against simulation.​First construct Heavy function to convert b..a interval into 0..∞ Assuming decreasing g(x), will need b=g(∞), a=g(0), b<a
In[]:=
With[{bb=1,aa=2},​​points=ListPlot[{Labeled[{aa,1},"a"],Labeled[{bb,1},"b"]},FillingAxis,FillingStyleDashed];​​theta=HeavisideTheta[y-bb]-HeavisideTheta[y-aa];​​label=HoldForm[HeavisideTheta[y-b]-HeavisideTheta[y-a]]//TraditionalForm;​​plot=Plot[theta,{y,0,3},Filling->Axis,PlotRangePadding->.2,PlotLabellabel];​​Show[plot,points]​​]
Out[]=
Obtain f(t) using Laplace Transform
In[]:=
g[x_]=
1
2
(1+x)
;​​gi=InverseFunction[g];​​a=g[0];​​b=g[∞];​​theta=HeavisideTheta[y-b]-HeavisideTheta[y-a];​​sol1=LaplaceTransform[D[gi[y],y]*y*theta,y,t]//FullSimplify
InverseFunction
:Inverse functions are being used. Values may be lost for multivalued inverses.
Out[]=
π
Erf[
t
]
2
t
Obtain f(t) using original definition
In[]:=
sol2=Integrate[g[x]Exp[-tg[x]],{x,0,∞}]
Out[]=
π
Erf[
t
]
2
t
In[]:=
Reduce[sol1==sol2]
Out[]=
True
Obtain f(t) by running gradient descent on large n, compare against “infinite n” representations above
In[]:=
n=1000;(*dimensionoftheproblem*)​​maxStep=50;​​h=Table[1.
2
i
,{i,1,n}];(*Hessianeigenvalues*)​​x0=ConstantArray[1.,{n}];(*startingpoint,producesaveragetrajectory*)​​xs=NestList[(#-h#)&,x0,maxStep/2];​​ys=Total[
2
#
*h]&/@xs;​​steps=Range[Length[xs]]*2-1;(*correctformissingconstants*)​​​​discretePlot=ListPlot[Transpose[{steps,ys}],PlotLegends{"exact"},PlotStyle->ColorData[97][2],PlotRangeAll,Filling->Axis,AxesLabel{"steps","loss"},PlotLabel->StringForm["gradient descent with n=``",n]];​​continuousPlot=Plot[sol1,{t,0,maxStep+1},PlotRange->All,PlotLegends{"infinite-n"}];​​Show[discretePlot,continuousPlot,PlotRange->All]
Out[]=
exact
infinite-n

Backward transformation from inverse Laplace transform

In[]:=
Clear["Global`*"]
Clear
:Symbol toc is Protected.
In[]:=
(*InverseLaplacegivesgi'ytheta,nowsolveforgi*)​​ilaplace=InverseLaplaceTransform
π
Erf[
t
]
2
t
,t,x
Out[]=
1-HeavisideTheta[-1+x]
2
x
Manually get a,b by finding non-zero boundary, and get representation free of HeavysideTheta
In[]:=
Plot[ilaplace,{x,-2,2}]
Out[]=
-2
-1
1
2
0.5
1.0
1.5
2.0
In[]:=
{b,a}={0,1};(*g[0]=a,g[∞]=b,gi[a]=0*)​​theta=HeavisideTheta[x-b]-HeavisideTheta[x-a];​​Plotilaplace,theta
1
2
x
,{x,-1,2},PlotStyle{Thick,Dashed}​​
Out[]=
-1.0
-0.5
0.5
1.0
1.5
2.0
0.5
1.0
1.5
In[]:=
F[x_]=Refine[ilaplace,{b<x<a}]
Out[]=
1
2
x
In[]:=
hi=Functiony,Evaluate@DSolveValuegi'[y]y==
1
2
y
,gi[a]==0,gi[y],y
Out[]=
Functiony,
-1+
y
y
