2

I am trying to modify the code from the answer here to fit my needs.

This is the modified code i made:

\documentclass{article}

\usepackage{tikz} \usetikzlibrary{decorations.pathreplacing,calligraphy} \begin{document} \pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[ shorten >=1pt,->, draw=black, node distance=\layersep, every pin edge/.style={<-,shorten <=1pt}, input neuron/.style={rectangle,minimum size=17pt,inner sep=0pt,fill=green!50}, output neuron/.style={circle,minimum size=17pt,inner sep=0pt, fill=red!50}, hidden neuron/.style={circle,minimum size=17pt,inner sep=0pt, fill=purple!50}, annot/.style={text width=4em, text centered} ]

% Draw the input layer nodes
\foreach \name / \y in {1,...,4}
% This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
    \node[input neuron, pin=left:$x_{\y}$] (I-\name) at (0,-\y) {};

% set number of hidden layers
\newcommand\Nhidden{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,5} {
      \path[yshift=0.5cm]
          node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
       }
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {};
}

% % Draw the output layer node % \node[output neuron,pin={[pin edge={->}]right:$y$}, right of=H\Nhidden-3] (O) {};

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,4}
    \foreach \dest in {1,...,5}
        \path (I-\source) edge (H1-\dest);

% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
   \foreach \source in {1,...,5}
       \foreach \dest in {1,...,5}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach \source in {1,...,5}
    \path (H\Nhidden-\source) edge (O);

% Annotate the layers
\draw [very thick,decorate,decoration={brace,amplitude=10pt}] (2,0) -- (8,0);
\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};

\end{tikzpicture}

\end{document}

And here what i got so far:

enter image description here

So, i need help with the following:

1 - Fixing the brace (theres an arrow at the end of it), and writing Hidden layers above it;

2 - Below the $x_{3}$ rectangle, i would like to add \vdots and change $x_{4}$ to $x_{m}$;

3 - Making the output layer look like the input, i.e., four circles $y_{1}$, $y_{2}$, $y_{3}$ followed by \vdots and $y_{k}$.

Murilo
  • 347

1 Answers1

3

Here's a solution, in respect to your original code. Just a few adjustments.

Redraw a neural network diagram

\documentclass{article}
% https://tex.stackexchange.com/q/629645/204164

\usepackage{tikz} \usetikzlibrary{decorations.pathreplacing,calligraphy} \begin{document} \pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[ shorten >=1pt,%->, draw=black, node distance=\layersep, every pin edge/.style={<-,shorten <=1pt}, input neuron/.style={rectangle,minimum size=17pt,inner sep=0pt,fill=green!50}, output neuron/.style={circle,minimum size=17pt,inner sep=0pt, fill=red!50}, hidden neuron/.style={circle,minimum size=17pt,inner sep=0pt, fill=purple!50}, annot/.style={text width=4em, text centered} ]

\begin{scope}[-&gt;]
% Draw the input layer nodes
\foreach \y in {1,...,4}
    {
    \if\y4
        \node[input neuron, pin=left:$x_{m}$] (I-\y) at (0,-\y) {}; 
    \else  
        \node[input neuron, pin=left:$x_{\y}$] (I-\y) at (0,-\y) {};
    \fi
    }

\path (I-3) --++ (-1,0) |- (I-4) node[pos=0.2] {\vdots};    %Vdots beteween x_3 and x_m

% set number of hidden layers
\newcommand\Nhidden{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,5} {
      \path[yshift=0.5cm]
          node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
       }
\node[annot,above of=H\N-1, node distance=1cm] (hl\N) {};
}

% Draw the output layer nodes
\foreach \t in {1,...,4}
    {
    \if\t4
        \node[output neuron,pin={[pin edge={-&gt;}]right:$y_k$}] (O-\t) at (\Nhidden*\layersep+\layersep,-\t) {}; 
    \else  
        \node[output neuron,pin={[pin edge={-&gt;}]right:$y_\t$}] (O-\t) at (\Nhidden*\layersep+\layersep,-\t) {};
    \fi
    }

\path (O-3) --++ (1,0) |- (O-4) node[pos=0.2] {\vdots}; %Vdots beteween y_3 and y_k

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,4}
    \foreach \dest in {1,...,5}
        \path (I-\source) edge (H1-\dest);

% connect all hidden stuff
\foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
   \foreach \source in {1,...,5}
       \foreach \dest in {1,...,5}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the last hidden layer with the output layer
   \foreach \source in {1,...,5}
       \foreach \dest in {1,...,4}
           \path (H3-\source) edge (O-\dest);



% Annotate the layers

\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};
\end{scope}

\draw [very thick,decorate,decoration={brace,amplitude=10pt}] (2,0) -- (8,0) node [midway,above=10pt] {Hidden layers};

\end{tikzpicture}

\end{document}

SebGlav
  • 19,186