如何在节点下方添加不同的标签?

如何在节点下方添加不同的标签?

我有以下代码,我想让计数像 1,2,3 然后是 12(神经元的总数)。

    \documentclass{standalone}
    \usepackage{tikz}
    \begin{document}
    \pagestyle{empty}
    \def\layersep{3cm}
    \def\nodeinlayersep{1.5cm}
    \begin{tikzpicture}[
       shorten >=1pt,->,
       draw=black!50,
        node distance=\layersep,
        every pin edge/.style={<-,shorten <=1pt},
        neuron/.style={circle,draw=black!70,fill=white,minimum size=17pt,inner sep=0pt},
        input neuron/.style={neuron, fill=gray!50,},
        output neuron/.style={neuron, fill=gray!50},
        hidden neuron/.style={neuron, fill=blue!50},
        annot/.style={text width=4em, text centered},
        bias/.style={neuron, fill=blue!33,minimum size=2.3em},%<-- added %%%
    ]
        % Draw the input layer nodes
           \foreach \name / \y in {1,...,3} {
        \ifnum \y=1
        \node[input neuron,  pin=left:\textbf{\textit{n}}] (I-\name) at (0,-\y-2.5) {};
        \else
        \ifnum \y=2
        \node[input neuron, pin=left:\textbf{\textit{m}}] (I-\name) at (0,-\y-2.5) {};
        \else
        \ifnum \y=3
        \node[input neuron, pin=left:\textbf{\textit{U}}] (I-\name) at (0,-\y-2.5) {};
        \else\fi
        \fi
        \fi 
       } 
        % set number of hidden layers
        \newcommand\Nhidden{2}

        % Draw the hidden layer nodes
        \foreach \N in {0,...,\Nhidden} {
           \foreach \y in {0,...,5} { % <-- added 0 instead of 1 %%%%%
         \ifnum \y=4
         \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
           \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else
             \ifnum \y=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             \ifnum \N<3 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
               \node[bias] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {Bias}; %<-- added
               \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
               \node[hidden neuron,label=below:$^{\y}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; %<-- added %%%%%%%%%%%
                   \else\fi %<-- added %%%%%%%%%%%%
             \fi %<-- added %%%%%%%
             \fi
        }
           \ifnum \N>0 %<-- added %%%%%%
        \node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; % <- added yshift=2cm %%%%%%%%%%%%
        \else\fi %<-- added %%%%%
        }
        % Draw the output layer node
        \node[output neuron,pin={[pin edge={->}]right:\textbf{\textit{E}}}, right of=H\Nhidden-3] (O) {}; 
        % Connect every node in the input layer with every node in the
        % hidden layer.
        \foreach \source in {1,...,3}
            \foreach \dest in {1,...,3,5} {
              % \path[yellow] (H-0) edge (H1-\dest);
              \path[dashed,blue!50] (H0-0) edge (H1-\dest); %<-- added %%%%%
                \path[black!80] (I-\source) edge (H1-\dest);};

        % connect all hidden stuff
        \foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
           \foreach \source in {0,...,3,5} 
               \foreach \dest in {1,...,3,5}{

                   \ifnum \source=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%
               \path[dashed,blue!80](H\lastN-\source) edge (H\N-\dest);%<-- added 
                  \else %<-- added %%%
                  \path[black!80] (H\lastN-\source) edge (H\N-\dest);%<-- added 
                  \fi %<-- added %%%
                  }; %<-- added %%%%

        % Connect every node in the hidden layer with the output layer
        \foreach \source in {1,...,3,5}
        \path[black!80] (H\Nhidden-\source) edge (O);
        \path[dashed,blue!50] (H2-0) edge (O); %<-- added %%%%
        % Annotate the layers
        \node[annot,left of=hl1] {Input layer};
        \node[annot,right of=hl\Nhidden] {Output layer};  
    \end{tikzpicture}
    % End of code
    \end{document}

我已经尝试使用条件\ifnum y<4\else ifnum y=5但是没有作用。

答案1

我希望我正确理解了你的问题...

\documentclass{standalone}
\usepackage{tikz}
\def\nodenum#1{%
  \ifnum#1=5\relax%
    1000%
  \else%
    #1%
  \fi}%
