如何绘制 GRU(门控循环单元)?

如何绘制 GRU(门控循环单元)?

我试图重现 GRU 的情节,如下所示。但我找不到生成这些类似开关的按钮的方法。

enter image description here

答案1

我制作了一个自定义节点样式switch,其中有几个自定义的键需要设置。

  • open相当不言自明,布尔切换可以设置为 true 或 false,默认为true
  • closed的反义词open,因此默认为false
  • swap可设定交换闩锁的方向。

另外,我将默认label参考位置改为第一个圆。

enter image description here

代码:

\begin{tabular}{r l}
                 The default behavior without keys: & \tikz[baseline={-0.55ex}] \draw              (0,0) -- node[switch]                          ++(2,0); \\
                 When setting the switch to closed: & \tikz[baseline={-0.55ex}] \draw              (0,0) -- node[switch={closed}]                 ++(2,0); \\
    Adding a label, and explicitly set \verb|open|: & \tikz[baseline={-0.55ex}] \draw[thick]       (0,0) -- node[switch={open},label={z}]         ++(2,0); \\
                        The style also obeys color: & \tikz[baseline={-0.55ex}] \draw[red]         (0,0) -- node[switch={closed},label={below:r}] ++(2,0); \\
                            As well as line widths: & \tikz[baseline={-0.55ex}] \draw[ultra thick] (0,0) -- node[switch={open,swap},label={d}]    ++(2,0); \\
\end{tabular}

并表明它可以放置在各种(直线)路径上:

enter image description here

代码:

Example in a \verb|tikzpicture|:

\begin{tikzpicture}
    \draw (0,0) -- node[switch,label={below:q}]  ++(0,-2  )
                -- node[switch={swap}]           ++(2, 0  )
                -- node[switch={closed},label=a] ++(0, 1.5)
                -- node[switch,blue]             ++(2,-1  );
\end{tikzpicture}

笔记我通常画画黑点表示闩锁在关闭时应该去往何处,但如果出于某种原因您不想要第二个圆圈,则path picture样式的 -key 中有一行可以通过在%其前面放置 a 来忽略它。我已通过 MWE 中的注释指出了哪一行。

完成 MWE:

\documentclass[]{article}

\usepackage{tikz}

%====================================================================================
% START: COPY THIS PART TO YOUR DOCUMENT TO USE THE 'switch' STYLE
%====================================================================================
\makeatletter
\newif\ifswitch@open
\newif\ifswitch@swap

% Keys to be set by 'switch={<keys>}'
\pgfkeys{
    /switch/.cd,
        open/.is if=switch@open,
        open=true,
        closed/.code=\pgfkeysalso{open=false},
        swap/.is if=switch@swap,
}

% Switch tikz style 
\tikzset{
    switch/.style={
        % For placing
        sloped,
        allow upside down,
        midway,
        % For sizing
        inner sep = 0pt,
        minimum height = 7mm+0.5\pgflinewidth,
        minimum width = 8mm+2\pgflinewidth+2pt,
        % Node cannot contain text, so contents are set to {}
        node contents={},
        % Tricky command to adjust the default label position
        prefix after command= {
            \pgfextra{
                \tikzset{
                    every label/.style={
                        shift={(switch@label@coor)},
                    }
                }
            }
        },
        % The drawing of the switch, inside the node
        path picture={%
            \ifswitch@open
                \draw[white,line width=2\pgflinewidth] (-0.4,0) -- (0.4,0);
                \ifswitch@swap
                    \draw (-0.4,0) -- ++(-25:0.8);
                \else
                    \draw (-0.4,0) -- ++(25:0.8);
                \fi
            \fi
            \fill (-0.4,0) coordinate (switch@label@coor) circle (1pt+\pgflinewidth);
            \fill ( 0.4,0) circle (1pt+\pgflinewidth); % Place a '%' before this line to omit the second black circle
        },
    },
    % To be able to accept the keys as argument
    switch/.prefix code={\pgfkeys{/switch/.cd,#1}},
}
\makeatother
%====================================================================================
% END: COPY THIS PART TO YOUR DOCUMENT TO USE THE 'switch' STYLE
%====================================================================================

\begin{document}
    \begin{tabular}{r l}
                     The default behavior without keys: & \tikz[baseline={-0.55ex}] \draw              (0,0) -- node[switch]                          ++(2,0); \\
                     When setting the switch to closed: & \tikz[baseline={-0.55ex}] \draw              (0,0) -- node[switch={closed}]                 ++(2,0); \\
        Adding a label, and explicitly set \verb|open|: & \tikz[baseline={-0.55ex}] \draw[thick]       (0,0) -- node[switch={open},label={z}]         ++(2,0); \\
                            The style also obeys color: & \tikz[baseline={-0.55ex}] \draw[red]         (0,0) -- node[switch={closed},label={below:r}] ++(2,0); \\
                                As well as line widths: & \tikz[baseline={-0.55ex}] \draw[ultra thick] (0,0) -- node[switch={open,swap},label={d}]    ++(2,0); \\
    \end{tabular}

    Example in a \verb|tikzpicture|:

    \begin{tikzpicture}
        \draw (0,0) -- node[switch,label={below:q}]  ++(0,-2  )
                    -- node[switch={swap}]           ++(2, 0  )
                    -- node[switch={closed},label=a] ++(0, 1.5)
                    -- node[switch,blue]             ++(2,-1  );
    \end{tikzpicture}
\end{document}

边注我必须提一下,我之所以发布这个答案,是因为我已经将这种风格用于个人用途(称之为慈善)。如果你想要更好或更多的答案(将来),你应该考虑发布一个最小工作示例(MWE),并包含你已经尝试过的代码。这样,用户开始尝试你的问题的门槛就会更低。

相关内容