0

I want to add text where I put the dots, for example w_1, w_2, ... w_n. This is the code:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}

\tikzset{%
   neuron missing/.style={
    draw=none, 
    scale=2,
    text height=0.333cm,
    execute at begin node=\color{black}$\vdots$
  },
}

\newcommand{\DrawNeuronalNetwork}[2][]{
\xdef\Xmax{0}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count [count=\Y] in {#2}
{\pgfmathsetmacro{\Xmax}{max(\X,\Xmax)}
 \xdef\Xmax{\Xmax}
 \xdef\Ymax{\Y}
}
\foreach \Layer/\X/\Col/\Miss/\Lab/\Count [count=\Y] in {#2}
{\node[anchor=south] at ({2*\Y},{\Xmax/2+0.1}) {\Layer};
 \foreach \m in {1,...,\X}
 {
  \ifnum\m=\Miss
   \node [neuron missing] (neuron-\Y-\m) at ({2*\Y},{\X/2-\m}) {};
  \else
   \node [circle, draw=black, thick, inner sep=0pt,minimum size=1cm] (neuron-\Y-\m) at 
  ({2*\Y},{\X/2-\m}) {};
 \ifnum\Y=1
  \else
   \pgfmathtruncatemacro{\LastY}{\Y-1}
   \foreach \Z in {1,...,\LastX}
   {
    \ifnum\Z=\LastMiss
    \else
     \draw[->] (neuron-\LastY-\Z) -- (neuron-\Y-\m);
    \fi
    }
  \fi
 \fi
 \ifnum\Y=1
  \ifnum\m=\X
   \draw [<-] (neuron-\Y-\m) -- ++(-1,0) node [above, midway] {$\Lab_{\Count}$};
  \else
   \ifnum\m=\Miss
   \else
    \draw [<-] (neuron-\Y-\m) -- ++(-1,0) node [above, midway] {$\Lab_{\m}$};
   \fi
  \fi
 \else
   \ifnum\Y=\Ymax
    \ifnum\m=\X
     \draw [->] (neuron-\Y-\m) -- ++(1,0) node [above, midway] {$\Lab_{\Count}$};
    \else
     \ifnum\m=\Miss
     \else
      \draw [->] (neuron-\Y-\m) -- ++(1,0) node [above, midway] {$\Lab_{\m}$};
     \fi
    \fi
   \else
     \ifnum\m=1
      \node[above=0pt of neuron-\Y-\m] {$\Lab_1$};
     \fi
     \ifnum\m=\X
      \node[below=0pt of neuron-\Y-\m] {$\Lab_{\Count}$};
     \fi
   \fi
 \fi     
 }
 \xdef\LastMiss{\Miss}
 \xdef\LastX{\X}
}
}
\begin{tikzpicture}[x=2.5cm, y=1.5cm, >=stealth,font=\sffamily]
\DrawNeuronalNetwork{
Capa de entrada/1/black/8/x/,
Capa oculta/7/black/6/H/k,
%Bottleneck Layer/3/blue/2/T/f,
%Demapping Layer/7/red/4/G/f,
Capa de salida/1/black/4/y(x)/}
\end{tikzpicture}
\end{document} 

enter image description here

0

1 Answer 1

1

There's a lot happening in your code. Maybe a more TikZ-automatic way to place the nodes could help you.

Here I'm using the chains library to place nodes automatically:

  • Chain main has always only three nodes.
  • From the middle of these three (see start branch 2 style) two branches are started:
    • main/up and
    • main/down.

On main/up we will place up nodes − 1 additional nodes, on main/down the same for down nodes.

For the down branch we define a style down <down nodes - 1> style that sets the \vdots in relation to the total number of down nodes so that it is always the second to last node.

Similar calculations are done in the very last \foreach loop where we want to skip the \vdots node when drawing the arrows.

You can of course – similar on how I added the arrows with x and y(x) – just draw these arrows manually.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, quotes, chains, scopes}
\begin{document}
\begin{tikzpicture}[
  x = 2.5cm, y = 1.5cm,
  node distance = .5cm and 2.5cm,
  %
  vertex/.style={
    shape = circle, draw, inner sep=+0pt, minimum size=1cm,
    node contents=},
  start branch 2/.style={
    insert path={
      {[start branch = up going above]}
      {[start branch = down going below]}}},
  %
  > = {Stealth [sep=2pt 1]},
  start chain = main going right,
  up nodes/.initial = 4,
  down nodes/.initial = 4,
  declare function={
      upnum(\x) = int(-\x + \pgfkeysvalueof{/tikz/up\space nodes}+1);
    downnum(\x) = \x == \pgfkeysvalueof{/tikz/down\space nodes} ? "k"
            : int( \x + \pgfkeysvalueof{/tikz/up\space nodes}-1);
  }
]
\foreach \i in {1, 2, 3}
  \node[on chain, vertex] [start branch \i/.try];

\path (main-1) edge[<-, "$x$" above]   ++ (left:1)
      (main-3) edge[->, "$y(x)$"above] ++ (right:1);

{ [continue branch=up]
  \foreach \i in {2,...,\pgfkeysvalueof{/tikz/up nodes}}
    \node[on chain, vertex]; }
{ [continue branch=down,
   % second to last:
   down \the\numexpr\pgfkeysvalueof{/tikz/down nodes}-1\relax/.style
     = {node contents = $\vdots$, draw = none}]
  \foreach \i in {2,...,\pgfkeysvalueof{/tikz/down nodes}}
    \node[on chain, vertex, down \i/.try]; }

\path (main/up-end)   [late options = {label = above:$H_1$}]
      (main/down-end) [late options = {label = below:$H_k$}];

{ [nodes={node font=\scriptsize, sloped}]
\foreach \t[
  evaluate={\n=upnum \t;}
] in {1,...,\pgfkeysvalueof{/tikz/up nodes}}
  \path[->] (main-1)     edge["$ w _{\n} $"] (main/up-\t)
            (main/up-\t) edge["$ w'_{\n} $"]  (main-3);

\foreach \t[
  expand list,
  evaluate={\n=downnum \t;}
] in {2, ..., % third to last, skip second to last, last
  \the\numexpr\pgfkeysvalueof{/tikz/down nodes}-2\relax,
              \pgfkeysvalueof{/tikz/down nodes}}
  \path[->] (main-1)       edge["$ w _{\n} $"] (main/down-\t)
            (main/down-\t) edge["$ w'_{\n} $"]  (main-3);
}
\end{tikzpicture}
\end{document}

Output

enter image description here

You must log in to answer this question.

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