如何在 Latex 中使用 tikz 绘制循环或反馈神经网络?

如何在 Latex 中使用 tikz 绘制循环或反馈神经网络?

我正在写论文。我在用 Tikz 在 latex 中绘制循环神经网络时遇到了问题,一个有隐藏层,另一个没有隐藏层,如附图所示。 在此处输入图片描述

我搜索了很多,但在网上找不到解决方案。我有一个 tikz 代码来绘制前馈神经网络,这是我在网上找到的。

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
    plain/.style={
        draw=none,
        fill=none,
    },
    net/.style={
        matrix of nodes,
        nodes={
            draw,
            circle,
            inner sep=8.5pt
        },
        nodes in empty cells,
        column sep=0.6cm,
        row sep=-11pt
    },
    >=latex
    ]
    \matrix[net] (mat)
    {
        |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
        & |[plain]| \\
        |[plain]| & \\
        & |[plain]| \\
        |[plain]| & |[plain]| \\
        & & \\
        |[plain]| & |[plain]| \\
        & |[plain]| \\
        |[plain]| & \\
        & |[plain]| \\
    };
    \foreach \ai [count=\mi ]in {2,4,...,10}
    \draw[<-] (mat-\ai-1) -- node[above] {} +(-1cm,0);
    \foreach \ai in {2,4,...,10}
    {\foreach \aii in {3,6,9}
        \draw[->] (mat-\ai-1) -- (mat-\aii-2);
    }
    \foreach \ai in {3,6,9}
    \draw[->] (mat-\ai-2) -- (mat-6-3);
    \draw[->] (mat-6-3) -- node[above] {} +(1cm,0);
    \end{tikzpicture}
    
    \caption{Multilayer feedforward neural network with one hidden layer.}
    \label{hiddenlayer}
\end{figure} 

请帮我解决这个问题。

答案1

在此处输入图片描述

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{decorations.markings}

