I'm trying to draw a neural network diagram with tikz
. I found this code online:
\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}
It produces the following image:
It has 5 input nodes but I want to generalize this drawing by having n nodes in each of the layers. So in input layer, number of node shown will be 4 with vertical '…' in middle, the hidden layer would contain 3 nodes with vertical '…' in middle and output would have 2 nodes with vertical '…' in middle. I want to maintain their conical shape. Something like this:
It is a really bad drawing, sorry about that, one can now imagine how bad I must be with tikz
. The dots in middle are actually fully filled, in my drawing they are hollow. Any help or advice would be appreciated.
Best Answer
Here, we a have a festival of
\foreach
:Although it seems unwise to have
n
denote the number of nodes in each layer when they could be different and the arrangement of the diagram suggests they are not.