2

I have a neural network and I am trying to connect the final layers in the graph. I tried a few methods but none have worked so far. Also I am having a problem in subscripting the term B in the upper nodes, they are supposed to be B_1,B_2 and B_3 . Any help you can give me would be much appreciated.

Here is what I have so far:

\documentclass[11pt]{report}
\def\layersep{3cm}
\def\nodeinlayersep{1.5cm}
\begin{document}
   \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,draw,circle, fill=white!50,},
    output neuron/.style={neuron,draw,circle, fill=white!50},
    hidden neuron/.style={neuron,draw,circle, fill=white!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron,draw,circle, fill=white!50},%<-- added %%%
]
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,3}
    \node[input neuron] (I-\name) at (0,-\y-2.5) {$x_\y$};  
    % set number of hidden layers
    \newcommand\Nhidden{2}
    % Draw the hidden layer nodes
    \foreach \N in {0,...,\Nhidden} {
       \foreach \y in {0,...,5} { % <-- added 0 instead of 1 %%%%%
     \ifnum \y=4
     \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
       \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     \else
         \ifnum \y=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \ifnum \N<3 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
           \node[bias] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$B_{\pgfmathparse{\N+1}}$}; %<-- added
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
           \node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {}; %<-- added %%%%%%%%%%%
               \else\fi %<-- added %%%%%%%%%%%%
         \fi %<-- added %%%%%%%
         \fi
       }
       \ifnum \N>0 %<-- added %%%%%%
    \node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; % <- added yshift=2cm %%%%%%%%%%%%
    \else\fi %<-- added %%%%%
    }
    % Draw the output layer node
    \foreach \name / \y in {1,2}
    \node[output neuron] (O-\name) at (3*\layersep,-\y-2.5)  {};  
    % set number of hidden layers


    % 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} {
          % \path[yellow] (H-0) edge (H1-\dest);
          \path[dashed,black] (H0-0) edge (H1-\dest); %<-- added %%%%%
            \path[black] (I-\source) edge (H1-\dest);};

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

               \ifnum \source=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%
           \path[dashed,black](H\lastN-\source) edge (H\N-\dest);%<-- added 
              \else %<-- added %%%
              \path[black] (H\lastN-\source) edge (H\N-\dest);%<-- added 
              \fi %<-- added %%%
              }; %<-- added %%%%

    % Connect every node in the hidden layer with the output layer


    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
\end{document}



2
  • No one can really see what you got because \layersep and \nodeinlayersep are not defined in your snippet. Please provide us with a complete minimal working example that starts with \documentclass and ends with \end{document}.
    – user194703
    Mar 11, 2020 at 19:16
  • Apologies, I will add that now
    – KaneM
    Mar 11, 2020 at 19:17

1 Answer 1

4

This is a minimal damage proposal. \pgfmathparse only parses, you can use \the\numexpr\N+1 here.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\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,draw,circle, fill=white!50,},
    output neuron/.style={neuron,draw,circle, fill=white!50},
    hidden neuron/.style={neuron,draw,circle, fill=white!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron,draw,circle, fill=white!50},%<-- added %%%
]
    \def\layersep{3cm}
    \def\nodeinlayersep{1.5cm}
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,3}
    \node[input neuron] (I-\name) at (0,-\y-2.5) {$x_\y$};  
    % set number of hidden layers
    \newcommand\Nhidden{2}
    % Draw the hidden layer nodes
    \foreach \N in {0,...,\Nhidden} {
       \foreach \y in {0,...,5} { % <-- added 0 instead of 1 %%%%%
     \ifnum \y=4
     \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
       \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     \else
         \ifnum \y=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \ifnum \N<3 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
           \node[bias] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep )
           {$B_{\the\numexpr\N+1}$}; %<-- added
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
           \node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {}; %<-- added %%%%%%%%%%%
               \else\fi %<-- added %%%%%%%%%%%%
         \fi %<-- added %%%%%%%
         \fi
       }
       \ifnum \N>0 %<-- added %%%%%%
    \node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; % <- added yshift=2cm %%%%%%%%%%%%
    \else\fi %<-- added %%%%%
    }
    % Draw the output layer node
    \foreach \name / \y in {1,2}
    \node[output neuron] (O-\name) at (3*\layersep,-\y-2.5)  {};  
    % set number of hidden layers


    % 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} {
          % \path[yellow] (H-0) edge (H1-\dest);
          \path[dashed,black] (H0-0) edge (H1-\dest); %<-- added %%%%%
            \path[black] (I-\source) edge (H1-\dest);};

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

               \ifnum \source=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%
           \path[dashed,black](H\lastN-\source) edge (H\N-\dest);%<-- added 
              \else %<-- added %%%
              \path[black] (H\lastN-\source) edge (H\N-\dest);%<-- added 
              \fi %<-- added %%%
              }; %<-- added %%%%

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

    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
\end{document}

enter image description here

If you replace the last loop by

\foreach \X in {1,2}
{\foreach \source in {0,...,3,5}
\path[black] \ifnum\source=0
 [dashed]
\fi
(H\Nhidden-\source) edge (O-\X);}

you get

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .