6

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]
    \hiddenlayer[count=3, bias=false, title=Hidden\\layer, text=\nodetextclear] \linklayers
    \outputlayer[count=4, title=Output\\layer, text=\nodetextx] \linklayers
\end{neuralnetwork}

enter image description here

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?

5

Yes:

output of code

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
% \linklayers have \nn@lastnode instead of \lastnode,
% patch it to replace the former with the latter, and similar for thisnode
\xpatchcmd{\linklayers}{\nn@lastnode}{\lastnode}{}{}
\xpatchcmd{\linklayers}{\nn@thisnode}{\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]
      \linklayers[not from={3}]
    \outputlayer[count=4, exclude={3}, title=Output\\layer, text=\nodetextx] 
      \linklayers[not to={3}]

    % draw dots
    \path (L0-2) -- node{$\vdots$} (L0-4);
    \path (L2-2) -- node{$\vdots$} (L2-4);
\end{neuralnetwork}
\end{document}
  • One last little thing, is there a way to add a figure caption as well? These are usually created automatically when adding images/tables. – guy Dec 14 '17 at 14:26
  • 2
    @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
  • 1
    @guy I just realized I'd misunderstood something about the package, so the answer is updated. – Torbjørn T. Dec 16 '17 at 22:07
  • should'nt the output layer contain y's instead? E.g. with \nodetexty. – Stoner Jan 11 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.