3

I would be mostly obliged if you can help me: I would like to insert a graph like this:

enter image description here

In my case, I need 3 inputs, 4 hidden layers and 2 outputs.

I know that there are similar topics, but I do not understand how to modify the code.

Thank you.

EDIT

For example,

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.6cm,
    row sep=-11pt
},
>=latex
]
\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
    & |[plain]| \\
    |[plain]| & \\
    & |[plain]| \\
    |[plain]| & |[plain]| \\
    & & \\
    |[plain]| & |[plain]| \\
    & |[plain]| \\
    |[plain]| & \\
    & |[plain]| \\
};
\foreach \ai [count=\mi ]in {2,4,...,10}
\draw[<-] (mat-\ai-1) -- node[above] {I\mi} +(-1cm,0);
\foreach \ai in {2,4,...,10}
{\foreach \aii in {3,6,9}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {3,6,9}
\draw[->] (mat-\ai-2) -- (mat-6-3);
\draw[->] (mat-6-3) -- node[above] {O1} +(1cm,0);
\end{tikzpicture}

\end{figure}
\end{document}

I do not understand how to add or remove units. Most importantly, what does \ai or \aii stand for?

  • By "similar topics", do you mean e.g. tex.stackexchange.com/questions/132444? If you were to point us to the code you had in mind, someone could tell you how to modify it. – Torbjørn T. Feb 15 '17 at 11:41
  • Please add a minimal working example (MWE) showing what have you done till now and explaining your problem, if you don't know how to create it, see here! – CarLaTeX Feb 15 '17 at 11:44
  • 1
    ai and aii are loop variables. – Thruston Feb 15 '17 at 12:25
5

With your code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.6cm,
    row sep=-11pt
},
>=latex
]

