在节点中拟合文本,用于神经网络

在节点中拟合文本,用于神经网络

我正在尝试建立一个神经网络在此处输入图片描述隐藏层中的节点内有一些文本。我的代码在这里,是我在 tex stackexchange 上找到的代码的修改版本:

\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=0.8cm,
  row sep=-12pt
  },
>=latex
]
\matrix[net] (mat)
{
|[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
|[plain]| & |[plain]| \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
|[plain]| & |[plain]| & \\
& |[plain]|  \\
|[plain]| &  \\
& |[plain]| \\
|[plain]| & |[plain]| \\
& |[plain]| \\
|[plain]| & \\
& |[plain]| \\
};
\foreach \ai [count=\mi ]in {2,4,...,16}
  \draw[<-] (mat-\ai-1) -- node[above] {$m_\mi$} +(-1.2cm,0);
\foreach \ai in {2,4,...,16}
{\foreach \aii in {3,7,11,15}
  \draw[->] (mat-\ai-1) -- (mat-\aii-2);
}
\foreach \ai [count=\mi ] in {3,7,11,15}{
  \draw[->] (mat-\ai-2) -- (mat-9-3);
  \node[text width=0.8cm, align=left, scale=0.6] at (mat-\ai-2) {$y=\sigma(\Sigma w_{\mi i}m_i)$} ;
 }
\draw[->] (mat-9-3) -- node[above] {RT} +(1.2cm,0);
\end{tikzpicture}

如何才能将文本放在节点中间而不让它溢出?

答案1

你有两个选择:提前把节点弄大一点,稍后再放置文本,或者让 TiZ 放置文本时,自动调整节点以容纳内容。

第二种可能更直接,也更不容易出错,所以我会选择第二种方案。

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\newcounter{mi}
\newcommand*\mymi{\stepcounter{mi}\themi}
\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=0.8cm,
    row sep=-12pt
  },
  >=latex,
  heading/.style={
    plain,
    text width=15mm,
    text centered,
  },
  larger/.style={
    text width=15mm,
    inner sep=1pt,
    text centered,
    font=\footnotesize
  },
  ]
  \matrix [net] (mat)
  {
    |[heading]|{Input\\layer}   & |[heading]|{Hidden\\layer} &  |[heading]|{Output\\layer} \\
    & |[plain]| \\
    |[plain]| & |[larger]| {$y=\sigma(\Sigma w_{\mymi i}m_i)$}\\
    & |[plain]| \\
    |[plain]| & |[plain]| \\
    & |[plain]| \\
    |[plain]| &  |[larger]| {$y=\sigma(\Sigma w_{\mymi i}m_i)$}\\
    & |[plain]| \\
    |[plain]| & |[plain]| & \\
    & |[plain]|  \\
    |[plain]| &   |[larger]| {$y=\sigma(\Sigma w_{\mymi i}m_i)$}\\
    & |[plain]| \\
    |[plain]| & |[plain]| \\
    & |[plain]| \\
    |[plain]| &  |[larger]| {$y=\sigma(\Sigma w_{\mymi i}m_i)$}\\
    & |[plain]| \\
  };
  \foreach \ai [count=\mi ]in {2,4,...,16}
  \draw[<-] (mat-\ai-1) -- node[above] {$m_\mi$} +(-1.2cm,0);
  \foreach \ai in {2,4,...,16}
  {\foreach \aii in {3,7,11,15}
    \draw[->] (mat-\ai-1) -- (mat-\aii-2);
  }
  \foreach \ai [count=\mi ] in {3,7,11,15}{
    \draw[->] (mat-\ai-2) -- (mat-9-3);
  }
  \draw[->] (mat-9-3) -- node[above] {RT} +(1.2cm,0);
\end{tikzpicture}
\end{document}

神经网络

如果您需要最左边节点的间距均匀,则需要调整中间层的空节点,以便它们的高度与包含内容的行上的高度相匹配。

相关内容