绘制 RNN 方案

绘制 RNN 方案

在此处输入图片描述

有人能帮我在乳胶中实现这个方案吗? 有可能吗?

到目前为止我尝试过这个:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, bending, chains, graphs, positioning}

\begin{document}

\begin{figure}[ht]
    \centering
    % First figure: Time stamp = 1.0
    \begin{minipage}{.32\textwidth}
        \centering
        \begin{tikzpicture}[nn width/.initial=.75cm,
  every nn layer label/.style={
    node font=\footnotesize, label position=above, anchor=north,
    inner xsep=+.15em, align=center,
    text width={max(width("Output"),\pgfkeysvalueof{/tikz/nn width}}},
  every nn node/.style={
    shape=circle, draw, solid,
    minimum size=\pgfkeysvalueof{/tikz/nn width}},
  every nn layer/.style={draw, node distance=1.25em}, % distance between circles
  nn layer/.style args={#1:#2}{
    matrix, every nn layer,
    execute at empty cell={
      \tikzset{new set=\tikzmatrixname,
        start chain={\tikzmatrixname} going below}%
      \node foreach \nncount in {1, ..., #1}[%
        on chain, set=\tikzmatrixname, every nn node, nn node \nncount/.try]{};
      \node[every nn layer label, above=1em of \tikzmatrixname-begin]{#2};},
    node contents=\\},
  nn layer'/.style={densely dashed, nn layer={#1}}][
  >={Latex[round, scale=.7]},
  every curve to/.style={distance=7mm},
  dd/.style=densely dotted,
  node distance=.5em and 1cm, % this controls the distance between layers
]
\node (inp) [                          nn layer'=2:Input  Layer];
\node (buf) [below=of inp,             nn layer =3:Buffer      ];
\node (hid) [right=of buf, yshift=3em, nn layer'=3:Hidden Layer];
\node (out) [right=of hid,             nn layer'=1:Output Layer];

\graph {
  {(inp), (buf)} ->[complete bipartite,     out=  0, in=180] (hid)
                 ->[complete bipartite, dd                 ] (out),
  (hid)          ->[                    dd, out=-90, in= 90] (buf)
};

% Time stamp label below the buffer
\node[below=of buf, label distance=2em] {Time stamp = 1.0};

            % (Include your complete TikZ code for the first figure here)
        \end{tikzpicture}
    \end{minipage}\hfill
    % Second figure: Time stamp = 1.5
    \begin{minipage}{.32\textwidth}
        \centering
        \begin{tikzpicture}[nn width/.initial=.75cm,
  every nn layer label/.style={
    node font=\footnotesize, label position=above, anchor=north,
    inner xsep=+.15em, align=center,
    text width={max(width("Output"),\pgfkeysvalueof{/tikz/nn width})}},
  every nn node/.style={
    shape=circle, draw, solid,
    minimum size=\pgfkeysvalueof{/tikz/nn width}},
  every nn layer/.style={draw, node distance=1.25em}, % distance between circles
  nn layer/.style args={#1:#2}{
    matrix, every nn layer,
    execute at empty cell={
      \tikzset{new set=\tikzmatrixname,
        start chain={\tikzmatrixname} going below}%
      \node foreach \nncount in {1, ..., #1}[%
        on chain, set=\tikzmatrixname, every nn node, nn node \nncount/.try]{};
      \node[every nn layer label, above=1em of \tikzmatrixname-begin]{#2};},
    node contents=\\},
  nn layer'/.style={densely dashed, nn layer={#1}}][
  >={Latex[round, scale=.7]},
  every curve to/.style={distance=7mm},
  dd/.style=densely dashed,
  node distance=.5em and 1cm, % this controls the distance between layers
]
\node (inp) [                          nn layer'=2:Input  Layer];
\node (buf) [below=of inp,             nn layer =3:Buffer      ];
\node (hid) [right=of buf, yshift=3em, nn layer'=3:Hidden Layer];
\node (out) [right=of hid,             nn layer'=1:Output Layer];

\graph {
  {(inp), (buf)} ->[complete bipartite, dd, out=  0, in=180] (hid)
                 ->[complete bipartite,                      ] (out),
  (hid)          ->[                    out=-90, in= 90] (buf)
};

% Time stamp label below the buffer
\node[below=of buf, label distance=2em] {Time stamp = 1.5};

            % (Include your complete TikZ code for the second figure here)
        \end{tikzpicture}
    \end{minipage}\hfill
    % Third figure: Time stamp = 2.0
    \begin{minipage}{.32\textwidth}
        \centering
        \begin{tikzpicture}[nn width/.initial=.75cm,
  every nn layer label/.style={
    node font=\footnotesize, label position=above, anchor=north,
    inner xsep=+.15em, align=center,
    text width={max(width("Output"),\pgfkeysvalueof{/tikz/nn width}}},
  every nn node/.style={
    shape=circle, draw, solid,
    minimum size=\pgfkeysvalueof{/tikz/nn width}},
  every nn layer/.style={draw, node distance=1.25em}, % distance between circles
  nn layer/.style args={#1:#2}{
    matrix, every nn layer,
    execute at empty cell={
      \tikzset{new set=\tikzmatrixname,
        start chain={\tikzmatrixname} going below}%
      \node foreach \nncount in {1, ..., #1}[%
        on chain, set=\tikzmatrixname, every nn node, nn node \nncount/.try]{};
      \node[every nn layer label, above=1em of \tikzmatrixname-begin]{#2};},
    node contents=\\},
  nn layer'/.style={densely dashed, nn layer={#1}}][
  >={Latex[round, scale=.7]},
  every curve to/.style={distance=7mm},
  dd/.style=densely dotted,
  node distance=.5em and 1cm, % this controls the distance between layers
]
\node (inp) [                          nn layer'=2:Input  Layer];
\node (buf) [below=of inp,             nn layer =3:Buffer      ];
\node (hid) [right=of buf, yshift=3em, nn layer'=3:Hidden Layer];
\node (out) [right=of hid,             nn layer'=1:Output Layer];

\graph {
  {(inp), (buf)} ->[complete bipartite,     out=  0, in=180] (hid)
                 ->[complete bipartite, dd                 ] (out),
  (hid)          ->[                    dd, out=-90, in= 90] (buf)
};
a
% Time stamp label below the buffer
\node[below=of buf, label distance=2em] {Time stamp = 1.0};

            % (Include your complete TikZ code for the third figure here)
        \end{tikzpicture}
    \end{minipage}
    \caption{RNN at different time stamps.}
    \label{fig:rnn_time_stamps}
\end{figure}

\end{document}

但我把它们放在一起却得到了一个糟糕的结果: 在此处输入图片描述

我对此很陌生

答案1

受到我自己的回答的启发绘制神经网络每一层都是一个只有一个单元的矩阵,层内的节点放在链上,节点通过graphs库的语法连接,因为这样可以更容易地连接一堆节点。

为此,每个矩阵都会建立一个以矩阵命名的集合,矩阵内的每个节点都会放入该集合中。在操作中,graph可以使用括号中的名称来引用集合中的所有节点(就像它是一个节点一样)。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, bending, chains, graphs, positioning}
\tikzset{
  nn width/.initial=.75cm,
  every nn layer label/.style={
    node font=\footnotesize, label position=above, anchor=north,
    inner xsep=+.15em, align=center,
    text width={max(width("Output"),\pgfkeysvalueof{/tikz/nn width}}},
  every nn node/.style={
    shape=circle, draw, solid,
    minimum size=\pgfkeysvalueof{/tikz/nn width}},
  every nn layer/.style={draw, node distance=1.25em}, % distance between circles
  nn layer/.style args={#1:#2}{
    matrix, every nn layer,
    execute at empty cell={
      \tikzset{new set=\tikzmatrixname,
        start chain={\tikzmatrixname} going below}%
      \node foreach \nncount in {1, ..., #1}[%
        on chain, set=\tikzmatrixname, every nn node, nn node \nncount/.try]{};
      \node[every nn layer label, above=1em of \tikzmatrixname-begin]{#2};},
    node contents=\\},
  nn layer'/.style={densely dashed, nn layer={#1}}}
\begin{document}
\begin{tikzpicture}[
  >={Latex[round, scale=.7]},
  every curve to/.style={distance=7mm},
  dd/.style=densely dotted,
  node distance=.5em and 1cm, % this controls the distance between layers
]
\node (inp) [                          nn layer'=2:Input  Layer];
\node (buf) [below=of inp,             nn layer =3:Buffer      ];
\node (hid) [right=of buf, yshift=3em, nn layer'=3:Hidden Layer];
\node (out) [right=of hid,             nn layer'=1:Output Layer];

\graph {
  {(inp), (buf)} ->[complete bipartite,     out=  0, in=180] (hid)
                 ->[complete bipartite, dd                 ] (out),
  (hid)          ->[                    dd, out=-90, in= 90] (buf)
};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

答案2

例如,您可以使用matrix of nodes布局基础知识并使用fit库绘制图层的周长。bending库可以帮助绘制曲线上的箭头,并arrows.meta提供箭头提示的选择。

下面的示例演示了两种绘制曲线的方法。(还有更多!)

示例图

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,arrows.meta,bending}
% ateb: https://tex.stackexchange.com/a/701679/ i gwestiwn bdzh: https://tex.stackexchange.com/q/701674/
\begin{document}
\begin{tikzpicture}
  [
    nnode/.style={draw,rounded corners,text width=10mm,text height=10mm,anchor=south},
    lnode/.style={text width=10mm,align=center,anchor=south,inner sep=0pt},
    lfit/.style={draw,dashed,fit=#1},
  ]
  \matrix (mat) [matrix of nodes,nodes in empty cells,row sep=5mm,column sep=20mm]
  {
    |[lnode]| {Input\\Layer} & &  \\
    |[nnode]| & &  \\
    |[nnode]| &  |[lnode]| {Hidden\\Layer} &  \\
    |[lnode]| Buffer  &  |[nnode]| & |[lnode]| {Output\\Layer} \\
    |[nnode]|  & |[nnode]| &  |[nnode]| \\
    |[nnode]|  & |[nnode]| &  \\
    |[nnode]|  & &  \\    
   };
   \node (ol) [lfit=(mat-4-3) (mat-5-3)] {};
   \node (hl) [lfit=(mat-3-2) (mat-4-2) (mat-5-2) (mat-6-2)] {};
   \node (il) [lfit=(mat-1-1) (mat-2-1) (mat-3-1)] {};
   \node (bf) [lfit=(mat-4-1) (mat-5-1) (mat-6-1) (mat-7-1), solid] {};
   \foreach \i/\k/\m in {2/0/120,3/0/140,5/0/180,6/0/200,7/0/235}
    \foreach \j in {4,5,6}
      \draw [-{Latex[bend]}] (mat-\i-1) [out=\k,in=\m] to (mat-\j-2.west);
   \foreach \i in {4,5,6} \draw [-{Latex},dashed] (mat-\i-2.0) -- (mat-5-3.west);
   \foreach \i/\j in {4/5,5/6,6/7} \draw [-{Latex[bend]},dashed] (mat-\i-2.south) .. controls +(-1,-1) and +(1,1) .. (mat-\j-1.north);
\end{tikzpicture}

\end{document}

相关内容