Tikz:如何连接此图中的最终层(节点)?

Tikz:如何连接此图中的最终层(节点)?

我有一个神经网络,我正在尝试连接图中的最后几层。我尝试了几种方法,但到目前为止都没有奏效。此外,我在上部节点中为术语 B 加下标时遇到了问题,它们应该是 B_1、B_2 和 B_3 。如果您能给我提供任何帮助,我将不胜感激。

以下是我目前所掌握的信息:

\documentclass[11pt]{report}
\def\layersep{3cm}
\def\nodeinlayersep{1.5cm}
\begin{document}
   \begin{tikzpicture}[
   shorten >=1pt,->,
   draw=black!50,
    node distance=\layersep,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron,draw,circle, fill=white!50,},
    output neuron/.style={neuron,draw,circle, fill=white!50},
    hidden neuron/.style={neuron,draw,circle, fill=white!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron,draw,circle, fill=white!50},%<-- added %%%
]
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,3}
    \node[input neuron] (I-\name) at (0,-\y-2.5) {$x_\y$};  
    % 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 ) {$B_{\pgfmathparse{\N+1}}$}; %<-- added
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
           \node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {}; %<-- 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
    \foreach \name / \y in {1,2}
    \node[output neuron] (O-\name) at (3*\layersep,-\y-2.5)  {};  
    % set number of hidden layers


    % 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,black] (H0-0) edge (H1-\dest); %<-- added %%%%%
            \path[black] (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,black](H\lastN-\source) edge (H\N-\dest);%<-- added 
              \else %<-- added %%%
              \path[black] (H\lastN-\source) edge (H\N-\dest);%<-- added 
              \fi %<-- added %%%
              }; %<-- added %%%%

    % Connect every node in the hidden layer with the output layer


    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
\end{document}



答案1

这是一个最小损害的提议。\pgfmathparse仅解析,您可以\the\numexpr\N+1在这里使用。

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[
   shorten >=1pt,->,
   draw=black!50,
    node distance=\layersep,
    every pin edge/.style={<-,shorten <=1pt},
    neuron/.style={circle,fill=black!25,minimum size=17pt,inner sep=0pt},
    input neuron/.style={neuron,draw,circle, fill=white!50,},
    output neuron/.style={neuron,draw,circle, fill=white!50},
    hidden neuron/.style={neuron,draw,circle, fill=white!50},
    annot/.style={text width=4em, text centered},
    bias/.style={neuron,draw,circle, fill=white!50},%<-- added %%%
]
    \def\layersep{3cm}
    \def\nodeinlayersep{1.5cm}
    % Draw the input layer nodes
    \foreach \name / \y in {1,...,3}
    \node[input neuron] (I-\name) at (0,-\y-2.5) {$x_\y$};  
    % 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 )
           {$B_{\the\numexpr\N+1}$}; %<-- added
           \else\fi %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         \else %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \ifnum \N>0 %<-- added %%%%%%%%%%%%%%%%%%%%%%%%%
           \node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y*\nodeinlayersep ) {}; %<-- 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
    \foreach \name / \y in {1,2}
    \node[output neuron] (O-\name) at (3*\layersep,-\y-2.5)  {};  
    % set number of hidden layers


    % 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,black] (H0-0) edge (H1-\dest); %<-- added %%%%%
            \path[black] (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,black](H\lastN-\source) edge (H\N-\dest);%<-- added 
              \else %<-- added %%%
              \path[black] (H\lastN-\source) edge (H\N-\dest);%<-- added 
              \fi %<-- added %%%
              }; %<-- added %%%%

    % Connect every node in the hidden layer with the output layer
    \foreach \X in {1,2}
    {\foreach \source in {0,...,3,5}
    \path[black] (H\Nhidden-\source) edge (O-\X);}

    % Annotate the layers
    \node[annot,left of=hl1] {Input layer};
    \node[annot,right of=hl\Nhidden] {Output layer};  
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果你将最后一个循环替换为

\foreach \X in {1,2}
{\foreach \source in {0,...,3,5}
\path[black] \ifnum\source=0
 [dashed]
\fi
(H\Nhidden-\source) edge (O-\X);}

你得到

在此处输入图片描述

相关内容