Neural network

Classical neural network.

Author

Emre Anakok

Published

March 18, 2025

\documentclass[border=3pt,tikz]{standalone}
\usepackage{amsmath} % for aligned
\usepackage{listofitems} % for \readlist to create arrays
\usetikzlibrary{arrows.meta} % for arrow size
\usepackage[outline]{contour} % glow around text
\contourlength{1.4pt}

% COLORS
\usepackage{xcolor}
\colorlet{myred}{red!80!black}
\colorlet{myblue}{blue!80!black}
\colorlet{mygreen}{green!60!black}
\colorlet{myyellow}{yellow!60!white}
\colorlet{myorange}{orange!70!red!60!black}
\colorlet{mydarkred}{red!30!black}
\colorlet{mydarkblue}{blue!40!black}
\colorlet{mydarkgreen}{green!30!black}
\colorlet{mydarkyellow}{yellow!30!black}

% STYLES
\tikzset{
  >=latex, % for default LaTeX arrow head
  node/.style={thick,circle,draw=myblue,minimum size=22,inner sep=0.5,outer sep=0.6},
  node in/.style={node,green!20!black,draw=mygreen!30!black,fill=mygreen!25},
  node hidden/.style={node,blue!20!black,draw=myblue!30!black,fill=myblue!20},
  node convol/.style={node,orange!20!black,draw=myorange!30!black,fill=myorange!20},
  node out/.style={node,red!20!black,draw=myred!30!black,fill=myred!20},
  connect/.style={thick,mydarkblue}, %,line cap=round
  connect arrow/.style={-{Latex[length=4,width=3.5]},thick,mydarkblue,shorten <=0.5,shorten >=1},
  node 1/.style={node in}, % node styles, numbered for easy mapping with \nstyle
  node 2/.style={node hidden},
  node 3/.style={node out}
}
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3

\begin{document}


\begin{tikzpicture}[x=2.2cm,y=1.4cm]
    \message{^^JNeural network, shifted}
    \readlist\Nnod{4,5,5,5,3} % array of number of nodes per layer
    \readlist\Nstr{n,m_1,m_2,m_3,m} % array of string number of nodes per layer
    \readlist\Cstr{\strut x^{(0)},x^{(\prev)},x^{(\prev)},x^{(\prev)},y} % array of coefficient symbol per layer
    \def\yshift{0.5} % shift last node for dots
    
    \message{^^J  Layer}
    \foreachitem \N \in \Nnod{ % loop over layers
      \def\lay{\Ncnt} % alias of index of current layer
      \pgfmathsetmacro\prev{int(\Ncnt-1)} % number of previous layer
      \message{\lay,}
      \foreach \i [evaluate={\c=int(\i==\N); \y=\N/2-\i-\c*\yshift;
                   \index=(\i<\N?int(\i):"\Nstr[\lay]");
                   \x=\lay; \n=\nstyle;}] in {1,...,\N}{ % loop over nodes
        % NODES
        \node[node \n] (N\lay-\i) at (\x,\y) {$\Cstr[\lay]_{\index}$};
        
        % CONNECTIONS
        \ifnum\lay>1 % connect to previous layer
          \foreach \j in {1,...,\Nnod[\prev]}{ % loop over nodes in previous layer
            \draw[connect arrow,white,line width=1.2] (N\prev-\j) -- (N\lay-\i);
            \draw[connect arrow] (N\prev-\j) -- (N\lay-\i);
            %\draw[connect] (N\prev-\j.0) -- (N\lay-\i.180); % connect to left
          }
        \fi % else: nothing to connect first layer
        
      }
      \path (N\lay-\N) --++ (0,1+\yshift) node[midway,scale=1.5] {$\vdots$};
    }
  
    \node[thick,circle,draw=mydarkyellow,fill=myyellow,minimum size=22,inner sep=0.5,outer sep=0.6](epsilon) at (6,-0.5){$\varepsilon$};
      \foreach \j in {1,2,3}{ % loop over nodes in previous layer
            %\draw[connect arrow,white,line width=1.2] (N5-\j) -- (epsilon);
            \draw[connect arrow] (N5-\j) -- (epsilon);
            }
    
    % LABELS
    \node[above=5,align=center,mygreen!60!black] at (N1-1.90) {Couche\\[-0.2em]d'entrée};
    \node[above=2,align=center,myblue!60!black] at (N3-1.90) {Couche(s) cachée(s)};
    \node[above=8,align=center,myred!60!black] at (N\Nnodlen-1.90) {Couche\\[-0.2em]de sortie};
     \node[above=10,align=center,mydarkyellow!60!black] at (epsilon) {Fonction\\[-0.2em]de coût\\[-0.2em]};
    
  \end{tikzpicture}
\end{document}