Latex画神经网络图
2020-04-12
11 min read
BiLSTM
🥙效果图
📜代码
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle] (Y0) at (0, 0) {$\dots$};
\node[rectangle, draw, right=2em of Y0, minimum height=1cm, minimum width=1cm] (RNN) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN, draw, minimum height=1cm, minimum width=1cm] (RNN2) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN2, draw, minimum height=1cm, minimum width=1cm] (RNN3) {LSTM$_\rightarrow$};
\node[rectangle, right= of RNN3, draw, minimum height=1cm, minimum width=1cm] (RNN4) {LSTM$_\rightarrow$};
\node[rectangle, right=2em of RNN4] (RNN5) {$\dots$};
\node[rectangle, above=of RNN4, draw, minimum height=1cm, minimum width=1cm] (R25) {LSTM$_\leftarrow$};
\node[rectangle, left=of R25, minimum height=1cm, minimum width=1cm, draw] (R24) {LSTM$_\leftarrow$};
\node[rectangle, left=of R24, draw, minimum height=1cm, minimum width=1cm] (R23) {LSTM$_\leftarrow$};
\node[rectangle, left=of R23, draw, minimum height=1cm, minimum width=1cm] (R22) {LSTM$_\leftarrow$};
\node[rectangle, left=2em of R22] (R21) {$\dots$};
\node[right=2em of R25] (Y20) {$\dots$};
\node[below=of RNN] (X1) {$\vec{x}_1$};
\node[below=of RNN2] (X2) {$\vec{x}_2$};
\node[below=of RNN3] (X3) {$\vec{x}_3$};
\node[below=of RNN4] (X4) {$\vec{x}_4$};
\node[above=of R25] (Y5) {$\vec{h}_4$};
\node[above=of R24] (Y4) {$\vec{h}_3$};
\node[above=of R23] (Y3) {$\vec{h}_2$};
\node[above=of R22] (Y2) {$\vec{h}_1$};
\draw[-stealth, thick] (X1) -- (RNN);
\draw[-stealth, thick] (X2) -- (RNN2);
\draw[-stealth, thick] (X3) -- (RNN3);
\draw[-stealth, thick] (X4) -- (RNN4);
\draw[-stealth, thick, densely dotted] (Y0) -- (RNN);
\draw[-stealth, thick] (RNN) -- node[above, pos=0.35] {$\vec{h}_2^\rightarrow$} (RNN2);
\draw[-stealth, thick] (RNN2) -- node[above, pos=0.35] {$\vec{h}_3^\rightarrow$} (RNN3);
\draw[-stealth, thick] (RNN3) -- node[above, pos=0.35] {$\vec{h}_4^\rightarrow$} (RNN4);
\draw[-stealth, densely dotted, thick] (RNN4) -- (RNN5);
\node[below=4em of Y0] (d) {\dots};
\node[below=4em of RNN5] (d) {\dots};
\path[-stealth, ultra thick, white] (X1) edge[bend left=45] (R22);
\path[-stealth, thick] (X1) edge[bend left=45] (R22);
\path[-stealth, ultra thick, white] (X2) edge[bend left=45] (R23);
\path[-stealth, thick] (X2) edge[bend left=45] (R23);
\path[-stealth, ultra thick, white] (X3) edge[bend left=45] (R24);
\path[-stealth, thick] (X3) edge[bend left=45] (R24);
\path[-stealth, ultra thick, white] (X4) edge[bend left=45] (R25);
\path[-stealth, thick] (X4) edge[bend left=45] (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\draw[-stealth, thick] (R22) -- (Y2);
\draw[-stealth, thick] (R23) -- (Y3);
\draw[-stealth, thick] (R24) -- (Y4);
\draw[-stealth, thick] (R25) -- (Y5);
\draw[stealth-, densely dotted, thick] (R21) -- (R22);
\draw[stealth-, thick] (R22) -- node[above, pos=0.65] {$\vec{h}_3^\leftarrow$} (R23);
\draw[stealth-, thick] (R23) -- node[above, pos=0.65] {$\vec{h}_4^\leftarrow$} (R24);
\draw[stealth-, thick] (R24) -- node[above, pos=0.65] {$\vec{h}_5^\leftarrow$} (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\path[-stealth, ultra thick, white] (RNN) edge[bend right=45] (Y2);
\path[-stealth, thick] (RNN) edge[bend right=45] (Y2);
\path[-stealth, ultra thick, white] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, thick] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, ultra thick, white] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, thick] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, ultra thick, white] (RNN4) edge[bend right=45] (Y5);
\path[-stealth, thick] (RNN4) edge[bend right=45] (Y5);
\end{tikzpicture}
\end{document}
LSTM单元
🥙效果图
📜代码
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usepackage{bm}
\usepackage{relsize}
\usepackage{pgfplots}
\usetikzlibrary{arrows,shapes, decorations.pathmorphing,backgrounds,positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle, rounded corners=10, minimum width=20em, minimum height=12em, draw, very thick, fill=white] (lstm) at (0, 0) {};
\node[rectangle, draw] at (-2.5, -0.8) (s1){$\sigma$};
\node[rectangle, draw, right=1em of s1] (s2) {$\sigma$};
\node[rectangle, draw, right=1em of s2] (t1) {$tanh$};
\node[rectangle, draw, right=1em of t1] (s3) {$\sigma$};
\node[circle, draw, above=2em of t1, inner sep=0em] (m1) {$\otimes$};
\node[circle, draw, above=6em of s1, inner sep=0em] (m2) {$\otimes$};
\node[circle, draw, right=6.55em of m2, inner sep=0em] (p1) {$\oplus$};
\node[circle, draw, right=4.5em of m1, inner sep=0em] (m3) {$\otimes$};
\node[rectangle, draw, above=1em of m3, inner sep=0.2em] (tt) {$tanh$ };
\node[circle, draw, below=1em of s1, inner sep=0em] (conc) {$||$};
\node[below=5em of s1] (xt) {$\vec{x}_t$};
\node[left=3em of conc] (ht1) {$\vec{h}_{t-1}$};
\node[left=3em of m2] (ct1) {$c_{t-1}$};
\node[right=18em of m2] (ct) {$c_t$};
\node[right=18em of conc] (ht) {$\vec{h}_t$};
\node[] (yt) at (3, 3) {$\vec{h}_t$};
\draw[-stealth, line width=1mm, white] (xt) -- (conc);
\draw[-stealth, very thick] (xt) -- (conc);
\draw[-stealth, line width=1mm, white] (ht1) -- (conc);
\draw[-stealth, very thick] (ht1) -- (conc);
\draw[-stealth, very thick] (conc) -- (s1);
\path[-stealth, very thick] (conc) edge[bend right] (s2.south);
\path[-stealth, very thick] (conc) edge[bend right] (t1.south);
\path[-stealth, very thick] (conc) edge[bend right] (s3.south);
\draw[-stealth, very thick] (s1) -- node[left] {$f_t$} (m2);
\draw[-stealth, very thick] (s2) edge[bend left] node[above] {$i_t$} (m1.west);
\draw[-stealth, very thick] (t1) -- node[right] {$\tilde{c_t}$} (m1);
\draw[-stealth, very thick] (m1) -- (p1);
\draw[-stealth, line width=1mm, white] (ct1) -- (m2);
\draw[-stealth, very thick] (ct1) -- (m2);
\draw[-stealth, very thick] (m2) -- (p1);
\draw[-stealth, very thick] (s3) edge[bend left] node[left] {$o_t$} (m3.west);
\draw[-stealth, line width=1mm, white] (p1) -- (ct);
\draw[-stealth, very thick] (p1) -- (ct);
\draw[-stealth, very thick] (tt) -- (m3);
\draw[-stealth, line width=1mm, white] (m3) edge[bend right] (ht.west);
\draw[-stealth, very thick] (m3) edge[bend right] (ht.west);
\draw[-stealth ,very thick] (p1) edge[bend right] (tt.west);
\draw[-stealth, line width=1mm,white] (m3) edge[bend right] (yt.south);
\draw[-stealth, very thick] (m3) edge[bend right] (yt.south);
\end{tikzpicture}
\end{document}
自注意力
🥙效果图
📜代码
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node (X1) {$\vec{h}_{1}$};
\node[rectangle, right= 0.5em of X1] (x_dots_1) {$\dots$};
\node[right=0.5em of x_dots_1] (Xj) {$\vec{h}_{j}$};
\node[rectangle, right= 1em of Xj] (x_dots_2) {$\dots$};
\node[right=1em of x_dots_2] (Xn) {$\vec{h}_{n}$};
\node[rectangle, draw, ultra thick, above=of X1] (attn1) {\large $a_\phi$};
\node[rectangle, draw, ultra thick, above=of Xj] (attnj) {\large $a_\phi$};
\node[rectangle, draw, ultra thick, above=of Xn] (attnn) {\large $a_\phi$};
\draw[-stealth, thick] (X1) -- (attn1);
\draw[-stealth, thick] (Xj) -- (attn1);
\draw[-stealth, thick] (Xj) -- (attnj);
\draw[-stealth, thick] ([xshift=3em]Xj) -- (attnj);
\draw[-stealth, thick] (Xj) -- (attnn);
\draw[-stealth, thick] (Xn) -- (attnn);
\node[above= of attn1, opacity=0.2] (alpha1j) {$\alpha_{1,j}$};
\node[above= of attnj, opacity=1] (alphajj) {$\alpha_{j,j}$};
\node[above= of attnn, opacity=0.6] (alphanj) {$\alpha_{n,j}$};
\node[circle, draw, above=of alpha1j] (times1) {$\times$};
\node[circle, draw, above=of alphajj] (timesj) {$\times$};
\node[circle, draw, above=of alphanj] (timesn) {$\times$};
\node[rectangle, draw, above=of timesj] (sum) {$\Sigma$};
\node[above=1em of sum] (x_tprim) {$\vec{h}_j'$};
\draw[-stealth, line width=1.5mm, white] (attn1) -- (alpha1j);
\draw[-stealth, thick, opacity=0.2] (attn1) -- (alpha1j);
\draw[-stealth, line width=1.5mm, white] (attnj) -- (alphajj);
\draw[-stealth, thick, opacity=1] (attnj) -- (alphajj);
\draw[-stealth, line width=1.5mm, white] (attnn) -- (alphanj);
\draw[-stealth, thick, opacity=0.6] (attnn) -- (alphanj);
\draw[-stealth, white, line width=1.5mm] (X1) edge[bend right=30] (times1);
\draw[-stealth, thick] (X1) edge[bend right=30] node[rectangle, draw, fill=white, midway] {$f_\psi$} (times1);
\draw[-stealth, white, line width=1.5mm] (Xj) edge[bend right=30] (timesj);
\draw[-stealth, thick] (Xj) edge[bend right=30] node[rectangle, draw, fill=white, midway] {$f_\psi$} (timesj);
\draw[-stealth, thick] (Xn) edge[bend right=30] node[rectangle, draw, fill=white, midway] {$f_\psi$} (timesn);
\draw[-, line width=1.5mm, white] (times1) -- (sum);
\draw[-stealth, thick] (times1) -- (sum);
\draw[-, line width=1.5mm, white] (timesj) -- (sum);
\draw[-stealth, thick] (timesj) -- (sum);
\draw[-stealth, thick] (timesn) -- (sum);
\draw[-stealth, thick] (times1) -- (sum);
\draw[-stealth, line width=1.5mm, white] (alpha1j) -- (times1);
\draw[-stealth, thick, opacity=0.2] (alpha1j) -- (times1);
\draw[-stealth, line width=1.5mm, white] (alphajj) -- (timesj);
\draw[-stealth, thick, opacity=1] (alphajj) -- (timesj);
\draw[-stealth, line width=1.5mm, white] (alphanj) -- (timesn);
\draw[-stealth, thick, opacity=0.6] (alphanj) -- (timesn);
\draw[-stealth, thick] (sum) -- (x_tprim);
\end{tikzpicture}
\end{document}
RNN
🥙效果图
📜代码
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node[rectangle] (Y0) at (0, 0) {$\dots$};
\node[rectangle, draw, right=2em of Y0, minimum height=1cm, minimum width=1cm] (RNN) {RNN};
\node[rectangle, right=of RNN, draw, minimum height=1cm, minimum width=1cm] (RNN2) {RNN};
\node[rectangle, right=of RNN2, draw, minimum height=1cm, minimum width=1cm] (RNN3) {RNN};
\node[rectangle, right= of RNN3, draw, minimum height=1cm, minimum width=1cm] (RNN4) {RNN};
\node[rectangle, right=2em of RNN4] (RNN5) {$\dots$};
\node[below=of RNN] (X1) {$\vec{x}_1$};
\node[below=of RNN2] (X2) {$\vec{x}_2$};
\node[below=of RNN3] (X3) {$\vec{x}_3$};
\node[below=of RNN4] (X4) {$\vec{x}_4$};
\node[above=of RNN4] (Y5) {$\vec{h}_4$};
\node[above=of RNN3] (Y4) {$\vec{h}_3$};
\node[above=of RNN2] (Y3) {$\vec{h}_2$};
\node[above=of RNN] (Y2) {$\vec{h}_1$};
\draw[-stealth, thick] (X1) -- (RNN);
\draw[-stealth, thick] (X2) -- (RNN2);
\draw[-stealth, thick] (X3) -- (RNN3);
\draw[-stealth, thick] (X4) -- (RNN4);
\draw[-stealth, thick] (RNN) -- (Y2);
\draw[-stealth, thick] (RNN2) -- (Y3);
\draw[-stealth, thick] (RNN3) -- (Y4);
\draw[-stealth, thick] (RNN4) -- (Y5);
\draw[-stealth, densely dotted, thick] (Y0) -- (RNN);
\draw[-stealth, densely dotted, thick] (RNN) -- (RNN2);
\draw[-stealth, densely dotted, thick] (RNN2) -- (RNN3);
\draw[-stealth, densely dotted, thick] (RNN3) -- (RNN4);
\draw[-stealth, densely dotted, thick] (RNN4) -- (RNN5);
\end{tikzpicture}
\end{document}