我想使用 tikz 创建 FNN 和 DNN 图形,我在互联网上找到了这个 FNN 示例并且我很喜欢它:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usetikzlibrary{positioning,calc}
\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{figure}
\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.5cm,
row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer}
& |[plain]| \parbox{1cm}{\centering Hidden\\layer}
& |[plain]| \parbox{1cm}{\centering ...}
& |[plain]| \parbox{2cm}{\centering Hidden\\layer n}
& |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& |[plain]| & |[plain]| & |[plain]| & |[plain]| \\
|[plain]| & & |[plain]| & & |[plain]| \\
& |[plain]| & |[plain]| & |[plain]| & \\
|[plain]| & |[dot]| & |[plain]| & |[dot]| \\
& |[plain]| & |[plain]| & |[plain]| & |[dot]| \\
|[plain]| & |[dot]| & |[plain]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & |[plain]| & |[plain]| & |[dot]| \\
|[dot]| & |[dot]| & |[plain]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & |[plain]| & |[plain]| & \\
|[plain]| & & |[plain]| & & |[plain]| \\
& |[plain]| & |[plain]| \\
};
\foreach \ai/\mi in {2/Input feature 1,4/Input feature 2,6/Input feature 3,12/Input feature n}
\draw[<-] (mat-\ai-1) -- node[above] {\mi} +(-3cm,0);
\foreach \ai in {2,4,6,12}
{\foreach \aii/\mii in {3/N1,11/Nn}
\draw[->] (mat-\ai-1) -- (mat-\aii-2) node[yshift=0.6cm] {\mii};
}
\foreach \ai in {3,11}
{ \draw[->] (mat-4-3) -- node[above] {Output 1} +(2cm,0);}
\foreach \ai in {3,11}
{ \draw[->] (mat-\ai-2) -- (mat-10-3);
\draw[->] (mat-10-3) -- node[above] {Output n} +(2cm,0);}
\end{tikzpicture}
\caption{Feedforward Neural Network structure.}
\label{fig:FNN}
\end{figure}
\end{document}
但我对 Latex 和 tikz 不太熟悉,所以我不知道如何修改它,以便它也能适用于我的 DNN。这是我修改后的代码的样子:
我该怎么做?谢谢!
答案1
我通过以下方式找到了解决方案:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\usetikzlibrary{positioning,calc}
\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{center}
\begin{figure}
\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.1cm,
row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer}
& |[plain]| \parbox{1.2cm}{\centering Hidden\\layer 1}
& |[plain]| \parbox{0cm}{\centering ...}
& |[plain]| \parbox{1.2cm}{\centering Hidden\\layer n}
& |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& |[plain]| & |[plain]| & |[plain]| & |[plain]| \\
|[plain]| & & |[plain]| & & |[plain]| \\
& |[plain]| & |[plain]| & |[plain]| & \\
|[plain]| & |[dot]| & |[dot]| & |[dot]| \\
& |[plain]| & |[plain]| & |[plain]| & |[dot]| \\
|[plain]| & |[dot]| & |[dot]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & |[plain]| & |[plain]| & |[dot]| \\
|[dot]| & |[dot]| & |[dot]| & |[dot]| & |[plain]| \\
|[dot]| & |[plain]| & |[plain]| & |[plain]| & \\
|[plain]| & & |[plain]| & & |[plain]| \\
& |[plain]| & |[plain]| \\
};
\foreach \ai/\mi in {2/Input feature 1,4/Input feature 2,6/Input feature 3,12/Input feature n}
\draw[<-] (mat-\ai-1) -- node[above] {\mi} +(-3cm,0);
\foreach \ai in {2,4,6,12}
{\foreach \aii/\mii in {3/N1,11/Nn}
\draw[->] (mat-\ai-1) -- (mat-\aii-2) node[yshift=0cm] {\mii};
}
\foreach \ai in {3,11}
{
\draw[->] (mat-\ai-4) -- (mat-4-5);
\draw[->] (mat-4-5) -- node[above] {Output 1} +(2cm,0);\
}
\foreach \ai in {3,11}
{
\draw[->] (mat-\ai-4) -- (mat-10-5);
\draw[->] (mat-10-5) -- node[above] {Output n} +(2cm,0);
}
\end{tikzpicture}
\caption{Deep Neural Network structure.}
\label{fig:DNN}
\end{figure}
\end{center}
\end{document}