2

I know there are already some questions about drawing a neural network in here, but I'm trying to draw something like this : enter image description here

I have tried to apply some tutos and modified on some but this is my result, it's still so far away from the one above. enter image description here

and this is my code:

\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}
\begin{document}
\def\layersep{5cm}
\begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
 \tikzstyle{every pin edge}=[<-,shorten <=1pt]
 \tikzstyle{neuron}=[circle,fill=black!25,minimum size=25pt,inner sep=0pt]
 \tikzstyle{Cauche d'entrée}=[neuron, fill=gray!33];
 \tikzstyle{Cauche cachée}=[neuron, fill=gray!66];
 \tikzstyle{Cauche de sortie}=[neuron, fill=gray!99];
 \tikzstyle{annot} = [text width=6em, text centered]

 % Draw the rectangle
\filldraw[fill=gray!33!white, draw=black] (0,0) rectangle (1.4,6);
 % Draw the input layer nodes
 \foreach \name / \y in {1,2,3,4,5,6}
 % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
 \node[Cauche d'entrée] (I-\name) at (0,-\y) {};

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

 % Draw the output layer node
 \node[Cauche de sortie, pin={[pin edge={->}]right:Prévision}, right of=H-3] (O) {};

 % Connect every node in the input layer with every node in the
 % hidden layer.
 \foreach \source in {1,...,6}
 \foreach \dest in {1,2,3,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 (O);

 % Annotate the layers
 \node[annot,above of=H-1, node distance=1cm] (hl) {Cauche cachée};
 \node[annot,left of=hl] {Cauche d'entrée};
 \node[annot,right of=hl] {Cauche de sortie};
 \end{tikzpicture}
 \end{document}

As you can see, I'm so far away from what I want to draw. Any help or advice would be appreciated.

5

as starting point:

enter image description here

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                calc, chains,
                decorations.pathreplacing,
                calligraphy,% had to be after decorations.pathreplacing
                positioning}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}

\begin{document}
    \begin{tikzpicture}[
             font = \sffamily,
        shorten > = 1pt,
                > = Stealth,
    node distance = 6mm and 18mm,
      start chain = going below,
 every pin/.style = {pin distance=11mm, pin edge={thin, black, ->}},
    neuron/.style = {circle, draw, fill=#1,
                     minimum size=17pt, inner sep=1pt,
                     },
     annot/.style = {text width=6em, align=center},
        BC/.style = {decorate,
                            decoration={calligraphic brace, amplitude=4pt,
                            pre =moveto, pre  length=1pt,
                            post=moveto, post length=1pt,
                            raise=6mm, mirror},
                            thick,
                            pen colour={red}
                         }
                        ]
% input variables
\foreach \i in {1,...,9}
{
 \ifnum\i=5
    \node[neuron=white,draw=none,on chain]   (in-\i)        {$\vdots$};
 \else
     \ifnum\i<9
        \node[neuron=white,on chain]             (in-\i)    {};
     \else
        \node[neuron=white,draw=none,on chain]   (in-\i)    {$\vdots$};
     \fi
 \fi
}
% Draw the rectangle
\path   let \p1 = ($(in-1.north) - (in-9.south)$),
            \n1 = {veclen(\y1,\x1)} in
        node (r) [minimum height=\n1,
                  minimum width=9mm, draw, fill=gray!10,
                  below right=0mm and 18 mm of in-1.north]
                  {\rotatebox{90}{Variables selection}};
% input layer neurons
\node[neuron=gray!22,right=of in-1 -| r.east] (I-1) {};
\node[neuron=gray!22,right=of in-3 -| r.east] (I-2) {};
\node[neuron=gray!22,right=of in-6 -| r.east] (I-3) {};
\node[neuron=gray!22,right=of in-7 -| r.east] (I-4) {};
% hiden layer neurons
\node[neuron=gray!44,right=of in-2 -| I-1.east] (H-1) {};
\node[neuron=gray!44,right=of in-5 -| I-1.east] (H-2) {};
\node[neuron=gray!44,right=of in-8 -| I-1.east] (H-3) {};
% out layer neuron
\node[neuron=gray!66,right=of H-2,pin=right:Prévision] (out) {};
% bias nodes
\path (I-1) -| (H-1) node (B1) [pos=0.25,neuron=blue!33] {Bias};
\path (I-1 -| H-1) -| (out) node (B2) [pos=0.25,neuron=blue!33] {Bias};
% Annotations of the layers
\node[annot,above=of in-1]                  {pre-input layer};
\node[annot,above=of I-1]                   {Cauche d'entrée};
\node[annot,above=of I-1.north -| H-1]      {Cauche cachée};
\node[annot,above=of I-1.north -| out]    {Cauche de sortie};

% input labels
\draw[BC] (in-1.north) --
    node[left=11mm,align=right] {xxx\\ yyy\\ zzz} (in-5.south) ;
\draw[BC] (in-6.north) --
    node[left=11mm,align=right] {xxx\\ yyy\\ zzz} (in-9.south);
% nodes connections
% input
\foreach \i in {1,...,4,6,7,8}
    {\draw[->]   (in-\i) -- (in-\i -| r.west);}
\foreach \i in {1,3,6,7}
    {\draw[->]   (in-\i) -- (in-\i -| r.west);}
% input layer
\foreach \i in {1,...,4}
    {\draw[->]   (I-\i -| r.east) -- (I-\i);}
% hidden layer
\foreach \i in {1,...,4}
    \foreach \j in {1,...,3}
    \draw[->] (I-\i) edge (H-\j);
% output layer
 \foreach \i in {1,2,3}
    \draw[->] (H-\i) edge (out);
% bias connection.
 \foreach \i in {1,2,3}
    \draw[->,dashed,blue] (B1) edge (H-\i);
    \path[->,dashed,blue] (B2) edge (out);
   \end{tikzpicture}
\end{document}

i try to reproduce showed image ... but probably in image are missing some element(s), for example text on the input side, etc. this should not be big problem to add to code :)

note, that above mwe use recent tikz syntax, not obsolete one as it is used in your mwe.

  • thank you so much @Zarko, I learned a lot from your code, and you are actually a life saver, I have been trying to reproduce that for a long time, and reading about tikz, because I'm still a beginner to it, obviously I'll be drawing a lot of graphs using it in my articles, based on your experience can you please recommend me with a good reference(s) that can give me a good start? – Hamid701 Aug 8 '17 at 9:43
  • @Hamid701, instead of saying "thank you" vote for answer and even accept it (by click up-pen and/or check mark on the top left side of answer) :). literature: tikz & pgf manual (it is huge, but for start should be sufficient to read tutorials and then part III), it is part of your pgf installation, then examples here on site. for graphs can be handy pgfplots package and of course learn-by-doing. when you stuck in it, ask here. happy tex-ing. – Zarko Aug 8 '17 at 10:51
  • yeah I just did that, but it says that I can't vote yet till I get 15 in reputation, I'm kinda new here :), thank you again I'll follow what you said. – Hamid701 Aug 9 '17 at 10:00

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.