绘制梯度下降

绘制梯度下降

你能帮我画这张 SGD 图片吗?欢迎使用颜色和清晰的效果 :)

在此处输入图片描述

梅威瑟:

\documentclass[11pt]{article}  
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{subfigure}

\usepackage[font=footnotesize]{caption}
\usepackage{pgfplots}
\usepackage{tikz}

% Scriptsize axis style.
\pgfplotsset{every axis/.append style={tick label style={/pgf/number format/fixed},font=\scriptsize,ylabel near ticks,xlabel near ticks,grid=major}}

\begin{document}  
\begin{figure}
    \begin{tikzpicture}[samples=200,smooth]
        \begin{scope}
            \clip(-4,-1) rectangle (4,4);
            \draw plot[domain=0:360] ({cos(\x)*sqrt(20/(sin(2*\x)+2))},{sin(\x)*sqrt(20/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(16/(sin(2*\x)+2))},{sin(\x)*sqrt(16/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(12/(sin(2*\x)+2))},{sin(\x)*sqrt(12/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(8/(sin(2*\x)+2))},{sin(\x)*sqrt(8/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(4/(sin(2*\x)+2))},{sin(\x)*sqrt(4/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(1/(sin(2*\x)+2))},{sin(\x)*sqrt(1/(sin(2*\x)+2))});
            \draw plot[domain=0:360] ({cos(\x)*sqrt(0.0625/(sin(2*\x)+2))},{sin(\x)*sqrt(0.0625/(sin(2*\x)+2))});

            \draw[->,blue,ultra thick] (-2,3.65) to (-1.93,3);
            \draw[->,blue,ultra thick] (-1.93,3) to (-1.75,2.4);
            \draw[->,blue,ultra thick] (-1.75,2.4) to (-1.5,1.8);
            \draw[-`enter code here`>,blue,ultra thick] (-1.5,1.8) to (-1.15,1.3);          
            \node at (-1.4,3.8){\scriptsize $w[0]$};
            \node at (-1.2,3.2){\scriptsize $w[1]$};
            \node at (-1.05,2.6){\scriptsize $w[2]$};
            \node at (-0.8,2){\scriptsize $w[3]$};
            \node at (-0.6,1.4){\scriptsize $w[4]$};
        \end{scope}
    \end{tikzpicture}
\end{figure} 
\end{document}

在此处输入图片描述

答案1

可能的策略:

  • 预先定义并命名arrowcurve您的图应该与之相交的曲线路径(如下所示)。
  • 在 for 循环中绘制并命名所有图(curve\i如下所示)
  • 查找并命名曲线路径和图之间的所有交点。
  • 对于除第一个之外的每一个交互,在前一个和当前交点之间绘制一个箭头。沿路径添加带有所需文本的节点。

奖励更新:添加了颜色百分比变量,使图表根据函数改变颜色\y

\documentclass[11pt]{article}  
\usepackage{tikz}
\usetikzlibrary{calc,intersections}

\begin{document}  
\begin{figure}
    \begin{tikzpicture}[samples=50,smooth]
            %\clip(-4,-1) rectangle (4,4);
            \path[bend left,name path=arrowcurve] (-2,4) to[out=-30,in=-160] (0,0);
            \foreach \y[count=\i] in {20,16,12,8,4,1,0.0625}{
            \pgfmathsetmacro\colper{\y*4} % color percentage variable
                \draw[name path global/.expanded=curve\i,white!\colper!black] plot[domain=0:360] ({cos(\x)*sqrt(\y/(sin(2*\x)+2))},{sin(\x)*sqrt(\y/(sin(2*\x)+2))});
                \draw[name intersections = {of ={curve\i} and arrowcurve}](intersection-1) coordinate (P\i);
                \ifnum\i=1 
                    % do nothing
                \else%
                    \pgfmathtruncatemacro\imin{\i-1}
                    \pgfmathtruncatemacro\iprint{\i-2}
                    \draw[->, blue, ultra thick] (P\imin) -- (P\i) node[above right,midway] {\scriptsize $\hat{r}_{\iprint}$}; 
                \fi%
            }     
    \end{tikzpicture}
\end{figure} 
\end{document}

结果

答案2

稍微简化的图像:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                quotes,
                shapes.geometric}

\begin{document}
    \begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=2pt,
              node contents={}},
ell/.style = {ellipse, draw=gray, rotate=-5,
              minimum width=2*#1, minimum height=#1,
              node contents={}},
every edge/.style = {draw, -{Triangle[angle=60:1pt 3]},blue,ultra thick},
every edge quotes/.style = {font=\scriptsize, inner sep=1pt, auto, sloped}
                        ]
\node[dot];
\foreach \i [count=\c from 1] in {8, 16, 32, 44, 56}
\node (n\c) [ell=\i mm, line width=11.2/\i pt];
%
\draw
    (n5.north west) edge ["${w[0]}$"] (n4.north west)
    (n4.north west) edge ["${w[1]}$"]  (n3.north west)
    (n3.north west) edge ["${w[2]}$"]  (n2.north west);
    \end{tikzpicture}
\end{document}

附录: 带有弯曲箭头,不使用椭圆的节点形状:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                bending,
                intersections,
                quotes,
                shapes.geometric}

\begin{document}
    \begin{tikzpicture}[
every edge/.style = {draw, -{Triangle[angle=60:1pt 3,flex]},
                             bend right=11, blue,ultra thick},
every edge quotes/.style = {font=\scriptsize, inner sep=1pt, 
                            auto, sloped}
                            ]
\fill (0,0) circle[radius=3pt];
\path[name path=C] foreach \i in {4, 8, 16, 22, 28}
        {(0,0) circle[draw=red!\i, x radius=2*\i mm, y radius=\i mm, rotate=-5]};
\foreach \i in  {4, 8, 16, 22, 28}
    \draw[line width=11.2/\i, draw=white!\i!gray]
        (0,0) circle[x radius=2*\i mm, y radius=\i mm, rotate=-5];
\path[name path=V] (-4,2.4) .. controls + (0,-2) and + (-2,0) .. (0,0);
%
\draw [name intersections={of=C and V, sort by=C, name=A}]
        (A-5) edge ["${w[0]}$"] (A-4)
        (A-4) edge ["${w[1]}$"] (A-3)
        (A-3) edge ["${w[2]}$"] (A-2);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容