\begin{document}
\pagestyle{empty}
\def\layersep{3cm}
\def\nodeinlayersep{1.5cm}
\begin{tikzpicture}[
   shorten >=1pt,->,
   draw=black!50,
    node distance=\layersep,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={circle,draw=black!70,fill=white,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron, fill=gray!50,},
    output neuron/.style={neuron, fill=gray!50},
    hidden neuron/.style={neuron, fill=blue!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron, fill=blue!33,minimum size=2.3em},%<-- added %%%
]
    % Draw the input layer nodes
       \foreach \name / \y in {1,...,3} {
    \ifnum \y=1
    \node[input neuron,  pin=left:\textbf{\textit{n}}] (I-\name) at (0,-\y-2.5) {};
    \else
    \ifnum \y=2
    \node[input neuron, pin=left:\textbf{\textit{m}}] (I-\name) at (0,-\y-2.5) {};
    \else
    \ifnum \y=3
    \node[input neuron, pin=left:\textbf{\textit{U}}] (I-\name) at (0,-\y-2.5) {};
    \else\fi
    \fi
    \fi 
   } 
    % set number of hidden layers
    \newcommand\Nhidden{2}

    % Draw the hidden layer nodes
    \foreach \N in {0,...,\Nhidden} {
       \foreach \y in {0,...,5} { % <-- added 0 instead of 1 %%%%%
     \ifnum \y=4
     \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
       \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     \else
         \ifnum \y=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \ifnum \N<3 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
           \node[bias] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {Bias}; %<-- added
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
            \node[hidden neuron,label=below:$^{\nodenum{\y}}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; %<-- added %%%%%%%%%%%
               \else\fi %<-- added %%%%%%%%%%%%
         \fi %<-- added %%%%%%%
         \fi
    }
       \ifnum \N>0 %<-- added %%%%%%
    \node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; % <- added yshift=2cm %%%%%%%%%%%%
    \else\fi %<-- added %%%%%
    }
    % Draw the output layer node
    \node[output neuron,pin={[pin edge={->}]right:\textbf{\textit{E}}}, right of=H\Nhidden-3] (O) {}; 
    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,3}
        \foreach \dest in {1,...,3,5} {
          % \path[yellow] (H-0) edge (H1-\dest);
          \path[dashed,blue!50] (H0-0) edge (H1-\dest); %<-- added %%%%%
            \path[black!80] (I-\source) edge (H1-\dest);};

    % connect all hidden stuff
    \foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
       \foreach \source in {0,...,3,5} 
           \foreach \dest in {1,...,3,5}{

               \ifnum \source=0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%
           \path[dashed,blue!80](H\lastN-\source) edge (H\N-\dest);%<-- added 
              \else %<-- added %%%
              \path[black!80] (H\lastN-\source) edge (H\N-\dest);%<-- added 
              \fi %<-- added %%%
              }; %<-- added %%%%

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,3,5}
    \path[black!80] (H\Nhidden-\source) edge (O);
    \path[dashed,blue!50] (H2-0) edge (O); %<-- added %%%%
    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
% End of code
\end{document}

答案2

我认为你把事情复杂化了。

我将在单独的循环中绘制偏置节点,这样就不需要那么多条件了。绘制隐藏层的循环的伪代码如下:

for i from 1 to <number of hidden layers>
   for j from 1 to 5
      if j = 4
        draw dots
      else
         if j = 5
            draw node with label 12
         else
            draw node with label j

注意:我还更改了绘制输入节点的循环。\ifnum我没有使用三个,而是使用了两个循环变量,\foreach \y/\txt in {1/n,2/u,3/U},并在节点\txt中使用pin

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\def\layersep{3cm}
\def\nodeinlayersep{1.5cm}
\begin{tikzpicture}[
   shorten >=1pt,->,
   draw=black!50,
    node distance=\layersep,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={circle,draw=black!70,fill=white,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron, fill=gray!50,},
    output neuron/.style={neuron, fill=gray!50},
    hidden neuron/.style={neuron, fill=blue!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron, fill=blue!33,minimum size=2.3em},%<-- added %%%
]
    % Draw the input layer nodes
    \foreach \y /\txt in {1/n,2/m,3/U} {
       \node[input neuron, pin=left:\textbf{\textit{\txt}}] (I-\y) at (0,-\y-2.5) {};
   } 

    % set number of hidden layers
    \newcommand\Nhidden{2}
    % draw bias nodes
    \foreach \N in {0,...,\Nhidden}
        \node[bias] (H\N-0) at (\N*\layersep,0) {Bias};

    % Draw the hidden layer nodes
    \foreach \N in {1,...,\Nhidden} {
       \foreach \y in {1,...,5} { 
     \ifnum \y=4
       \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
     \else
        \ifnum \y=5
           \node[hidden neuron,label=below:$^{12}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; 
         \else
           \node[hidden neuron,label=below:$^{\y}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; 
         \fi
    \fi
    }

    \node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; 

    }
    % Draw the output layer node
    \node[output neuron,pin={[pin edge={->}]right:\textbf{\textit{E}}}, right of=H\Nhidden-3] (O) {}; 
    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,3}
        \foreach \dest in {1,...,3,5} {
          \path[dashed,blue!50] (H0-0) edge (H1-\dest); 
            \path[black!80] (I-\source) edge (H1-\dest);};

    % connect all hidden stuff
    \foreach [remember=\N as \lastN (initially 1)] \N in {2,...,\Nhidden}
       \foreach \source in {0,...,3,5} 
          \foreach \dest in {1,...,3,5}{
             \ifnum \source=0
                 \path[dashed,blue!80](H\lastN-\source) edge (H\N-\dest);
             \else 
                 \path[black!80] (H\lastN-\source) edge (H\N-\dest);
            \fi 
          } 

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,3,5}
    \path[black!80] (H\Nhidden-\source) edge (O);
    \path[dashed,blue!50] (H2-0) edge (O); %<-- added %%%%
    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
% End of code
\end{document}

附录

如果你确实想在同一个循环中添加偏差节点,你可以像下面这样进行。伪代码:

for i from 0 to <number of hidden layers>
   draw bias node
   if i > 0
      for j from 1 to 5
         if j = 4
           draw dots
         else
            if j = 5
               draw node with label 12
            else
               draw node with label j

真实代码:

% Draw the hidden layer nodes
\foreach \N in {0,...,\Nhidden} {
\node[bias] (H\N-0) at (\N*\layersep,0) {Bias};
\ifnum \N > 0
\foreach \y in {1,...,5} { 
 \ifnum \y=4
   \node at (\N*\layersep,-\y*\nodeinlayersep) {$\vdots$};
 \else
    \ifnum \y=5
       \node[hidden neuron,label=below:$^{12}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; 
     \else
       \node[hidden neuron,label=below:$^{\y}$] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {$\frac{1}{1+e^{-x}}$}; 
     \fi
\fi
}

\node[annot,above of=H\N-1, node distance=1cm,yshift=2cm] (hl\N) {Hidden layer \N}; 
\fi
}

相关内容