1

I want to represent my ANN for a article, but with 12 neurons it become way too polluted. So how can I add the three dots after a node and than add the last node to it? enter image description here

               \documentclass{standalone}
                    \usepackage{tikz}
                    \begin{document}
                    \pagestyle{empty}
                    \def\layersep{3cm}
                    \def\nodeinlayersep{1.5cm}
                    \begin{tikzpicture}[
                       shorten >=1pt,->,
                       draw=black!50,
                        node distance=\layersep,
                        every pin edge/.style={<-,shorten <=1pt},
                        neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
                        input neuron/.style={neuron, fill=green!50},
                        output neuron/.style={neuron, fill=red!50},
                        hidden neuron/.style={neuron, fill=blue!50},
                        annot/.style={text width=4em, text centered}
                    ]
                        % Draw the input layer nodes
                        \foreach \name / \y in {1,...,3}
                        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
                            \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y) {};
                        % set number of hidden layers
                        \newcommand\Nhidden{2}
                        % Draw the hidden layer nodes
                        \foreach \N in {1,...,\Nhidden} {
                           \foreach \y in {1,...,12} {
                              \path[yshift=7cm]
                              node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$};
                               }
                        \node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
                        }
                        % Draw the output layer node
                        \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H\Nhidden-6] (O) {};
        %
                        % Connect every node in the input layer with every node in the
                        % hidden layer.
                        \foreach \source in {1,...,3}
                            \foreach \dest in {1,...,12}
                                \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,...,12}
                               \foreach \dest in {1,...,12}
                                   \path (H\lastN-\source) edge (H\N-\dest);
                        % Connect every node in the hidden layer with the output layer
                        \foreach \source in {1,...,12}
                            \path (H\Nhidden-\source) edge (O);
                        % Annotate the layers
                        \node[annot,left of=hl1] {Input layer};
                        \node[annot,right of=hl\Nhidden] {Output layer};
                    \end{tikzpicture}
                    % End of code
                    \end{document}
  • I'm not sure what you want, do you want three dots between each layer? Or three dots from each node in layer n to each node in layer n+1? – Thorbjørn E. K. Christensen Nov 15 '17 at 18:02
  • I want to add dots after the 4th node of each hidden layer. – Caio Custódio Nov 15 '17 at 18:13
  • Can you draw it in paint? – Thorbjørn E. K. Christensen Nov 15 '17 at 18:16
  • I'm not at all sure what you want so my answer has some options. – Thorbjørn E. K. Christensen Nov 15 '17 at 18:37
5

Is this what you were after? I've added comments in the code where I've changed things.

output of code

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{3cm}
\def\nodeinlayersep{1.5cm}
\begin{tikzpicture}[
   shorten >=1pt,->,
   draw=black!50,
    node distance=\layersep,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron, fill=green!50},
    output neuron/.style={neuron, fill=red!50},
    hidden neuron/.style={neuron, fill=blue!50},
    annot/.style={text width=4em, text centered}
]
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,3}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
    \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y-2.5) {};  %%% <-- MODIFIED
    % set number of hidden layers
    \newcommand\Nhidden{2}

    % Draw the hidden layer nodes
    \foreach \N in {1,...,\Nhidden} {
       \foreach \y in {1,...,5} { %%% MODIFIED (1,...,12 -> 1,...,5, and the next five lines)
         \ifnum \y=4
           \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
         \else
           \node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$};
         \fi
       }
    \node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Hidden layer \N};
    }
    % Draw the output layer node
    \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H\Nhidden-3] (O) {}; %%% <-- MODIFIED (from H\Nhidden-6 to H\Nhidden-3) 
    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,3}
        \foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,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,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
           \foreach \dest in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
               \path (H\lastN-\source) edge (H\N-\dest);
    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,3,5} %%% <-- MODIFIED (1,...,12 -> 1...,3,5)
        \path (H\Nhidden-\source) edge (O);
    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};
\end{tikzpicture}
% End of code
\end{document}
  • As I will have to make another alterations, I'd like to know how I can change the space between the nodes vertically. @TorbjørnT. – Caio Custódio Nov 16 '17 at 21:52
  • 1
    @CaioCustódio Change \nodeinlayersep? – Torbjørn T. Nov 16 '17 at 22:19

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.