1

I am trying to make a neural networkenter image description here with some text inside the nodes in the hidden layer. My code is here which is a modified version of one I found on 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}

How can I fit the text in the middle of node without it spilling out?

2
  • Can you please complete your code so we don't have to figure out what's needed to compile it, possibly deleting stuff we can't guess and defining things differently?
    – cfr
    Commented Mar 9, 2017 at 23:30
  • You can use elliptic nodes: \usetikzlibrary{shapes} in the preamble and option ellipse for the hidden nodes.
    – gernot
    Commented Mar 9, 2017 at 23:48

1 Answer 1

2

You really have 2 choices: make the nodes bigger in advance and place the text later or let TikZ place the text at the time, automatically fitting the node to accommodate the content.

The second is likely to be more straightforward and less error-prone, so I'd go for that option.

\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}

neural network

If you need even spacing of the leftmost nodes, you'll need to adjust the empty nodes in the middle layer so that their height matches those on lines which include the content.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .