# DAVIDSTUTZ

Check out the latest superpixel benchmark — Superpixel Benchmark (2016) — and let me know your opinion! @david_stutz
29thOCTOBER2016

## Network Graph of a Multilayer Perceptron in LaTeX using TikZ

Network graph of a multilayer perceptron in LaTeX using TikZ. The example is part of a set of examples available on GitHub.

multilayer-perceptron.tex
\documentclass[twoside,11pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, latexsym}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{figure}[t]
\centering
\begin{tikzpicture}[shorten >=1pt]
\tikzstyle{unit}=[draw,shape=circle,minimum size=1.15cm]
%\tikzstyle{hidden}=[draw,shape=circle,fill=black!25,minimum size=1.15cm]
\tikzstyle{hidden}=[draw,shape=circle,minimum size=1.15cm]

\node[unit](x0) at (0,3.5){$x_0$};
\node[unit](x1) at (0,2){$x_1$};
\node at (0,1){\vdots};
\node[unit](xd) at (0,0){$x_D$};

\node[hidden](h10) at (3,4){$y_0^{(1)}$};
\node[hidden](h11) at (3,2.5){$y_1^{(1)}$};
\node at (3,1.5){\vdots};
\node[hidden](h1m) at (3,-0.5){$y_{m^{(1)}}^{(1)}$};

\node(h22) at (5,0){};
\node(h21) at (5,2){};
\node(h20) at (5,4){};

\node(d3) at (6,0){$\ldots$};
\node(d2) at (6,2){$\ldots$};
\node(d1) at (6,4){$\ldots$};

\node(hL12) at (7,0){};
\node(hL11) at (7,2){};
\node(hL10) at (7,4){};

\node[hidden](hL0) at (9,4){$y_0^{(L)}$};
\node[hidden](hL1) at (9,2.5){$y_1^{(L)}$};
\node at (9,1.5){\vdots};
\node[hidden](hLm) at (9,-0.5){$y_{m^{(L)}}^{(L)}$};

\node[unit](y1) at (12,3.5){$y_1^{(L+1)}$};
\node[unit](y2) at (12,2){$y_2^{(L+1)}$};
\node at (12,1){\vdots};
\node[unit](yc) at (12,0){$y_C^{(L+1)}$};

\draw[->] (x0) -- (h11);
\draw[->] (x0) -- (h1m);

\draw[->] (x1) -- (h11);
\draw[->] (x1) -- (h1m);

\draw[->] (xd) -- (h11);
\draw[->] (xd) -- (h1m);

\draw[->] (hL0) -- (y1);
\draw[->] (hL0) -- (yc);
\draw[->] (hL0) -- (y2);

\draw[->] (hL1) -- (y1);
\draw[->] (hL1) -- (yc);
\draw[->] (hL1) -- (y2);

\draw[->] (hLm) -- (y1);
\draw[->] (hLm) -- (y2);
\draw[->] (hLm) -- (yc);

\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt] (-0.5,4) -- (0.75,4) node [black,midway,yshift=+0.6cm]{input layer};
\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt] (2.5,4.5) -- (3.75,4.5) node [black,midway,yshift=+0.6cm]{$1^{\text{st}}$ hidden layer};
\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt] (8.5,4.5) -- (9.75,4.5) node [black,midway,yshift=+0.6cm]{$L^{\text{th}}$ hidden layer};
\draw [decorate,decoration={brace,amplitude=10pt},xshift=-4pt,yshift=0pt] (11.5,4) -- (12.75,4) node [black,midway,yshift=+0.6cm]{output layer};
\end{tikzpicture}
\caption[Network graph for a $(L+1)$-layer perceptron.]{Network graph of a $(L+1)$-layer perceptron with $D$ input units and $C$ output units. The $l^{\text{th}}$ hidden layer contains $m^{(l)}$ hidden units.}
\label{fig:multilayer-perceptron}
\end{figure}

\end{document}

multilayer-perceptron.pdf
multilayer-perceptron

What is your opinion on the code snippet? Is it working? Let me know your thoughts in the comments below or using the following platforms:

• Sina

Hi, thanks for sharing your code.
Actually when I compile the latex code in Texmaker, the integrated pdf reader shows the graph properly, with the shade in the intermediate layers (quite innovative by the way!), but when I try to open the output by Adobe Reader, the inner fleshes are as bold as the normal ones (no shading). Would you please tell me what’s the problem?

Thanks again.

• davidstutz