3

I am trying to draw a neural network like following, but I want to draw a square at the beginning, to be specific:

My neural Network:

\documentclass[12pt, oneside, letterpaper]{book}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage{neuralnetwork}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\linklayers}{\nn@lastnode}{\lastnode}{}{}
\xpatchcmd{\linklayers}{\nn@thisnode}{\thisnode}{}{}
\begin{document}
\begin{figure}[h]
\centering
 \label{fig:arquitectura_autoencoder}
\begin{neuralnetwork}[height=9.5,nodespacing=1cm,nodesize=20pt,  layerspacing=3.6cm][h]

    %comandos previos
    \newcommand{\x}[2]{\ifthenelse{\equal{#2}{0}}{$b_0$}{\ifnum #2=8 $x_n$ \else $x_#2$ \fi}} %si es el índice cero, entonces le asignas b0, sino si es la neurona 8, lo cambias por el índice n
    \newcommand{\ffirst}[2]{\ifthenelse{\equal{#2}{0}}{$b_1$}
    {\ifnum #2=4 $a_{\frac{n}{2}}^{(2)}$ \else $a_{#2}^{(2)}$ \fi}}
    \newcommand{\fsecond}[2]{\ifthenelse{\equal{#2}{0}}{$b_2$}
    {\ifnum #2=3 $a_{\frac{n}{4}}^{(3)}$ \else $a_{#2}^{(3)}$ \fi}}
    \newcommand{\fthird}[2]{\ifthenelse{\equal{#2}{0}}{$b_3$}
    {\ifnum #2=4 $a_{\frac{n}{2}}^{(4)}$ \else$a_{#2}^{(4)}$ \fi}}
    \newcommand{\xhat}[2]{\ifthenelse{\equal{#2}{0}}{$b_0$}{\ifnum #2=8 $\hat{x}_n$ \else $\hat{x}_#2$ \fi}}

    %red neuronal
    \inputlayer[count=8, bias=true,exclude={7},title=Input\\layer, text=\x]
    \hiddenlayer[count=4, bias=true,exclude={3},title={$L_2$\\$f_{\cdot 2} = \tanh(x)$}, text=\ffirst] 
    \linklayers[not from={7},not to={3}] %para que no dibuje la flecha desde la neurona 7
    \hiddenlayer[count=3, bias=true,exclude={2}, title={$L_3$\\$f_{\cdot 3} = ReLU(x) $}, text=\fsecond]
    \linklayers[not from={3},not to={2}]
    \hiddenlayer[count=4, bias=true,exclude={3}, title={$L_4$\\$f_{\cdot 4} = \tanh(x)$}, text=\fthird]
    \linklayers[not from={2},not to={3}]
    \outputlayer[count=8, exclude={7},title={Output layer\\$\hat{x}_i=a_{i}^{(5)}$}, text=\xhat]
    \linklayers[not to={7},not from={3}]


     % draw dots
    \path (L0-6) -- node{$\vdots$} (L0-8);
    \path (L1-2) -- node{$\vdots$} (L1-4);
    \path (L2-1) -- node{$\vdots$} (L2-3);
    \path (L3-2) -- node{$\vdots$} (L3-4);
    \path (L4-6) -- node{$\vdots$} (L4-8);

\end{neuralnetwork}
\end{figure}
\end{document}

Generates:

enter image description here

What I want is something similar, but I want a little square before the input layer and then the output layer because I want to add more information. I attach a similar image I want.

What I want:

enter image description here

Is this possible to do it? I think it is hard.

  • 1
    Hello and welcome. Please provide fully compilable code. This saves everyone from having to search needlessly for the packages you use. – AndréC Feb 16 '20 at 07:30
  • Thank you @AndréC, I have edited the question, sorry about that... – Henry Navarro Feb 18 '20 at 13:35
  • Consider using sane_tikz: https://github.com/negrinho/sane_tikz. I can reproduce both figures easily with it. Will do later if you don't solve it first. Also, it might be better to be more specific about what you mean by "a square in the beginning" (maybe hand annotate the partial solution you have with the modifications). – rnegrinho Feb 20 '20 at 15:59
  • @rnegrinho I won't try to do it by myself, not enough time. You can be sure if you answer is correct I will upvote it and I will accept it. So, take your time. – Henry Navarro Feb 20 '20 at 16:11
  • @DannyHern I won't do it unless it clear what you intend. The two figures that you refer to are very different and the text you wrote does not disambiguate it sufficiently for me. Of course, I can reproduce both figures, but that is not the purpose of this question. – rnegrinho Feb 20 '20 at 16:34

1 Answers1

3

Without the neuralnetwork package it is rather easy to draw something that looks like the screenshot you want.

\documentclass[12pt, oneside, letterpaper]{book}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,shapes.geometric}
\begin{document}
\begin{figure}[h]
\centering
 \begin{tikzpicture}[neuron/.style={ellipse,minimum width=1.2em,minimum
    height=1.6em,draw,thick},
    box/.style={draw,minimum height=3.4em+#1,text width=2em,align=center},
    font=\sffamily\tiny,yscale=1.25,node distance=1cm,
    every label/.append style={node font=\tiny},
    Dotted/.style={% https://tex.stackexchange.com/a/52856/194703
    dash pattern=on 0.1\pgflinewidth off #1\pgflinewidth,line cap=round,
    shorten >=2mm,shorten <=2mm,line width=2pt},
    Dotted/.default=4,thick]
  \begin{scope}[local bounding box=neurons]
   \path foreach \X in {1,...,4}
   {(-2,{-\X-(\X==4)*1.5}) node[neuron](x\X){} 
    \ifnum\X=4
      edge[Dotted,line width=2pt] (x3)
    \fi
    (2,{-\X-(\X==4)*1.5}) node[neuron](y\X){}
    \ifnum\X=4
      edge[Dotted] (y3)
    \fi};
   \path foreach \Y in {1,2,3} 
   {([yshift=2cm-\Y cm]neurons.center) 
    node[neuron,label=above:N\Y] (N\Y){}
    foreach \X in {1,...,4} { edge (x\X) edge (y\X)}};
  \end{scope}
  \node[above=6mm of N1,align=center]{Hidden\\ Nodes};
  \path let \p1=($(neurons.north)-(neurons.south)$) in
   node[box=\y1,left=of neurons] (PCAC) {PCA Compression}
   node[box=\y1+2cm,left=of PCAC] (Ori) {Original HIV Data}
   node[box=\y1,right=of neurons] (PCAD) {PCA Deompression}
   node[box=\y1+2cm,right=of PCAD] (Pre) {Predicted HIV Data};
  \draw foreach \X in {1,...,4}
  { (x\X) -- node[near start,above]{x\X} (PCAC.east|-x\X)
  (y\X) -- node[near start,above]{y\X} (PCAD.west|-y\X)};
  \draw foreach \X [count=\Y] in {0.1,0.2,0.3,0.4,0.9}
  { ($(PCAC.north west)!\X!(PCAC.south west)$)
    -- coordinate (w\Y) ($(Ori.north east)!\X!(Ori.south east)$)
    ($(PCAD.north east)!\X!(PCAD.south east)$)
    -- coordinate (e\Y) ($(Pre.north west)!\X!(Pre.south west)$)}
   (w4) edge[Dotted] (w5) (e4) edge[Dotted] (e5);
 \end{tikzpicture}
\caption{A neuronal network.}
\label{fig:arquitectura_autoencoder}
\end{figure}
\end{document}

enter image description here