1

I am actually struggling to draw a custom neural network diagram which I had made using MS PowerPoint. I got a lot of help from my previous question but still I am not able to resolve some of the issues, in order to make the copy of the figure which I have.

Expected:

enter image description here

Currently, I am able to resolve the problem till this level. Here is my attempt. But there are the following error:

  1. How to add dots for hidden layers (showing the several hidden layers in between)
  2. How to make the output node to only 1 and also changing its label to summation (symbol),

Current:

enter image description here

\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{listofitems} % for \readlist to create arrays

\tikzset{>=latex} % for LaTeX arrow head
\colorlet{mywhite}{white!80!black}
\colorlet{myred}{red!80!black}
\colorlet{myblue}{blue!80!black}
\colorlet{mygreen}{green!60!black}
\colorlet{mydarkred}{myred!40!black}
\colorlet{mydarkblue}{myblue!40!black}
\colorlet{mydarkgreen}{mygreen!40!black}
\tikzstyle{node}=[very thick,circle,draw=myblue,minimum size=22,inner sep=0.5,outer sep=0.6]
\tikzstyle{connect}=[->,thick,mydarkblue,shorten >=1]
\tikzset{ % node styles, numbered for easy mapping with \nstyle
  node 1/.style={node,mydarkgreen,draw=mywhite,fill=mywhite!25},
  node 2/.style={node,mydarkblue,draw=mywhite,fill=mywhite!20},
  node 3/.style={node,mydarkred,draw=mywhite,fill=mywhite!20},
}
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3

\begin{document}


% NEURAL NETWORK
\begin{tikzpicture}[x=2.4cm,y=1.2cm]

  \readlist\Nnod{4,3,2} % array of number of nodes per layer
  \readlist\Nstr{n,m,k} % array of string number of nodes per layer
  \readlist\Cstr{x,h^{(\prev)},y} % array of coefficient symbol per layer
  \def\yshift{0.55} % shift last node for dots
  
  % LOOP over LAYERS
  \foreachitem \N \in \Nnod{
    \def\lay{\Ncnt} % alias of index of current layer
    \pgfmathsetmacro\prev{int(\Ncnt-1)} % number of previous layer
    \foreach \i [evaluate={\c=int(\i==\N); \y=\N/2-\i-\c*\yshift;
                 \x=\lay; \n=\nstyle;
                 \index=(\i<\N?int(\i):"\Nstr[\n]");}] in {1,...,\N}{ % loop over nodes
      % NODES
      \node[node \n] (N\lay-\i) at (\x,\y) {$\strut\Cstr[\n]_{\index}$};
      
      % CONNECTIONS
      \ifnumcomp{\lay}{>}{1}{ % connect to previous layer
        \foreach \j in {1,...,\Nnod[\prev]}{ % loop over nodes in previous layer
          \draw[white,line width=1.2,shorten >=1] (N\prev-\j) -- (N\lay-\i);
          \draw[connect] (N\prev-\j) -- (N\lay-\i);
        }
        \ifnum \lay=\Nnodlen
          \draw[connect] (N\lay-\i) --++ (0.5,0); % arrows out
        \fi
      }{
        \draw[connect] (0.5,\y) -- (N\lay-\i); % arrows in
      }
      
    }
    \path (N\lay-\N) --++ (0,1+\yshift) node[midway,scale=1.6] {$\vdots$}; % dots
  }
  
  % LABELS
  \node[above=3,align=center,mydarkgreen] at (N1-1.90) {Input\\[-0.2em]layer};
  \node[above=2,align=center,mydarkblue] at (N2-1.90) {Hidden\\[-0.2em]layers};
  \node[above=3,align=center,mydarkred] at (N\Nnodlen-1.90) {Output\\[-0.2em]layer};
  
  
  \begin{scope}[scale=0.75,xshift=18cm,yshift=-2cm] % <---- HERE
    \draw[blue!50!black] plot[domain={-1.5:5}] (\x,{sin(\x^2 r)});
    \draw[blue!50!black, densely dotted] (-2,0) -- (5,0);
    \draw[black, densely dashed] (0,0) 
        -- ++(0,-2) node[below] {$x_1$} coordinate[pos=0] (A);
    \draw[black, densely dashed] (0.5,0) 
        -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.5,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (2.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);

    \draw[thick, <->] (-2,4) 
        -- node[left, midway, anchor=south, rotate=90, font=\bfseries] (Y) {$f(x)$} (-2,-2)
        -- node[below, pos=1, font=\bfseries] {Quad. points} (5,-2);
  \end{scope}
  
