Page MenuHomec4science

steepest.tex
No OneTemporary

File Metadata

Created
Thu, Mar 13, 17:09

steepest.tex

\documentclass{article}
\usepackage{pstricks,pst-3dplot,pstricks-add,ifthen}
% steepestdescent{a}{b}{x0}{y0}{step}{iterations}{view}{algo}:
% a, b: ellipsoid axes
% x0, y0, step, iterations: gradient descent initial position, learning step and iterations
% view: 3D or 2D (top view)
% type: LS (least squares) or LMS (stochastic gradient descent)
%
\def\steepestdescent#1#2#3#4#5#6#7#8{%
% cartesian ellipsoid z = f(x, y); expects to find [x y] on the stack
\def\ellipsoidCPS{#2 div dup mul exch #1 div dup mul add}
% parametric form (x, y, z) = f(theta, rho); expects [theta rho] on the stack
\def\ellipsoidP{/r exch def dup cos #1 r mul mul exch sin #2 r mul mul r r mul}
% x and y updates in steepest descent: x -= stepsize * dz/dx
\ifthenelse{\equal{#8}{LMS}}{\def\rndp{rand 2147483647 div 0.8 sub 0.4 mul}}{\def\rndp{0}}
\def\gradx{ pop dup -2 #5 mul mul #1 dup mul div add \rndp\space add}
\def\grady{exch pop dup -2 #5 mul mul #2 dup mul div add \rndp\space add}
%
% center plot in psbox according to view
\ifthenelse{\equal{#7}{2D}}{\def\la{-2.6}\def\lb{3}}{\def\la{-1}\def\lb{5}}
\begin{pspicture}(-3,\la)(3,\lb)
\ifthenelse{\equal{#7}{2D}}{%
\psset{Beta=90}\psset{Alpha=180}}{
\psset{Beta=30}\psset{Alpha=150}}
\pstThreeDCoor[linecolor=darkgray,%
xMin=-2.5,xMax=2.5,nameX={$h_2$},
yMin=-2.5,yMax=2.5,nameY={$h_1$},
zMin=0,zMax=3,nameZ={}]
% plot the level sets
\parametricplotThreeD[xPlotpoints=180,yPlotpoints=20,linecolor=red,linewidth=0.5pt](0,360)(0, 2){%
t u \ellipsoidP}
\parametricplotThreeD[xPlotpoints=180,yPlotpoints=17,linecolor=red,linewidth=0.5pt](0, 2)(0,360){%
u t \ellipsoidP}
% gradient descent
\pstVerb{/xx #3 def /yy #4 def /xn xx yy \gradx\space def /yn xx yy \grady\space def}
\multido{\nA=0+1}{#6}{%
\pstThreeDLine[linecolor=blue,arrows=->](xx, yy, xx yy \ellipsoidCPS)(xn, yn, xn yn \ellipsoidCPS\space)%
\pstVerb{/xx xn def /yy yn def /xn xx yy \gradx\space def /yn xx yy \grady\space def}}
\end{pspicture}}
\begin{document}
\centering
\begin{tabular}{cc}
{\steepestdescent{1}{1}{-1.3}{1.3}{0.1}{15}{3D}{LMS}} &
{\steepestdescent{1}{1}{-1.3}{1.3}{0.1}{15}{2D}{LMS}}
\end{tabular}
\begin{tabular}{cc}
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.1}{15}{}{LMS} &
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.1}{25}{2D}{LMS}
\end{tabular}
\begin{tabular}{cc}
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.23}{7}{}{} &
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.23}{7}{2D}{}
\end{tabular}
\newpage
\footnotesize
\begin{verbatim}
% arguments:
% a, b: ellipsoid axes
% x0, y0, stepsize, iterations: gradient descent initialization
% 2D: top view
%
\def\steepestdescent#1#2#3#4#5#6#7{%
% cartesian ellipsoid z = f(x, y); expects to find [x y] on the stack
\def\ellipsoidCPS{#2 div dup mul exch #1 div dup mul add}
% parametric form (x, y, z) = f(theta, rho); expects [theta rho] on the stack
\def\ellipsoidP{/r exch def dup cos #1 r mul mul exch sin #2 r mul mul r r mul}
% x and y updates in steepest descent: x -= a dz/dx
\def\gradx{ pop dup -2 #5 mul mul #1 dup mul div add}
\def\grady{exch pop dup -2 #5 mul mul #2 dup mul div add}
%
% center plot in psbox according to view
\ifthenelse{\equal{#7}{2D}}{\def\la{-3}\def\lb{3}}{\def\la{-1}\def\lb{5}}
\begin{pspicture}(-3,\la)(3,\lb)
\ifthenelse{\equal{#7}{2D}}{%
\psset{Beta=90}\psset{Alpha=180}}{
\psset{Beta=30}\psset{Alpha=150}}
\pstThreeDCoor[linecolor=darkgray,%
xMin=-2.5,xMax=2.5,nameX={$h_2$},
yMin=-2.5,yMax=2.5,nameY={$h_1$},
zMin=0,zMax=3,nameZ={}]
% plot the level sets
\parametricplotThreeD[xPlotpoints=180,yPlotpoints=20,linecolor=red,linewidth=0.5pt]%
(0,360)(0, 2){t u \ellipsoidP}
\parametricplotThreeD[xPlotpoints=180,yPlotpoints=17,linecolor=red,linewidth=0.5pt]%
(0, 2)(0,360){u t \ellipsoidP}
% gradient descent
\pstVerb{/xx #3 def /yy #4 def /xn xx yy \gradx\space def /yn xx yy \grady\space def}
\multido{\nA=0+1}{#6}{%
\pstThreeDLine[linecolor=blue,arrows=->]%
(xx, yy, xx yy \ellipsoidCPS)(xn, yn, xn yn \ellipsoidCPS\space)%
\pstVerb{/xx xn def /yy yn def /xn xx yy \gradx\space def /yn xx yy \grady\space def}}
\end{pspicture}}
\begin{document}
\begin{tabular}{cc}
{\steepestdescent{1}{1}{-1.3}{1.3}{0.1}{15}{}} &
{\steepestdescent{1}{1}{-1.3}{1.3}{0.1}{15}{2D}}
\end{tabular}
\begin{tabular}{cc}
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.1}{15}{} &
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.1}{15}{2D}
\end{tabular}
\begin{tabular}{cc}
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.23}{7}{} &
\steepestdescent{1}{0.5}{-1.3}{0.8}{0.23}{7}{2D}
\end{tabular}
\end{document}
\end{verbatim}
\end{document}

Event Timeline