查看原文
其他

TikZ 绘制神经网络示例

LaTeX工作室 2024-03-18

今天分享几个 LaTeX 绘制神经网络图的案例(案列出处:Understanding LSTM Networks),我们来看第一个案例图是如何实现的。

Figure 1. pooling_max and pooling_average

首先,我们给出绘图代码运行的环境,由于篇幅不宜过长,完整的环境代码我放在最后,需要的可以在最后复制使用。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{ifthen}
\usepackage{amsmath}
\DeclareMathOperator{\sigm}{sigm}
\newcommand{\diff}{\mathop{}\!\mathrm{d}}
\begin{document}
   % your code
\end{document}
图1 神经网络单元

上面图1的绘图代码如下:

\begin{tikzpicture}[]
\def\pindist{35pt}
\def\nodesize{38pt}
\tikzstyle{every pin edge}=[signal]
\tikzstyle{annot} = [text width=4em, text centered]
\node[hiddennode, text width=\nodesize, minimum size=\nodesize,
pin={[pin edge={latex-}, pin distance=\pindist]above left:$x_1$},
pin={[pin edge={latex-}, pin distance=\pindist]below left:$x_2$},
pin={[pin edge={-latex}, pin distance=\pindist]right:$y_1$}
] (N1) at (-100pt,0) {$f(x)$};
\node[hiddennode, text width=\nodesize, minimum size=\nodesize,
pin={[pin edge={-latex}, pin distance=\pindist]above left:$\frac{\partial L}{\partial x_1}=\frac{\partial L}{\partial y_1}\frac{\partial y_1}{\partial x_1}$},
pin={[pin edge={-latex}, pin distance=\pindist]below left:$\frac{\partial L}{\partial x_2}=\frac{\partial L}{\partial y_1}\frac{\partial y_1}{\partial x_2}$},
pin={[pin edge={latex-}, pin distance=\pindist]right:$\frac{\partial L}{\partial y_1}$}
] (N2) at (+120pt,0) {$\diff f$};
\node[annot, text width=200pt, align=center, above=40pt of N1] (l1) {Forwardpass};
\node[annot, text width=200pt, align=center, above=40pt of N2] (l2) {Backwardpass};
\draw[signal, -] (0,-70pt) -- (0,+80pt);
\end{tikzpicture}

接下来就是图2绘图代码

图2 神经网络
\begin{tikzpicture}[]
\def\nodedist{35pt}
\def\layerdist{80pt}
\def\pindist{20pt}
\tikzstyle{every pin edge}=[signal]
\tikzstyle{annot} = [text width=4em, text centered]
\foreach \y in {1,...,3}
\node[inputnode, pin={[pin edge={latex-}, pin distance=\pindist]left:Input \y}]
(I\y) at (0,-\y*\nodedist) {$x_\y$};
\foreach \y in {1,...,4}
\node[hiddennode]
(H\y) at ($(\layerdist,-\y*\nodedist) +(0, 0.5*\nodedist)$) {$z_\y$};
\foreach \y in {1,...,1}
\node[outputnode, pin={[pin edge={-latex}, pin distance=\pindist]right:Output \y}]
(O\y) at ($(I2) + (2*\layerdist, 0)$) {$y_\y$};
\foreach \dest in {1,...,4}
\foreach \source in {1,...,3}
\draw[signal] (I\source) -- (H\dest);
\foreach \dest in {1,...,1}
\foreach \source in {1,...,4}
\draw[signal] (H\source) edge (O\dest);
\node[annot, above=4pt of H1] (hl) {Hidden layer};
\node[annot] at (I1 |- hl) {Input layer};
\node[annot] at (O1 |- hl) {Output layer};
\end{tikzpicture}

案例3 图神经网络及代码:

