使用latex绘制多层神经网络结构图
1,使用Tikz包;
2,参考官方例程单层神经网络结构,绘制了一个含有3隐藏层的BP神经网络节点图
代码如下:
1 \documentclass{article} 2 3 \usepackage{tikz} 4 \begin{document} 5 \pagestyle{empty} 6 7 \def\layersep{2.5cm} 8 9 \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep] 10 \tikzstyle{every pin edge}=[<-,shorten <=1pt] 11 \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt] 12 \tikzstyle{input neuron}=[neuron, fill=green!50]; 13 \tikzstyle{output neuron}=[neuron, fill=red!50]; 14 15 \tikzstyle{hidden neuron1}=[neuron, fill=blue!50]; 16 \tikzstyle{hidden neuron2}=[neuron, fill=blue!50]; 17 \tikzstyle{hidden neuron3}=[neuron, fill=blue!50]; 18 19 \tikzstyle{annot} = [text width=4em, text centered] 20 21 % Draw the input layer nodes 22 \foreach \name / \y in {1,...,4} 23 % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4} 24 \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {}; 25 26 % Draw the hidden layer1 nodes 27 \foreach \name / \y in {1,...,5} 28 \path[yshift=0.5cm] 29 node[hidden neuron1] (H1-\name) at (\layersep,-\y cm) {}; 30 %Draw the hidden layer2 nodes 31 \foreach \name / \y in {1,...,5} 32 \path[yshift=0.5cm] 33 node[hidden neuron2,right of=H1] (H2-\name) at (\layersep,-\y cm){}; 34 %Draw the hidden layer3 nodes 35 \foreach \name / \y in {1,...,5} 36 \path[yshift=0.5cm] 37 node[hidden neuron3,right of=H2] (H3-\name) at (2*\layersep,-\y cm){}; 38 39 % Draw the output layer node 40 \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H3-3] (O) {}; 41 42 % Connect every node in the input layer with every node in the 43 % hidden layer. 44 \foreach \source in {1,...,4} 45 \foreach \dest in {1,...,5} 46 \path (I-\source) edge (H1-\dest); 47 % 48 \foreach \source in {1,...,5} 49 \foreach \dest in {1,...,5} 50 \path (H1-\source) edge (H2-\dest); 51 % 52 \foreach \source in {1,...,5} 53 \foreach \dest in {1,...,5} 54 \path (H2-\source) edge (H3-\dest); 55 % Connect every node in the hidden layer with the output layer 56 \foreach \source in {1,...,5} 57 \path (H3-\source) edge (O); 58 59 % Annotate the layers 60 \node[annot,above of=H1-1, node distance=1cm] (hl) {Hidden layer1}; 61 \node[annot,left of=hl] {Input layer}; 62 \node[annot,right of=hl] (h2){Hidden layer2}; 63 \node[annot,right of=h2] (h3){Hidden layer3}; 64 \node[annot,right of=h3] {Output layer}; 65 \end{tikzpicture} 66 % End of code 67 \end{document}
效果如图: