# Generalize neural network

I am using this latex package to draw a neural network (autoencoder). So far, I have the following:

\begin{neuralnetwork}[height=4]
\newcommand{\nodetextclear}[2]{}
\newcommand{\nodetextx}[2]{$x_#2$}
\newcommand{\nodetexty}[2]{$y_#2$}
\inputlayer[count=4, bias=false, title=Input\\layer, text=\nodetextx]
\end{neuralnetwork}


I wondering if there is a way to make the diagram generalized. Instead of going to $x_4$, I would like it to go $x_n$ with three dots. Is it possible with this package?

Yes:

As you can see from the code some more work is needed. To not draw a specific node in a layer you can use the exclude key. To not draw links to/from that excluded node, you can use not to/not from in the options to \linklayers, but that requires a small patch at the moment, due to a bug in the package.

To position the dots, I make use of the fact that each node is named Ln-m, where n is the layer number (counting from 0) and m is the node number.

Finally, to get the subscript n for the last layer, an \ifnum is used in the definition of \nodetextx, similar to this example in the GitHub repo.

\documentclass[border=5mm]{standalone}
\usepackage{neuralnetwork}
\usepackage{xpatch}
\makeatletter
% patch it to replace the former with the latter, and similar for thisnode
\makeatother
\begin{document}
\begin{neuralnetwork}[height=4]
\newcommand{\nodetextclear}[2]{}
% use \ifnum to get different labels
\newcommand{\nodetextx}[2]{\ifnum #2=4 $x_n$ \else $x_#2$ \fi}
\newcommand{\nodetexty}[2]{$y_#2$}
% use exclude to turn off drawing of specific layers
\inputlayer[count=4, bias=false, exclude={3}, title=Input\\layer, text=\nodetextx]
\hiddenlayer[count=3, bias=false, title=Hidden\\layer, text=\nodetextclear]
\path (L0-2) -- node{$\vdots$} (L0-4);
\path (L2-2) -- node{$\vdots$} (L2-4);

• @guy They're usually not created automatically, but by you adding a \caption inside a figure/table environment. Do exactly the same here: put the neuralnetwork in a figure environment, and add \caption{bla bla}. – Torbjørn T. Dec 14 '17 at 14:27
• should'nt the output layer contain y's instead? E.g. with \nodetexty. – Stoner Jan 11 at 4:07