通过神经网络实现动画运动

通过神经网络实现动画运动

我想以动画形式呈现神经网络的运动过程,如下所示:

第 26 行:($(X27-Y2)!0.5!(X27-Y2)$)++(2.25cm,0)

第 37 行: (X0-Y0)+(.25 * \x,-0.25 * \y)node[draw=red,line width = 0.3mm,circle,inner

在此处输入图片描述


第 26 行:($(X27-Y2)!0.5!(X27-Y2)$)++(2.5cm,0)

第 37 行: (X1-Y0)+(.25 * \x,-0.25 * \y)node[draw=red,line width = 0.3mm,circle,inner 在此处输入图片描述

。 。 。


第 26 行:($(X27-Y2)!0.5!(X27-Y2)$)++(8cm,0)

第 37 行: (X23-Y0)+(.25 * \x,-0.25 * \y)node[draw=red,line width = 0.3mm,circle,inner

在此处输入图片描述

等等。动画能否快速运行以覆盖所有第一个隐藏层部分?

任何帮助将不胜感激!

以下是从 0 开始的代码:

\documentclass [10pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,calc,arrows.meta}

\begin{document}

Step1:

\begin{tikzpicture}[font=\sffamily,
        %Environment cfg
        >={Triangle[angle=45:.1cm 1]} % Arrows style by arrows.meta options
    ]
    \foreach \n [count=\y from 0] in {1, 2, ..., 28} {
        \foreach \d [count=\x from 0]in {1, 2, ..., 28} {
            \node[draw,circle,black!30,inner sep=0.75mm](X\x-Y\y) at (.25 * \d - 2, -0.25 * \n + 2){};
        }
    }

    \foreach \n in {1, 2, ..., 24} {
        \foreach \d in {1, 2, ..., 24} {
            \node[draw,circle,cyan,inner sep=0.75mm](X-Y) at (.25 * \d + 7, -0.25 * \n + 1.5){};
            }
    }

    \draw
    ($(X27-Y2)!0.5!(X27-Y2)$)++(2.25cm,0)
        node[draw,line width = 0.3mm,circle,black,inner sep=0.75mm](out1){};
    \draw
    ($(X14-Y0)!0.5!(X14-Y0)$)++(0,0.5cm)
        node[]{input neurons}
    (out1)++(2.5,0.5cm)
        node[]{first hidden layer};

    \foreach \n [count=\y from 0] in {1, 2, ..., 5} {
        \foreach \d [count=\x from 0] in {1, 2, ..., 5} {
            \draw
            (X0-Y0)+(.25 * \x,-0.25 * \y)node[draw=red,line width = 0.3mm,circle,inner sep=0.75mm](x\x-y\y){};
            \draw[->,blue!50!cyan,dashed]%Added some color
            (x\x-y\y) to [in=180, out=0] (out1);
        }
    }

\end{tikzpicture}

\end{document} 

答案1

制作这样的动画并不难。然而,更大的挑战是保持计算时间半合理。这可以通过只制作相关的圆节点,并用图案绘制其他节点来实现。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,patterns.meta}
\tikzdeclarepattern{
name=neurons,
type=uncolored,
bounding box={(-1.25mm,-1.25mm) and (1.25mm,1.25mm)},
tile size={(\tikztilesize,\tikztilesize)}, 
parameters={\tikzcircleradius,\tikztilesize}, 
tile transformation={xshift=1.75mm,yshift=1.75mm},
defaults={
radius/.store in=\tikzcircleradius,radius=1mm, 
tile size/.store in=\tikztilesize,tile size=2.5mm,
}, code={
\draw[thin] (0,0) circle[radius=\tikzcircleradius];
} }
\begin{document}
\foreach \Z in {1,...,24}
{\begin{tikzpicture}[font=\sffamily,>={Triangle[angle=45:.1cm 1]},
    circ/.style={circle,draw,inner sep=0pt,minimum size=2mm}]
 \path[pattern=neurons,local bounding box=input] 
     (-3.5,-3.5) rectangle +(7,7);
 \path[pattern=neurons,pattern color=cyan,
    local bounding box=first]  (5,-3) rectangle +(6,6)
    (.25*\Z+5-0.125, 3-0.125)   
          node[circ,thick,black] (first-1-1) {};
 \path foreach \X in {1,...,5}
  {foreach \Y in {1,...,5}       
  {(-3.5+.25 * \X -0.375+0.25*\Z, 3.5-0.25 * \Y+0.125)
   node[circ,red,thick] (input-\X-\Y){}}};
 \path (input.north) node[above]{input neurons}
   (first.north) node[above=1em]{first hidden layer}; 
 \foreach \X in  {1,...,5}
  {\foreach \Y in {1,...,5}
    {\draw[->,blue!50!cyan,shorten <={(\X-1)*0.25cm}]  (input-\X-\Y) to [in=180, out=0] (first-1-1);}}   
\end{tikzpicture}}
\end{document}

笔记:结果的pdf版本看起来不错。

在此处输入图片描述

但是,如果从中生成动画 gif,则圆圈不匹配!我不知道转换为什么会这样。但这意味着为了制作 gif,patterns似乎不是最佳选择。我不知道以前是否已经注意到这一点...

这是一段需要花费更多时间来编译但 gif 转换没有问题的代码。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\foreach \Z in {1,...,24}
{\begin{tikzpicture}[font=\sffamily,>={Triangle[angle=45:.1cm 1]},
    circ/.style={circle,draw,inner sep=0pt,minimum size=2mm}]
  % 
  \draw[local bounding box=input] foreach \Y in {1, 2, ..., 28} {
         foreach \X in {1, 2, ..., 28} {
          (.25 * \X - 28/8-0.25, -0.25 * \Y + 28/8+0.25) 
          circle[radius=1mm]}};
  \draw[local bounding box=first,xshift=8cm,cyan] foreach \Y in {1, 2, ..., 24} {
         foreach \X in {1, 2, ..., 24} {
          (.25 * \X - 24/8-0.25, -0.25 * \Y + 24/8+0.25) 
          circle[radius=1mm]}}
          (.25 * \Z- 24/8-0.25, 24/8)   
          node[circ,thick,black] (first-1-1) {};         
  \path foreach \X in {1,...,5}
  {foreach \Y in {1,...,5}       
  {(.25 * \X - 28/8-0.25+0.25*\Z-0.25, -0.25 * \Y + 28/8+0.25)
   node[circ,red,thick] (input-\X-\Y){}}};
  \path (input.north) node[above]{input neurons}
   (first.north) node[above]{first hidden layer}; 
  \foreach \X in  {1,...,5}
  {\foreach \Y in {1,...,5}
    {\draw[->,blue!50!cyan,shorten <={(\X-1)*0.25cm}]  (input-\X-\Y) to [in=180, out=0] (first-1-1);}}   
\end{tikzpicture}}
\end{document}

在此处输入图片描述

附录: 看过之后符号 1 的回答非常好我想补充一点,用虚线图案可以很容易地得到圆圈。关键因素是钥匙

dash pattern=on 0mm off 2.5mm,line cap=round,
double distance between line centers=2mm

其中2.5mm是圆心之间的距离,2mm是圆的半径。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta}

\begin{document}
\foreach \Z in {1,...,24}
{\begin{tikzpicture}[font=\sffamily,>={Triangle[angle=45:.1cm 1]},
    circ/.style={circle,draw,inner sep=0pt,minimum size=2mm}]
 \draw[local bounding box=input,
    dash pattern=on 0mm off 2.5mm,line cap=round,
    double distance between line centers=2mm] foreach \X in {1,...,28}
  {(-3.4cm+1.5\pgflinewidth,3.6cm-0.25*\X*1cm+1.5\pgflinewidth) -- ++ (6.75,0)};

 \draw[local bounding box=first,cyan,
    dash pattern=on 0mm off 2.5mm,line cap=round,
    double distance between line centers=2mm] foreach \X in {1,...,24}
  {(8cm-2.9cm+1.5\pgflinewidth,3.1cm-0.25*\X*1cm+1.5\pgflinewidth) -- ++ (5.75,0)};

 \path
    (.25*\Z+5-0.125, 3-0.125)   
          node[circ,thick,black] (first-1-1) {};
 \path foreach \X in {1,...,5}
  {foreach \Y in {1,...,5}       
  {(-3.5+.25 * \X -0.375+0.25*\Z, 3.5-0.25 * \Y+0.125)
   node[circ,red,thick] (input-\X-\Y){}}};
 \path (input.north) node[above]{input neurons}
   (first.north) node[above=1em]{first hidden layer}; 
 \foreach \X in  {1,...,5}
  {\foreach \Y in {1,...,5}
    {\draw[->,blue!50!cyan,shorten <={(\X-1)*0.25cm}]  (input-\X-\Y) to [in=180, out=0] (first-1-1);}}   
\end{tikzpicture}}
\end{document}

在此处输入图片描述

可以看出,没有任何偏移,并且编译时间非常短。

答案2

除了@Schrödinger's cat 的回答之外,这是一个补充材料。

如果您不介意用正方形代替圆形,那么有一种方法可以n^2用代码生成正方形O(1)。它使用dash pattern而不是pattern

\documentclass{article}
    \usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw[line width=200,dash pattern=on 2 off 2,dash phase=3]
            (-100pt,0)--(100pt,0);
        \draw[line width=200,dash pattern=on 2 off 2,dash phase=3]
            (0,-100pt)--(0,100pt);
        \draw[white,line width=200,dash pattern=on 2 off 6,dash phase=1]
            (0,-100pt)--(0,100pt);
        \draw[white,line width=200,dash pattern=on 2 off 6,dash phase=1]
            (-100pt,0)--(100pt,0);
    \end{tikzpicture}
\end{document}

8 位圆圈

\documentclass{article}
    \usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \def\verti{(-100pt,0)--(100pt,0)}\def\horiz{(0,-100pt)--(0,100pt)}
        \def\cross{\verti\horiz}
        \clip(-100pt,-100pt)rectangle(100pt,100pt);
        \begin{scope}[every path/.style={line width=200pt},
            blend mode=darken]
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on2off6,dash phase=5]\verti;
                \draw[dash pattern=on2off1on4off1,dash phase=1,white]\horiz;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[blend mode=darken]\verti;
                \draw[dash pattern=on4off1on2off1,dash phase=2,white]\cross;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on2off6,dash phase=5]\horiz;
                \draw[dash pattern=on2off1on4off1,dash phase=1,white]\verti;
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document}

