![使用 tikz 绘制神经网络](https://linux22.com/image/282414/%E4%BD%BF%E7%94%A8%20tikz%20%E7%BB%98%E5%88%B6%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C.png)
我正在尝试用 绘制神经网络图tikz
。我在网上找到了这段代码:
\begin{figure}[htp]
\centering
\begin{tikzpicture}[
plain/.style={
draw=none,
fill=none,
},
net/.style={
matrix of nodes,
nodes={
draw,
circle,
inner sep=8.5pt
},
nodes in empty cells,
column sep=0.6cm,
row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
|[plain]| & |[plain]| \\
& & \\
|[plain]| & |[plain]| \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
};
\foreach \ai [count=\mi ]in {2,4,...,10}
\draw[<-] (mat-\ai-1) -- node[above] {I\mi} +(-1cm,0);
\foreach \ai in {2,4,...,10}
{\foreach \aii in {3,6,9}
\draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {3,6,9}
\draw[->] (mat-\ai-2) -- (mat-6-3);
\draw[->] (mat-6-3) -- node[above] {O1} +(1cm,0);
\end{tikzpicture}
\caption{ANN diagram for Speed Sign recognition.}
\label{fig_m_3}
\end{figure}
它生成以下图像:
它有 5 个输入节点,但我想通过在每个层中都有 n 个节点来概括此图。因此,在输入层中,显示的节点数将为 4,中间有垂直的“...”,隐藏层将包含 3 个节点,中间有垂直的“...”,输出将有 2 个节点,中间有垂直的“...”。我想保持它们的圆锥形状。就像这样:
这真是一张糟糕的画,对此深表歉意,现在可以想象我的画技有多糟糕tikz
。中间的点实际上是完全填满的,在我的画中它们是空心的。任何帮助或建议都将不胜感激。
答案1
在这里,我们有一个节日\foreach
:
\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{%
every neuron/.style={
circle,
draw,
minimum size=1cm
},
neuron missing/.style={
draw=none,
scale=4,
text height=0.333cm,
execute at begin node=\color{black}$\vdots$
},
}
\begin{tikzpicture}[x=1.5cm, y=1.5cm, >=stealth]
\foreach \m/\l [count=\y] in {1,2,3,missing,4}
\node [every neuron/.try, neuron \m/.try] (input-\m) at (0,2.5-\y) {};
\foreach \m [count=\y] in {1,missing,2}
\node [every neuron/.try, neuron \m/.try ] (hidden-\m) at (2,2-\y*1.25) {};
\foreach \m [count=\y] in {1,missing,2}
\node [every neuron/.try, neuron \m/.try ] (output-\m) at (4,1.5-\y) {};
\foreach \l [count=\i] in {1,2,3,n}
\draw [<-] (input-\i) -- ++(-1,0)
node [above, midway] {$I_\l$};
\foreach \l [count=\i] in {1,n}
\node [above] at (hidden-\i.north) {$H_\l$};
\foreach \l [count=\i] in {1,n}
\draw [->] (output-\i) -- ++(1,0)
node [above, midway] {$O_\l$};
\foreach \i in {1,...,4}
\foreach \j in {1,...,2}
\draw [->] (input-\i) -- (hidden-\j);
\foreach \i in {1,...,2}
\foreach \j in {1,...,2}
\draw [->] (hidden-\i) -- (output-\j);
\foreach \l [count=\x from 0] in {Input, Hidden, Ouput}
\node [align=center, above] at (\x*2,2) {\l \\ layer};
\end{tikzpicture}
\end{document}
n
尽管当每层的节点数可能不同时,标出每层的节点数似乎是不明智的,但图表的排列表明它们并非如此。
答案2
我找到了这个包神经网络由 Mark K Cowan 制作,使绘制神经网络变得非常简单。例如:
\documentclass{standalone}
\usepackage{neuralnetwork}
\begin{document}
\begin{neuralnetwork}[height=4]
\newcommand{\x}[2]{$x_#2$}
\newcommand{\y}[2]{$\hat{y}_#2$}
\newcommand{\hfirst}[2]{\small $h^{(1)}_#2$}
\newcommand{\hsecond}[2]{\small $h^{(2)}_#2$}
\inputlayer[count=3, bias=true, title=Input\\layer, text=\x]
\hiddenlayer[count=4, bias=false, title=Hidden\\layer 1, text=\hfirst] \linklayers
\hiddenlayer[count=3, bias=false, title=Hidden\\layer 2, text=\hsecond] \linklayers
\outputlayer[count=2, title=Output\\layer, text=\y] \linklayers
\end{neuralnetwork}
\end{document}
答案3
这是一个解决方案,其中dot
将一个完全填充黑色圆圈(其大小可以通过 改变minimum size=xx <dimension>
)定义为一种样式。
代码
\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz,pgfplots,pgf}
\usetikzlibrary{matrix,shapes,arrows,positioning}
\begin{document}
\begin{figure}[htp]
\centering
\begin{tikzpicture}[
plain/.style={
draw=none,
fill=none,
},
dot/.style={draw,shape=circle,minimum size=3pt,inner sep=0,fill=black
},
net/.style={
matrix of nodes,
nodes={
draw,
circle,
inner sep=8.5pt
},
nodes in empty cells,
column sep=0.6cm,
row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer}
& |[plain]| \parbox{1cm}{\centering Hidden\\layer}
& |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& |[plain]| \\
|[plain]| & & |[plain]| \\
& |[plain]| & \\
|[plain]| & |[dot]| \\
& |[plain]| & |[dot]| \\
|[plain]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & |[dot]| \\
|[dot]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & \\
|[plain]| & & |[plain]| \\
& |[plain]| \\
};
\foreach \ai/\mi in {2/I1,4/I2,6/I3,12/In}
\draw[<-] (mat-\ai-1) -- node[above] {\mi} +(-1cm,0);
\foreach \ai in {2,4,6,12}
{\foreach \aii/\mii in {3/H1,11/Hn}
\draw[->] (mat-\ai-1) -- (mat-\aii-2) node[yshift=0.6cm] {\mii};
}
\foreach \ai in {3,11}
{ \draw[->] (mat-\ai-2) -- (mat-4-3);
\draw[->] (mat-4-3) -- node[above] {O1} +(1cm,0);}
\foreach \ai in {3,11}
{ \draw[->] (mat-\ai-2) -- (mat-10-3);
\draw[->] (mat-10-3) -- node[above] {On} +(1cm,0);}
\end{tikzpicture}
\caption{ANN diagram for Speed Sign recognition.}
\label{fig_m_3}
\end{figure}
\end{document}
答案4
我知道这是一个老问题。但我刚刚在示例关于神经网络,我认为它对未来的读者很有帮助,因为它很容易修改。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{2.5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
\tikzstyle{every pin edge}=[<-,shorten <=1pt]
\tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
\tikzstyle{input neuron}=[neuron, fill=green!50];
\tikzstyle{output neuron}=[neuron, fill=red!50];
\tikzstyle{hidden neuron}=[neuron, fill=blue!50];
\tikzstyle{annot} = [text width=4em, text centered]
% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
\node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {};
% Draw the hidden layer nodes
\foreach \name / \y in {1,...,5}
\path[yshift=0.5cm]
node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};
% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};
% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,4}
\foreach \dest in {1,...,5}
\path (I-\source) edge (H-\dest);
% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
\path (H-\source) edge (O);
% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};
\end{tikzpicture}
% End of code
\end{document}