图3 图神经网络
\begin{tikzpicture}[]
\def\nodedist{35pt}
\def\layerdist{80pt}
\def\pindist{20pt}
\tikzstyle{every pin edge}=[signal]
\tikzstyle{annot} = [text width=4em, text centered]
\foreach \y in {1,...,3}
\node[inputnode, pin={[pin edge={latex-}, pin distance=\pindist]left:Input \y}]
(I\y) at (0,-\y*\nodedist) {$x_\y$};
\foreach \y in {1,...,4}
\node[hiddennode]
(H1\y) at ($(\layerdist,-\y*\nodedist) +(0, 0.5*\nodedist)$) {$z_\y^1$};
\foreach \y in {1,...,4}
\node[hiddennode]
(H2\y) at ($(2*\layerdist,-\y*\nodedist) +(0, 0.5*\nodedist)$) {$z_\y^2$};
\foreach \y in {1,...,2}
\node[outputnode, pin={[pin edge={-latex}, pin distance=\pindist]right:Output \y}]
(O\y) at ($(H21) + (\layerdist, -\y*\nodedist)$) {$y_\y$};
\foreach \dest in {1,...,4}
\foreach \source in {1,...,3}
\draw[signal] (I\source) -- (H1\dest);
\foreach \dest in {1,...,4}
\foreach \source in {1,...,4}
\draw[signal] (H1\source) -- (H2\dest);
\foreach \dest in {1,...,2}
\foreach \source in {1,...,4}
\draw[signal] (H2\source) edge (O\dest);
\node[annot, above=4pt of H11] (hl) {Hidden layer 1};
\node[annot, above=4pt of H21] (hl) {Hidden layer 2};
\node[annot] at (I1 |- hl) {Input layer};
\node[annot] at (O1 |- hl) {Output layer};
\end{tikzpicture}

案例4 图神经网络及代码:

案例4 图神经网络

案例5 图神经网络代码:

案例5 图神经网络
案例6 图神经网络

最后我们给出下图及完整的代码:

img
\documentclass[
tikz,
border=3mm
]{standalone}

\usepackage{pgfplots}
\usepackage{ifthen}
\usepackage{amsmath}
\DeclareMathOperator{\sigm}{sigm}
\newcommand{\diff}{\mathop{}\!\mathrm{d}}
usetikzlibrary{shapes,arrows,positioning,calc,chains,scopes}
\definecolor{snowymint}{HTML}{E3F8D1}
\definecolor{wepeep}{HTML}{FAD2D2}
\definecolor{portafino}{HTML}{F5EE9D}
\definecolor{plum}{HTML}{DCACEF}
\definecolor{sail}{HTML}{A3CEEE}
\definecolor{highland}{HTML}{6D885A}

\tikzstyle{signal}=[arrows={-latex},draw=black,line width=1.5pt,rounded corners=4pt]

% RNN
\tikzstyle{block}=[draw=black,line width=1.0pt]
\tikzstyle{cell}=[style=block,draw=highland,fill=snowymint,
    rounded corners]
\tikzstyle{celllayer}=[style=block,draw,fill=portafino,
    inner sep=1pt,outer sep=0,
    minimum width=28pt, minimum height=14pt]
\tikzstyle{pointwise}=[style=block,ellipse,fill=wepeep,
    inner sep=1pt,outer sep=0, minimum size=12pt]

\begin{document}

\def\iolen{24pt}
\def\intergape{2pt}

% MLP and CNN
\tikzstyle{netnode}=[circle, inner sep=0pt, text width=22pt, align=center, line width=1.0pt]
\tikzstyle{inputnode}=[netnode, fill=sail,draw=black]
\tikzstyle{hiddennode}=[netnode, fill=snowymint,draw=black]
\tikzstyle{outputnode}=[netnode, fill=plum,draw=black]

% Architecture
\def\layerwidth{90pt}
\def\layerheight{14pt}

\tikzstyle{layer}=[style=block, draw, fill=black!20!white,
    inner sep=1pt,outer sep=0, font=\footnotesize,
    text centered, 
    minimum width=\layerwidth, minimum height=\layerheight]

\tikzstyle{fc}=[style=layer, fill=blue!30!white]
\tikzstyle{conv}=[style=layer, fill=green!30!white]
\tikzstyle{activation}=[style=layer, fill=orange!30!white]
\tikzstyle{pool}=[style=layer, fill=red!30!white]
\tikzstyle{bn}=[style=layer, fill=cyan!30!white]
\tikzstyle{recurrent}=[style=layer, fill=purple!30!white]
\tikzstyle{softmax}=[style=layer, fill=yellow!30!white]
\tikzstyle{point}=[]
\tikzstyle{branch}=[coordinate]

\def\vlayerwidth{30pt}
\def\vlayerheight{3pt}
\def\vblockheight{28pt}

\tikzstyle{vlayer}=[minimum width=\vlayerwidth, minimum height=\vlayerheight]
\tikzstyle{vblock}=[minimum width=\vlayerwidth, minimum height=\vblockheight, text width=1cm, align=center]


% Precision, Recall
\colorlet{fn}{gray!90!green!30!white}
\colorlet{tp}{green!40!white}
\colorlet{fp}{red!40!white}
\colorlet{tn}{gray!90!red!20!white} 