\end{tikzpicture}


\end{document}
7
  • 2
    Welcome to TeX.SX! You seem to be asking two [or more] unrelated questions, here. On TeX.SX, we try to keep unrelated questions on separate pages. If you have multiple questions that are unrelated to one another, you should ask each in a separate TeX.SX "question". You'll stand a better chance of getting a satisfactory answer to each of your questions.
    – dexteritas
    Commented Jan 20, 2023 at 13:37
  • 1
    For an example of MISO neural network see tex.stackexchange.com/questions/365404. From it to your desired look is just a step: some nodes replace with vertical dots ...
    – Zarko
    Commented Jan 20, 2023 at 13:38
  • @dexteritas Thanks a lot for your comments. Sure, I'll try to modify and make the question well. Commented Jan 20, 2023 at 19:54
  • @dexteritas I have now updated the question. Hope it becomes more specific and related to 1 topic only. Thanks a lot for your advice, I'll keep this in mind. Commented Jan 21, 2023 at 3:32
  • 1
    You could reduce your code to the part that is relevant for your question (remove the right part of the figure) and update the screenshots.
    – dexteritas
    Commented Jan 21, 2023 at 10:39

1 Answer 1

3
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{positioning, calc, arrows.meta}
\begin{document}
\begin{tikzpicture}[
state/.style={draw, circle, minimum size=0.8cm, inner sep=0pt},
arrow/.style={-Stealth, shorten >=1pt},
dot/.style={minimum size=4pt, inner sep=0pt, rounded corners=1pt, fill},
node distance=0.1cm and 2cm,
]
\node[state] (y1) {$y_1$};
\node[state, below=of y1] (y2) {$y_2$};
\node[state, below=0.8cm of y2] (yn) {$y_n$};
\node[state, below=of yn] (b) {$b$};
\node[state, above right=of y1] (a11) {$a_{11}$};
\node[state, below=of a11] (a21) {$a_{21}$};
\node[state, below right=of yn] (an1) {$a_{n1}$};
\node[state, below=of an1] (b1) {$b_1$};
\node[state, right=of a11] (ak1) {$a_{k1}$};
\node[state, right=of a21] (ak2) {$a_{k2}$};
\node[state, right=of an1] (ann) {$a_{nn}$};
\node[state, right=of b1] (bn) {$b_n$};
\node[state] at ($(ak2)!0.5!(ann)+(2,0)$) (sigma) {$\Sigma$};
\node[dot] at ($(a21)!0.3!(an1)$) {};
\node[dot] at ($(a21)!0.5!(an1)$) {};
\node[dot] at ($(a21)!0.7!(an1)$) {};
\node[dot] at ($(ak2)!0.3!(ann)$) {};
\node[dot] at ($(ak2)!0.5!(ann)$) {};
\node[dot] at ($(ak2)!0.7!(ann)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.3!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.5!($(an1)!0.5!(ann)$)$) {};
\node[dot] at ($($(a21)!0.5!(ak2)$)!0.7!($(an1)!0.5!(ann)$)$) {};
\node[dot] (dot1) at ($(a11)!0.5!(ak1)$) {};
\node[dot] (dot2) at ($(b1)!0.5!(bn)$) {};
\draw[arrow] (a11) -- (dot1) edge (ak1);
\draw[arrow] (b1) -- (dot2) edge (bn);
\draw[arrow] (y1) --node[above, font=\small]{$w_{11}^1$} (a11);
\draw[arrow] (y1) -- (a21);
\draw[arrow] (y2) --node[above, font=\small]{$w_{11}^1$} (a21);
\draw[arrow] (y2) -- (a21);
\draw[arrow] (y2) -- (an1);
\draw[arrow] (yn) -- (a11);
\draw[arrow] (yn) --node[above, font=\small]{$w_{11}^1$} (an1);
\draw[arrow] (yn) --node[above, font=\small]{$w_{n1}^1$} (b1);
\draw[arrow] (b) -- (a11);
\draw[arrow] (b) -- (an1);
\draw[arrow] (b) -- (b1);
\draw[arrow] (ak1) -- (sigma);
\draw[arrow] (ak2) -- (sigma);
\draw[arrow] (ann) -- (sigma);
\draw[arrow] (bn) -- (sigma);
\draw[arrow] (sigma) -- +(1,0)node[right]{$I$};
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thank you for the help. Commented Jan 21, 2023 at 19:09

You must log in to answer this question.

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