如何将单个注释设为“隐藏层”,而不是“隐藏层 1”、“隐藏层 2”等

如何将单个注释设为“隐藏层”,而不是“隐藏层 1”、“隐藏层 2”等

所以我想注释“隐藏层位于我的两个隐藏层的上方和中心,但我没有用我的代码实现它:

        \documentclass{article}
    \usepackage[utf8]{inputenc}     % Codificacao do documento (conversão automática dos acentos)
    \usepackage{tikz}
    \begin{document}
    \pagestyle{empty}

    \def\layersep{2.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=17pt,inner sep=0pt]
    \tikzstyle{input neuron}=[neuron, fill=gray!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=cyan!65];
    \tikzstyle{annot} = [text width=5em, text centered]

        % Draw the input layer nodes
        \foreach \name / \y in {1,...,2}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
        \node[input neuron, pin=left:Entrada $x_{\y}$] (I-\name) at (0,-\y) {$x_{\y}$};

        % set number of hidden layers
         \newcommand\Nhidden{2}

        % Draw the hidden layer nodes
        \foreach \N in {1,...,\Nhidden} {
        \foreach \y in {1,...,3}
            \path[yshift=0.5cm]
            node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
            ;
        \node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Camada \\ intermediária \N};
    }

        % Draw the output layer node
        \foreach \name / \y in {1}
                    \path[yshift=-0.5cm]
            node[output neuron,pin={[pin edge={->}]right:Saída {$y_{\y}$}}, right of=H\Nhidden-3] (O-\name) at (\Nhidden*\layersep,-\y cm) {$y_{\y}$};

        % Connect every node in the input layer with every node in the
        % hidden layer.
        \foreach \source in {1,...,2}
        \foreach \dest in {1,...,3}
        \path (I-\source) edge (H1-\dest);

            % connect all hidden stuff
        \foreach [remember=\N as \lastN (initially 1)] \N in {\Nhidden}
        \foreach \source in {1,...,3}
        \foreach \dest in {1,...,3}
        \path (H\lastN-\source) edge (H\N-\dest);

        % Connect every node in the hidden layer with the output layer
        \foreach \source in {1,...,3}
        \foreach \dest in {1}
        \path (H\Nhidden-\source) edge (O-\dest);

        % Annotate the layers
        \node[annot,left of=hl1] {Camada de entrada};
        \node[annot,right of=hl\Nhidden] {Camada de saída};
    \end{tikzpicture}
    % End of code
    \end{document}

答案1

首先,删除/注释掉生成“隐藏层”标签的行。

在图的末尾,即制作输入/输出标签的地方,将代码修改为以下内容:

    \path (H1-1) -- node[above=5mm] (hl) {Hidden layers} (H2-1);
    \node[annot] at (hl-|I-1) {Camada de entrada};
    \node[annot] at (hl-|O-1) {Camada de saída};

第一行将一个节点放置在每个隐藏层中第一个节点中间点上方 5mm 的位置。对于最后两行,坐标指定为(hl -| I-1)表示 y 坐标为hl、 x 坐标为 的点I-1

不相关的注释:\tikzstyle我认为已被弃用,因此我将样式规范移至环境的参数tikzpicture

代码输出

\documentclass{article}
\usepackage[utf8]{inputenc}     % Codificacao do documento (conversão automática dos acentos)
\usepackage{tikz}
\begin{document}
\pagestyle{empty}

\def\layersep{2.5cm}

\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, fill=gray!50},
    output neuron/.style={neuron, fill=red!50},
    hidden neuron/.style={neuron, fill=cyan!65},
    annot/.style={text width=5em, text centered}
]

    % Draw the input layer nodes
    \foreach \name / \y in {1,...,2}
    % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
    \node[input neuron, pin=left:Entrada $x_{\y}$] (I-\name) at (0,-\y) {$x_{\y}$};

    % set number of hidden layers
     \newcommand\Nhidden{2}

    % Draw the hidden layer nodes
    \foreach \N in {1,...,\Nhidden} {
    \foreach \y in {1,...,3}
        \path[yshift=0.5cm]
        node[hidden neuron] (H\N-\y) at (\N*\layersep,-\y cm) {};
        ;
%    \node[annot,above of=H\N-1, node distance=1cm] (hl\N) {Camada \\ intermediária \N};
}

    % Draw the output layer node
    \foreach \name / \y in {1}
                \path[yshift=-0.5cm]
        node[output neuron,pin={[pin edge={->}]right:Saída {$y_{\y}$}}, right of=H\Nhidden-3] (O-\name) at (\Nhidden*\layersep,-\y cm) {$y_{\y}$};

    % Connect every node in the input layer with every node in the
    % hidden layer.
    \foreach \source in {1,...,2}
    \foreach \dest in {1,...,3}
    \path (I-\source) edge (H1-\dest);

        % connect all hidden stuff
    \foreach [remember=\N as \lastN (initially 1)] \N in {\Nhidden}
    \foreach \source in {1,...,3}
    \foreach \dest in {1,...,3}
    \path (H\lastN-\source) edge (H\N-\dest);

    % Connect every node in the hidden layer with the output layer
    \foreach \source in {1,...,3}
    \foreach \dest in {1}
    \path (H\Nhidden-\source) edge (O-\dest);

    % Annotate the layers
    \path (H1-1) -- node[above=5mm] (hl) {Hidden layers} (H2-1);
    \node[annot] at (hl-|I-1) {Camada de entrada};
    \node[annot] at (hl-|O-1) {Camada de saída};
\end{tikzpicture}
% End of code
\end{document}

相关内容