神经网络中的中心/对齐输入节点

神经网络中的中心/对齐输入节点

我有一个神经网络图,但是,正如您所看到的,输入节点并不以隐藏层和输出为中心。

  1. 如何对齐输入节点?
  2. 如何在节点内添加符号(即 X1、X2、X3)。 在此处输入图片描述

我的 MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usetikzlibrary{matrix}

\begin{document}

\begin{figure}[tbp!]
\begin{tikzpicture}[
plain/.style={
  draw=none,
  fill=none,
  },
net/.style={
  matrix of nodes,
  nodes={
    draw,
    circle,
    inner sep=10pt
    },
  nodes in empty cells,
  column sep=2cm,
  row sep=-9pt
  },
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1.3cm}{\centering Input\\layer} & |[plain]| \parbox{1.3cm}{\centering Hidden\\layer} & |[plain]| \parbox{1.3cm}{\centering Output\\layer} \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
|[plain]| & |[plain]| \\
& & \\
|[plain]| & |[plain]| \\
  |[plain]| \\
|[plain]| & \\
  |[plain]| \\
};
\foreach \ai [count=\mi ]in {2,4,...,6}
  \draw[<-] (mat-\ai-1) -- node[above] {Input \mi} +(-2cm,0);
\foreach \ai in {2,4,...,6}
{\foreach \aii in {3,6,9}
  \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai in {3,6,9}
  \draw[->] (mat-\ai-2) -- (mat-6-3);
\draw[->] (mat-6-3) -- node[above] {Ouput} +(2cm,0);
\end{tikzpicture}
\end{figure}
\end{document}

答案1

  1. 您将节点错误地放入矩阵中。
  2. {<text>}只需通过添加到适当的矩阵单元即可在节点内添加文本。

    \begin{tikzpicture}[plain/.style={draw=none,fill=none},
                        net/.style={matrix of nodes,
                            nodes={draw,circle,inner sep=10pt},
                            nodes in empty cells,
                            column sep=2cm,
                            row sep=-9pt},
                        >=latex]
    
        \matrix[net] (mat) {
            |[plain]| \parbox{1.3cm}{\centering Input\\layer}   &
            |[plain]| \parbox{1.3cm}{\centering Hidden\\layer}  &
            |[plain]| \parbox{1.3cm}{\centering Output\\layer}  \\
            |[plain]|   \\
            |[plain]|   &   \\
            {$x_1$} &   |[plain]|   \\
            |[plain]|   &   |[plain]|   \\
            {$x_2$} &   &   \\
            |[plain]|   \\
            {$x_3$} &   |[plain]|   \\
            |[plain]|   &   \\
            |[plain]|   \\
        };
    
        \foreach \ai [count=\mi ]in {4,6,8}
            \draw[<-] (mat-\ai-1) -- node[above] {Input \mi} +(-2cm,0);
        \foreach \ai in {4,6,8}{
            \foreach \aii in {3,6,9}
                  \draw[->] (mat-\ai-1) -- (mat-\aii-2);
        }
    
        \foreach \ai in {3,6,9}
            \draw[->] (mat-\ai-2) -- (mat-6-3);
    
        \draw[->] (mat-6-3) -- node[above] {Ouput} +(2cm,0);
    \end{tikzpicture}
    

结果

在此处输入图片描述

答案2

这并不能直接回答你的问题。但对于正在寻找类似图表的乳胶代码的人来说,这可能有所帮助。

输出

\documentclass{article}

\usepackage{tikz}
\usepackage{verbatim}

\begin{comment}
:Title: Neural network
:Tags: Foreach

The ``\foreach`` command is very useful for quickly creating structured graphics
like this neural network diagram.

\end{comment}

\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=green!50];
    \tikzstyle{output neuron}=[neuron, fill=red!50];
    \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
    \tikzstyle{annot} = [text width=4em, text centered]

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

    % Draw the hidden layer nodes
    \foreach \name / \y in {1,...,5}
        \path[yshift=0.5cm]
            node[hidden neuron] (H-\name) at (\layersep,-\y cm) {};

    % Draw the output layer node
    \node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};

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

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

    % Annotate the layers
    \node[annot,above of=H-1, node distance=1cm] (hl) {Hidden layer};
    \node[annot,left of=hl] {Input layer};
    \node[annot,right of=hl] {Output layer};
\end{tikzpicture}
% End of code
\end{document}

来源

相关内容