\tikzset{->-/.style={decoration={
            markings,
            mark=at position #1 with {\arrow{>}}},postaction={decorate}}}
\begin{document}
     \begin{figure}[htp]
        \centering
        \begin{tikzpicture}[
            plain/.style={
                draw=none,
                fill=none,
            },
            net/.style={
                matrix of nodes,
                nodes={
                    draw,
                    circle,
                    inner sep=8.5pt
                },
                nodes in empty cells,
                column sep=0.6cm,
                row sep=-11pt
            },
            >=latex
            ]
            \matrix[net] (mat)
            {
                |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
                & |[plain]| \\
                |[plain]| & \\
                & |[plain]| \\
                |[plain]| & |[plain]| \\
                & & \\
                |[plain]| & |[plain]| \\
                & |[plain]| \\
                |[plain]| & \\
                & |[plain]| \\
            };
        \draw[->-=.5] (mat-6-3) --  ++(1cm,0)--++(0,4cm)coordinate(a);
            \foreach \ai [count=\mi ]in {2,4,...,10}
            \draw[<-] (mat-\ai-1) --  ++(-1cm,0)|-(a);
            \foreach \ai in {2,4,...,10}
            {\foreach \aii in {3,6,9}
                \draw[->] (mat-\ai-1) -- (mat-\aii-2);
            }
            \foreach \ai in {3,6,9}
            \draw[->] (mat-\ai-2) -- (mat-6-3);
%           \draw[->] (mat-6-3) --  ++(1cm,0)--++(0,4cm)coordinate(a);
        \end{tikzpicture}
        
        \caption{Multilayer feedforward neural network with one hidden layer.}
        \label{hiddenlayer}
     \end{figure} 
\end{document}

答案2

在此处输入图片描述

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,calc}
\begin{document}
\begin{tikzpicture}[
  neuron/.style={circle,draw,minimum size=7mm},
  on grid,
  > = stealth',
  rounded corners
  ]
  % nodes
  \node[neuron] (a) {};
  \node[neuron,below=of a] (b) {};
  \node[neuron,below=of b] (c) {};
  \node[neuron,below=of c] (d) {};
  \node[neuron,below=of d] (e) {};
  \node[neuron,right=2 of c] (B) {};
  \node[neuron,above=1.5 of B] (A) {};
  \node[neuron,below=1.5 of B] (C) {};
  % right upper corner of edges
  \coordinate (A1) at ($(A)+(1,1.5)$);
  \coordinate (B1) at ($(B)+(1.5,3.5)$);
  \coordinate (C1) at ($(C)+(2,5.5)$);
  % left outermost point of edges
  \coordinate (A2) at ($(A)+(-3,0)$);
  \coordinate (B2) at ($(B)+(-3.5,0)$);
  \coordinate (C2) at ($(C)+(-4,0)$);
  % edges
  \draw[->] (A) -| (A1) -| (A2|-e) -- (e);
  \draw     (B) -| (B1) -| (B2|-e) -- (e);
  \draw     (C) -| (C1) -| (C2|-e) -- (e);
  \foreach \x in {a,b,c,d,e} {
    \draw[->] (A2) |- (A2|-\x) -- (\x);
    \draw     (B2) |- (B2|-\x) -- (\x);
    \draw     (C2) |- (C2|-\x) -- (\x);
  }
  \foreach \x in {a,b,c,d,e} {
    \foreach \y in {A,B,C} {
      \draw[->] (\x) -- (\y);
    }
  }  
  % extra arrows
  \draw[->] (A) -- +(0.7,0);
  \draw[->] (B) -- +(1,0);
  \draw[->] (C) -- +(1.2,0);
  \draw[->] ({(1,0)}|-A1) -- +(-0.1,0);
  \draw[->] ({(1,0)}|-B1) -- +(-0.1,0);
  \draw[->] ({(1,0)}|-C1) -- +(-0.1,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows,calc}
\begin{document}
\begin{tikzpicture}[
  neuron/.style={circle,draw,minimum size=7mm},
  on grid,
  > = stealth',
  rounded corners
  ]
  % nodes
  \node[neuron] (a) {};
  \node[neuron,below=of a] (b) {};
  \node[neuron,below=of b] (c) {};
  \node[neuron,below=of c] (d) {};
  % right upper corner of edges
  \coordinate (a1) at ($(a)+(1,1)$);
  \coordinate (b1) at ($(b)+(2,3)$);
  \coordinate (c1) at ($(c)+(3,5)$);
  \coordinate (d1) at ($(d)+(4,7)$);
  % left outermost point of edges
  \coordinate (a2) at ($(a)+(-1,0)$);
  \coordinate (b2) at ($(b)+(-2,1)$);
  \coordinate (c2) at ($(c)+(-3,2)$);
  \coordinate (d2) at ($(d)+(-4,3)$);
  % extra edges
  \draw[->] (a) -| (a1) -| ([yshift=2mm]a2|-d) -- (d);
  \draw[->] (a2) |- ([yshift=2mm]a2|-b) -- (b);
  \draw[->] (a2) |- ([yshift=2mm]a2|-c) -- (c);
  \draw[->] (b) -| (b1) -| (b2|-d) -- (d);
  \draw[->] (b2) |- ([yshift=4mm]b2|-a) -- (a);
  \draw[->] (b2) |- (b2|-c) -- (c);
  \draw[->] (c) -| (c1) -| ([yshift=-6mm]c2|-d) -- (d);
  \draw[->] (c2) |- (c2|-a) -- (a);
  \draw[->] (c2) |- (c2|-b) -- (b);
  \draw[->] (d) -| (d1) -| ([yshift=-8mm]d2|-c) -- (c);
  \draw[->] (d2) |- ([yshift=-8mm]d2|-a) -- (a);
  \draw[->] (d2) |- ([yshift=-8mm]d2|-b) -- (b);
  % arrows
  \draw[->] (a) -- +(0.7,0);
  \draw[->] (b) -- +(1.2,0);
  \draw[->] (c) -- +(1.7,0);
  \draw[->] (d) -- +(2.2,0);
  \draw[->] (a|-a1) -- +(-0.1,0);
  \draw[->] (b|-b1) -- +(-0.1,0);
  \draw[->] (c|-c1) -- +(-0.1,0);
  \draw[->] (d|-d1) -- +(-0.1,0);
\end{tikzpicture}
\end{document}

相关内容