0

How can I draw this ?

enter image description here

A related diagram (more complicated) is here Tikz draw neural network outline

I did tried to modify the code provided by @Zarco but I just could modify the nodes. I end up really confused with the arrows.

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

    \begin{document}
        \begin{tikzpicture}[shorten >=1pt,->, draw=black!50,
            node distance = 6mm and 15mm,
              start chain = going below,
    every pin edge/.style = {<-,shorten <=1pt},
            neuron/.style = {circle, draw=black, fill=#1,   % <--- changed
                             minimum size=17pt, inner sep=0pt,
                             on chain},
             annot/.style = {text width=4em, align=center}
                            ]
    % Draw the input layer nodes
    \foreach \i in {1,...,3}
        \node[neuron=green!50,
              pin=180:] (I-\i)    {};
    % Draw the hidden layer nodes
        \node[neuron=blue!50,
              above right=6mm and 15mm of I-1.center] (H-1)     {$x_{1}$};
    \foreach \i [count=\j from 1] in {2}
        \node[neuron=blue!50,
              below=of H-\j]      (H-\i)    {$x_{\i}$};
    % Draw the output layer node
        \node[neuron=red!50,
              pin= {[pin edge=->]0:Output \#1},
              right=of I-2 -| H-1]  (O-1)   {$x_{1}$};
        \node[neuron=red!50,                            % <--- changed
              pin= {[pin edge=->]0:Output \#2},
              below=of O-1]         (O-2)   {$x_{2}$};  % <--- changed
\node[neuron=red!50,
              pin= {[pin edge=->]0:Output \#3},
              below=of 0-2]  (O-3)   {$x_{1}$};

    % Connect input nodes with hidden nodes and
    %  hiden nodes with output nodes with the output layer
        \foreach \i in {1,...,4}
            \foreach \j in {1,...,5}
    {
        \path (I-\i) edge (H-\j)
        \ifnum\i<3                  % <--- changed
              (H-\j) edge (O-\i)
        \fi;
    }
        \end{tikzpicture}
    \end{document}  

Thank you in advance.

1 Answers1

4

The chains library is a powerful tool, but maybe not really suited for such diagrams. This is an attempt to make things more automatic and accessible. Please read the annotations in the code. The inputs and outputs are stored in lists, and so are the numbers of neurons.

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{quotes,positioning}
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,->, draw=black!50,>=stealth,
        every pin edge/.style = {<-,shorten <=1pt},
        every pin/.style={font=\small\sffamily,text=gray,inner sep=1pt},
        neuron/.style = {circle, draw=black, fill=#1,
                         minimum size=20pt, inner sep=0pt},
        neuron/.default=white,               
        layer 1/.style={neuron=green!50,pin={[alias=el1-\i]180:\myedgelabel}},
        layer 2/.style={neuron=blue!50},
        layer 3/.style={neuron=red!50,pin={[pin edge={->},alias=el3-\i]0:\myedgelabel}},
         annot/.style = {text width=4em, align=center},
         neuron list/.store in=\NeuronList,
         neuron list={3,2,3}, %<- # of neurons at each layer
         input list/.store in=\InputList,
         input list={200,150,200},%<- inputs
         output list/.store in=\OutputList,
         output list={200,250,100},%<- outputs
                        ]
% Draw the input layer nodes
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[0]}
 \path foreach \i in {1,...,\imax}
    {(0,{(\imax/2-1/2-\i)*1.5})
    [/utils/exec=\pgfmathsetmacro{\myedgelabel}{{\InputList}[\i-1]}]
     node[layer 1] (P-\i)    {$P_\i$}};  
% Draw the hidden layer nodes
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[1]}
 \path foreach \i in {1,...,\imax}
    {(25mm,{(\imax/2-1/2-\i)*1.5}) node[layer 2] (T-\i)    {$T_\i$}};  
% % Draw the output layer node
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[2]}
 \path foreach \i in {1,...,\imax}
    {(60mm,{(\imax/2-1/2-\i)*1.5})
    [/utils/exec=\pgfmathsetmacro{\myedgelabel}{{\OutputList}[\i-1]}] 
        node[layer 3] (D-\i)    {$D_\i$}};  
% edges
  \path[nodes={text=red,font=\small\sffamily},->] 
   (P-1) edge["1"] (T-1)
   (P-1) edge["4"' {pos=0.2}] (T-2) %<- a prime swaps the label position
   (P-2) edge["2" {pos=0.15}] (T-2) %<- with pos you can control the position
   (P-3) edge["2"' {pos=0.3}] (T-1)
   (P-3) edge["3"] (P-2)
   (T-1) edge["2"] (D-1)
   (T-1) edge["3"] (D-2)
   (D-1) edge["2"] (D-2)
   (T-2) edge["1"] (D-2)
   (T-2) edge["3"] (D-3);
% extra 
  \begin{scope}[node distance=0pt,nodes={font=\small\sffamily,color=blue}]
   \node[above=of P-1]{(pure)};   
   \node[below=of P-3]{(pure)};   
   \node[below=of el3-2]{(pure)};   
  \end{scope}
\end{tikzpicture}
\end{document}  

enter image description here

  • Thank you Schrödinger's cat :) – I likeThatMeow Nov 27 '19 at 18:25
  • The color in the circles was not necessary (although it looks better). Is it possible to write the label "(pure)" below some of the numbers? If it's too complicated, then its fine as it is. – I likeThatMeow Nov 27 '19 at 18:27
  • @user178403 It is very easy to add these labels, I added a few examples. I changed the definition of neuron in such a way that dropping the fill colors is as easy as dropping =green!50 in neuron=green!50. –  Nov 27 '19 at 18:49
  • I see, thank you again. – I likeThatMeow Nov 27 '19 at 19:03