八角形

\documentclass{article}
    \usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \def\verti{(-100pt,0)--(100pt,0)}\def\horiz{(0,-100pt)--(0,100pt)}
        \def\diago{(-100pt,-100pt)--(100pt,100pt)}
        \def\antid{(-100pt,100pt)--(100pt,-100pt)}
        \clip(-100pt,-100pt)rectangle(100pt,100pt);
        \begin{scope}[every path/.style={line width=300pt},
            blend mode=darken]
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off7,dash phase=6.5]\verti;
                \draw[dash pattern=on3off1on5off1,dash phase=1.5,white]\horiz;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off11.142135,dash phase=1.5]\diago;
                \draw[dash pattern=on7off1on5.142135off1,dash phase=3.5,
                    white]\antid;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off11.142135,dash phase=1.5]\antid;
                \draw[dash pattern=on7off1on5.142135off1,dash phase=3.5,
                    white]\diago;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off11.142135,dash phase=8.5710675]\diago;
                \draw[dash pattern=on7off1on5.142135off1,dash phase=10.5710675,
                    white]\antid;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off11.142135,dash phase=8.5710675]\antid;
                \draw[dash pattern=on7off1on5.142135off1,dash phase=10.5710675,
                    white]\diago;
            \end{scope}
            \begin{scope}[blend group=normal]
                \draw[dash pattern=on3off7,dash phase=6.5]\horiz;
                \draw[dash pattern=on3off1on5off1,dash phase=1.5,white]\verti;
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document}

相关内容