\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
    |[plain]| & \\
    & |[plain]| \\
    |[plain]| & &  \\
    & |[plain]| \\
    |[plain]| & &  \\
    & |[plain]| \\
    |[plain]| & \\
};
\foreach \ai [count=\mi ]in {3,5,7}
\draw[<-] (mat-\ai-1) -- node[above] {Input \mi} +(-2cm,0);
\foreach \ai in {3,5,7}
{\foreach \aii in {2,4,...,8}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {2,4,...,8}
\draw[->] (mat-\ai-2) -- (mat-6-3);

\foreach \ai in {2,4,...,8}
\draw[->] (mat-\ai-2) -- (mat-4-3);

\foreach \ai [count=\mi ]in {4,6}
\draw[->] (mat-\ai-3) -- node[above] {Output \mi} +(2cm,0);
\foreach \ai in {3,5,7}
{\foreach \aii in {2,4,...,8}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\end{tikzpicture}

\end{figure}
\end{document}

enter image description here %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Or: See: http://www.texample.net/tikz/examples/neural-network/ (Kjell Magne Fauske)

Modification:

\documentclass{article}

\usepackage{tikz}
\usepackage{verbatim}

% Basis: http://www.texample.net/tikz/examples/neural-network/
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\begin{tikzpicture}[shorten >=1pt,->,draw=black!100, node distance=\layersep]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{output neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{hidden neuron}=[neuron, fill=white!100,draw=black];
    \tikzstyle{annot} = [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) {};

% Draw the hidden layer nodes
\foreach \name / \y in {1,...,4}
    \path[yshift=0.5cm]
        node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

% Draw the output layer node
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-2] (O1) {};
\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O2) {};

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

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

% Annotate the layers
\node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
\node[annot,left of=hl] {Input layer};
\node[annot,right of=hl] {Output layer};

\end{tikzpicture}
% End of code
\end{document}

enter image description here

  • @Zarko The code from the first revision of this answer definitely produces two outputs, but perhaps it was edited within the grace period, so that it doesn't show as a separate revision. (Bobyandbob: sorry for the noise.) – Torbjørn T. Feb 15 '17 at 14:59
  • Thank you! Now I understand how those lines code work much better. – Jonathan_Lo Feb 15 '17 at 15:35
  • It would be better to use the positioning library's syntax for relative positioning. – cfr May 24 '17 at 20:05
  • @cfr Do you mean the \foreach loops, i don't know what i should change. Or could you edit the answer with your improvements? – Bobyandbob May 24 '17 at 20:22
  • 1
    I mean right of= is considered deprecated and the positioning library's alternative syntax is recommended. right=of etc. – cfr May 24 '17 at 20:28
5

This is more extended comment and exercise of understanding of Bobyandbob answer than new answer. My findings:

  • kernel of image code is matrix with three column and eight rows
  • in the first rows are names of neural network layers
  • in other rows the empty cells are used for showing nodes in neural network
  • cells which are not used are in his answer designated with |[plain]|
  • not all cells in matrix are explicit defined (this cause confusion)
  • connections between nodes are drawn with nested loops (outer for left layer, inner for right layer)
  • inputs and outputs are drawn in single loops

Changes which gives me better understanding of answer:

  • strictly define all cell in matrix
  • for mark unused cells define new style, for example ec (as empty cell)
  • redefine style plain so, that in its use can be eliminated \parbox{...}

With this changes Bobyandbob's MWE become:

\documentclass[tikz, margin=3mm] {standalone}
\usetikzlibrary{arrows.meta, % for edge arrows 
                matrix,      % nodes will set as matrix elements
                }
\begin{document}
    \begin{tikzpicture}[
           > = Stealth, semithick,
plain/.style = {draw=none, fill=none, yshift=11mm,
                text width=7ex,  align=center},% for text in images, 
   ec/.style = {draw=none},% for emty cells
  net/.style = {% for matrix style
    matrix of nodes,
    nodes={circle, draw, semithick, minimum size=12mm, inner sep=0mm},% circles in image
    nodes in empty cells,% for not used cells in matrix
  column sep = 16mm, % distance between columns in matrix 
     row sep = -3mm  % distance between rows in matrix
            },
]
\matrix[net] (m)% m is matrix name, it is used for names of cell: firs has name m-1-1
                % in empty space between ampersands will show circles: 
                % i.e.: nodes of the neural network
{
|[plain]| Input layer & |[plain]| Hidden layer  & |[plain]| Output layer  \\
|[ec]|                &                         & |[ec]|                  \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         &                         \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         &                         \\
                      & |[ec]|                  & |[ec]|                  \\
|[ec]|                &                         & |[ec]|                  \\
};
\% inputs
\foreach \in [count=\ir from 1] in {3,5,7}
\draw[<-] (m-\in-1.west) -- node[above] {Input \ir} +(-22mm,0);
% connections between nodes in the first and second layer
\foreach \j in {3,5,7}
{
\foreach \k in {2,4,6,8} \draw[->] (m-\j-1) -- (m-\k-2);
}
% connections between nodes in the second and third layer
\foreach \j in {2,4,6,8}
{
\foreach \k in {4,6} \draw[->] (m-\j-2) -- (m-\k-3);
}
% output
\draw[->] (m-4-3.east) -- node[above] {Output 1} +(22mm,0);
\draw[->] (m-6-3.east) -- node[above] {Output 2} +(22mm,0);
    \end{tikzpicture}
\end{document}

The result is (almost) the same as in Bobyandbob's answer:

enter image description here

Between both MWE are also some minor, not inportant differences as used document class, size of nodes and determining coordinates used for inputs and outputs (added are anchors to node names).

  • Nice answer and comment. – Bobyandbob Feb 16 '17 at 23:12
5

Since nobody actually demonstrated how to include 4 hidden layers, here's another one. This doesn't have 4 hidden layers either, but it does have more than 1.

Internal connection strength is indicated by opacity, varied randomly from 25% to 100%.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows.meta,quotes,shadows,decorations.pathreplacing,positioning,fadings}
\usepackage{cfr-lm}
\begin{document}
\colorlet{mewnol}{blue!75!cyan}%
\colorlet{allanol}{blue!50!black}%
\begin{tikzpicture}
  [
    lliw nn/.code={\colorlet{lliwnn}{#1}},
    lliw nn=mewnol,
    nn/.style={draw=lliwnn, inner color=white, outer color=lliwnn!5, circular drop shadow},
    font=\sffamily\plstyle,
    >=Latex,
    every edge/.append style={->},
    every edge quotes/.append style={font=\sffamily\plstyle\footnotesize,},
    semithick,
  ]
  \matrix (n) [matrix of nodes, nodes={circle, minimum size=20pt, thick}, nodes in empty cells, column sep=7.5mm, column 1/.append style={nodes={coordinate}}, column 12/.append style={nodes={coordinate}}, column 2/.append style={lliw nn=allanol}, column 11/.append style={lliw nn=allanol}]
  {
    &[10mm]&&|[nn]|&&|[nn]|&&|[nn]|&&&&[10mm]\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&\\
    &|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&\\
    &&|[nn]|&&|[nn]|&&|[nn]|&&|[nn]|&&&\\
    &&&|[nn]|&&|[nn]|&&|[nn]|&&&&\\
  };
  \foreach \i [count=\j from 1] in {3,5,7} \draw [allanol]  (n-\i-1) edge ["Input \j"] (n-\i-2) ;
  \foreach \i [count=\j from 1] in {4,6} \draw [allanol] (n-\i-11) edge ["Output \j"] (n-\i-12)  ;
  \begin{scope}[every edge/.append style={draw=mewnol, opacity={random(25,100)/100}}]
    \foreach \i in {3,5,7} \draw (n-\i-10) edge (n-4-11) edge (n-6-11);
    \foreach \i in {3,5,7} \foreach \j in {2,4,6,8} \draw (n-\i-2) edge (n-\j-3) (n-\j-9) edge (n-\i-10);
    \foreach \k [evaluate=\k as \m using {int(\k+1)}, evaluate=\k as \n using {int(\k+2)} ] in {3,5,7} \foreach \i in {2,4,6,8} \foreach \j in {1,3,5,7,9} \draw  (n-\i-\k) edge (n-\j-\m) (n-\j-\m) edge (n-\i-\n) ;
  \end{scope}
  \begin{scope}[decoration={brace, amplitude=7.5pt}, thick, every node/.append style={align=center}]
    \draw [decorate, mewnol] ([xshift=-2.5pt]n-1-3.west |- n-1-3.north) ++(0,7.5pt) coordinate (l) -- ([xshift=2.5pt]n-1-10.east |- l) node [above=7.5pt,midway] {Hidden\\Layers};
    \draw [decorate, allanol] ([xshift=-2.5pt]n-1-2.west |- l) -- ([xshift=2.5pt]n-1-2.east |- l) node [above=7.5pt,midway]  {Input\\Layer};
    \draw [decorate, allanol] ([xshift=-2.5pt]n-1-11.west |- l) -- ([xshift=2.5pt]n-1-11.east |- l) node [above=7.5pt,midway]  {Output\\Layer};
  \end{scope}
  \draw [thick, ->, allanol] ([xshift=-2.5pt,yshift=-10pt]current bounding box.south -| n-9-2.west) coordinate (ll) -- ([xshift=2.5pt]n-9-11.east |- ll) node [midway, below] {Flow of Information};
  \node (cs) [every edge quotes, anchor=base west, xshift=50mm, mewnol] at ([xshift=5pt,yshift=-12.5pt]n-1-1 |- current bounding box.south) {Connection Strength};
  \fill [fill=mewnol, path fading=west] (cs.base west) ++(-5pt,\pgflinewidth) rectangle ++(-50mm,5pt) ;
\end{tikzpicture}
\end{document}

neural net

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.