0

This question is building of a solution posted here. The code I have below has to be optimized as the output layer is not centered to the rest of the figure.

I would like to know how to go about centering the output layer with the rest of the figure.

Also, I wanted to know how to go about setting the correct position of the output layer instead of having it hard coded they way that currently have it:

\node[output neuron, pin=right:Output \#\y] (O-\name) at (4*\layersep,-\y) {};

enter image description here

Here is the code that I have so far:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\pagestyle{empty}

\def\layersep{2.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,...,8}
% 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{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,9} {
      \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) {Hidden layer \N};
}

% Draw the output layer node
\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[output neuron, pin=right:Output \#\y] (O-\name) at (4*\layersep,-\y) {};
    %  How to calculate the exact location of the output layers instead of hardcoding the value

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,8}
    \foreach \dest in {1,...,9}
        \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,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% 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} 
Zarko
  • 296,517
Joe
  • 9,080

2 Answers2

2

Change coordinates for nodes in output layer from (4*\layersep,-\y) to (4*\layersep,-\y-2):

enter image description here

Complete MWE:

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
\def\layersep{2.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,...,8}
% 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{3}

% Draw the hidden layer nodes
\foreach \N in {1,...,\Nhidden} {
   \foreach \y in {1,...,9} {
      \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) {Hidden layer \N};
}

% Draw the output layer node
\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[output neuron, 
          pin=right:Output \#\y] (O-\name) 
          at (4*\layersep,-\y-2) {};  % <-----------
    %  How to calculate the exact location of the output layers instead of hardcoding the value

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,8}
    \foreach \dest in {1,...,9}
        \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,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% Annotate the layers

\node[annot,left of=hl1] {Input layer};
\node[annot,right of=hl\Nhidden] {Output layer};
\end{tikzpicture}
\end{document}
Zarko
  • 296,517
1

It is hard to give you a non hard coded answer since all the other values are hard coded. You need

at (4*\layersep,{-\y*1cm-(9-4-1)*0.5cm})

because there are 9 hidden nodes per layer, 4 output nodes, and then there is a hard coded [yshift=0.5cm] in the construction of the hidden neurons, thus the additional -1. The upshot is that if you change the number of output nodes from 4 to another value, you need to change the 4 in the above formula to the same value.

\documentclass{article}

\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\def\layersep{2.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,...,8}
% 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{3}

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

% Draw the output layer node
\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[output neuron, pin=right:Output \#\y] (O-\name) 
    at (4*\layersep,{-\y*1cm-(9-4-1)*0.5cm}) {};
    %  How to calculate the exact location of the output layers instead of hardcoding the value

% Connect every node in the input layer with every node in the
% hidden layer.
\foreach \source in {1,...,8}
    \foreach \dest in {1,...,9}
        \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,...,9}
       \foreach \dest in {1,...,9}
           \path (H\lastN-\source) edge (H\N-\dest);

% Connect every node in the hidden layer with the output layer
\foreach [remember=\N as \lastN (initially 3)] \N in {2,...,\Nhidden}
\foreach \source in {1,...,9}
    \foreach \dest in {1,...,4}
        \path (H\lastN-\source) edge (O-\dest);

% 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