\begin{tikzpicture}
\node at (-5.5,0){\begin{tikzpicture}
    \coordinate (p) at (0,0);
    \draw[
        ultra thick,
        step=1, 
        color=black,
        draw=black,
        fill=black!20!white,
        shift={(p)}
    ]
 (0,0) grid (5,-5) rectangle (0,0)
    foreach[count=~] \l in {90, 15, 19, 36, 85, 13, 55, 31, 42, 23, 41, 11, 11, 57, 58, 67, 89, 87, 83, 49,  2, 11, 19, 32, 91} {
        ({0.5+mod(~-1,5}, {-0.5-div(~-1,5}) node {\Large \l}
    };
    \draw[shift={(p)}, draw=red, ultra thick] (0,0) rectangle (3,-3);
    
    \coordinate (p) at (6.5,-1.5);
    \draw[
        ultra thick,
        step=1, 
        color=black,
        draw=black,
        fill=black!20!white,
        shift={(p)}
    ]
 (0,0) grid (2,-2) rectangle (0,0)
    foreach[count=~] \l in {90, 85, 89, 91} {
        ({0.5+mod(~-1,2}, {-0.5-div(~-1,2}) node {\Large \l}
    };
    \draw[shift={(p)}, draw=red, ultra thick] (0,0) rectangle (1,-1);
    % max     {90, 85, 89, 91}
    % average {32, 40, 38, 54}
\end{tikzpicture}
};
\node at (5.5,0){\begin{tikzpicture}
    \coordinate (p) at (0,0);
    \draw[shift={(p)}, fill=red!20!white] (0,0) rectangle (2,-2);
    \draw[shift={(p)}, fill=yellow!20!white] (2,0) rectangle (4,-2);
    \draw[shift={(p)}, fill=blue!20!white] (0,-2) rectangle (2,-4);
    \draw[shift={(p)}, fill=green!20!white] (2,-2) rectangle (4,-4);
    \draw[
        ultra thick,
        step=1, 
        color=black,
        draw=black,
        fill=black!20!white,
        shift={(p)}
    ]
 (0,0) grid (4,-4)
    foreach[count=~] \l in {73, 74, 17, 49, 10, 29, 41, 20, 4, 23, 39, 4, 50, 80, 56, 57} {
        ({0.5+mod(~-1,4}, {-0.5-div(~-1,4}) node {\Large \l}
    };
    
    % max pooling
    \coordinate (p) at (7,0.5);
    \draw[shift={(p)}, fill=red!20!white] (0,0) rectangle (1,-1);
    \draw[shift={(p)}, fill=yellow!20!white] (1,0) rectangle (2,-1);
    \draw[shift={(p)}, fill=blue!20!white] (0,-1) rectangle (1,-2);
    \draw[shift={(p)}, fill=green!20!white] (1,-1) rectangle (2,-2);
    \draw[
        ultra thick,
        step=1, 
        color=black,
        draw=black,
        fill=black!20!white,
        shift={(p)}
    ]
 (0,0) grid (2,-2)
    foreach[count=~] \l in {74, 49, 80, 57} {
        ({0.5+mod(~-1,2}, {-0.5-div(~-1,2}) node {\Large \l}
    };
    \draw[signal] (4.5,-1.5) -> ($(p) +(-0.5, -1.25)$);
    \node[text width=200pt, align=center, right of=p, yshift=10pt] (l1) {Max Pooling};
    
    % average pooling
    \coordinate (p) at (7,-2.5);
    \draw[shift={(p)}, fill=red!20!white] (0,0) rectangle (1,-1);
    \draw[shift={(p)}, fill=yellow!20!white] (1,0) rectangle (2,-1);
    \draw[shift={(p)}, fill=blue!20!white] (0,-1) rectangle (1,-2);
    \draw[shift={(p)}, fill=green!20!white] (1,-1) rectangle (2,-2);
    \draw[
        ultra thick,
        step=1, 
        color=black,
        draw=black,
        fill=black!20!white,
        shift={(p)}
    ]
 (0,0) grid (2,-2)
    foreach[count=~] \l in {46, 32, 39, 39} {
        ({0.5+mod(~-1,2}, {-0.5-div(~-1,2}) node {\Large \l}
    };
    \draw[signal] (4.5,-2.5) -> ($(p) +(-0.5, -0.75)$);
    \node[text width=200pt, align=center, right of=p, yshift=10pt] (l1) {Average Pooling};
\end{tikzpicture} 
};
\end{tikzpicture}
\end{document}

完整的代码块资源链接:

http://colah.github.io/posts/2015-08-Understanding-LSTMs

更新完毕。


继续滑动看下一个
向上滑动看下一个

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存