I'm using the neuralnetwork package to add a neural network illustration to my text. The neural network is of variational auto-encoder and as you know the middle layer of this type of network is made of some mean and std that we sample from the normal distribution made out of these parameters. How is it possible to create an illustration for this layer? How to name each node in a layer differently (mu and sigma)?

edit: This is the latex code I have already:

    \inputlayer[count=4, bias=false, title=Input\\layer, text=\nodetextx]
    \hiddenlayer[count=2, bias=false, title=Hidden\\layer, text=\nodetextclear] \linklayers
    \hiddenlayer[count=1, bias=false, title=Latent layer] \linklayers
    \outputlayer[count=4, title=Output\\layer, text=\nodetextx] \linklayers

which gives me this: variational autoencoder

But what I want it to be is this one: Real variational autoencoder

picture copy right: Toward Data Science

  • 3
    Welcome. Could you please add (1) a minimal compilable code showing what you already have and (2) a sketch of what you want (instead of a text which may confuse people)?
    – user156344
    Apr 13 '19 at 12:51
  • 1
    @EthanT - we are not neural network experts here. Please share the LaTeX code that you have done so far and your requirement. Apr 13 '19 at 13:54

A little late perhaps but here is something very similar. Code lives here.


% Variational autoencoder architecture. The earliest type of generative machine learning model.




  % #1 (str): namespace
  % #2 (list[list[str]]): list of labels to print in the node of each neuron
  \foreach \neurons [count=\lyrIdx] in #2 {
    \StrCount{\neurons}{,}[\arrlength] % uses the xstring package
    \foreach \n [count=\nIdx] in \neurons
      \node[neuron] (#1-\lyrIdx-\nIdx) at (2*\lyrIdx, \arrlength/2-1.4*\nIdx) {\n};

  % #1 (str): namespace
  % #2 (list[int]): number of nodes in each layer
  \foreach \n [count=\lyrIdx, remember=\lyrIdx as \previdx, remember=\n as \prevn] in #2 {
    \foreach \y in {1,...,\n} {
      \ifnum \lyrIdx > 1
        \foreach \x in {1,...,\prevn}
          \draw[->] (#1-\previdx-\x) -- (#1-\lyrIdx-\y);

    shorten >=1pt, shorten <=1pt,
    neuron/.style={circle, draw, minimum size=4ex, thick},

  % encoder
  \drawNodes{encoder}{{{,,,,}, {,,,}, {,,}}}
  \denselyConnectNodes{encoder}{{5, 4, 3}}

  % decoder
    \drawNodes{decoder}{{{,,}, {,,,}, {,,,,}}}
    \denselyConnectNodes{decoder}{{3, 4, 5}}

  % mu, sigma, sample nodes
  \foreach \idx  in {1,...,3} {
      \coordinate[neuron, right=2 of encoder-3-2, yshift=\idx cm,, fill=yellow, fill opacity=0.2] (mu\idx);
      \coordinate[neuron, right=2 of encoder-3-2, yshift=-\idx cm, fill=blue, fill opacity=0.1] (sigma\idx);
      \coordinate[neuron, right=4 of encoder-3-2, yshift=\idx cm-2cm, fill=green, fill opacity=0.1] (sample\idx);

  % mu, sigma, sample boxes
  \node [label=$\mu$, fit=(mu1) (mu3), draw, fill=yellow, opacity=0.45] (mu) {};
  \node [label=$\sigma$, fit=(sigma1) (sigma3), draw, fill=blue, opacity=0.3] (sigma) {};
  \node [label=sample, fit=(sample1) (sample3), draw, fill=green, opacity=0.3] (sample) {};

  % mu, sigma, sample connections
  \draw[->] (mu.east) -- (sample.west) (sigma.east) -- (sample.west);
  \foreach \a in {1,2,3}
  \foreach \b in {1,2,3} {
      \draw[->] (encoder-3-\a) -- (mu\b);
      \draw[->] (encoder-3-\a) -- (sigma\b);
      \draw[->] (sample\a) -- (decoder-1-\b);

  % input + output labels
  \foreach \idx in {1,...,5} {
      \node[left=0 of encoder-1-\idx] {$x_\idx$};
      \node[right=0 of decoder-3-\idx] {$\hat x_\idx$};


There is an error in your code:

\outputlayer[count=4, title=Output\\layer, text=\nodetexty]

You can insert what you want in text (text=$\mu^{#1}$, for example).

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.