3

I'm trying to build a neural network, based on the first answer here. The final networks should have 3 input nodes, 4 hidden nodes, and 2 output nodes.

Right now, the image looks like this:

enter image description here

Why is the middle node of the hidden layer bigger than the other nodes? I need it to be the same size. Note: I already successfully made one neural net (below). I just want this to be a modified version without the 'Data' column.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
active/.style={
    draw=none,
    fill=green!20,
},
invisible/.style={
    draw=none,
    fill=none,
    text=white,
},
halfinvisible/.style={
    draw,
    fill=none,
    text=white,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.5cm,
    row sep=-5pt
},
>=latex
]

\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\

    & &  \\ %Top Data and Input nodes
   &  |[halfinvisible]| \parbox{0.2cm}{\centering{ii}} &  \\
};
\end{tikzpicture}
\end{figure}
\end{document}

Here's the version I'm trying to copy (removing the Data column).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,chains,positioning,decorations.pathreplacing,arrows}

\begin{document}

    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    draw=none,
    fill=none,
},
active/.style={
    draw=none,
    fill=green!20,
},
invisible/.style={
    draw=none,
    fill=none,
    text=white,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    nodes in empty cells,
    column sep=0.5cm,
    row sep=-5pt
},
>=latex
]

\matrix[net] (mat)
{
    |[plain]| \parbox{1cm}{\centering Data} & |[plain]| \parbox{1cm}{\centering Input\\layer} & |[plain]| \parbox{1cm}{\centering Hidden\\layer} & |[plain]| \parbox{1cm}{\centering Output\\layer} \\
    %& & |[plain]|  \\ %Top Data and Input nodes
   %|[plain]| & |[plain]| & \\ %Top Hidden node
   |[plain]| \parbox{0.2cm}{\centering 20} & & &\\
   |[plain]| \parbox{0.2cm}{\centering 30} & & & \\
   |[plain]| \parbox{0.2cm}{\centering 40} & & \\
  |[invisible]| \parbox{0.2cm}{} & |[invisible]| \parbox{0.2cm}{\centering 40} & \\
};

\foreach \ai [count=\mi ]in {2,3,4}
\draw[<-] (mat-\ai-2) --node[above]{Input \mi} +(-2cm,0);

\foreach \ai in {2,...,4}
    {\foreach \aii in {2,...,5}
        \draw[->] (mat-\ai-2) -- (mat-\aii-3);
    }

\foreach \ai in {2,...,5}
    {\foreach \aii in {2,3}
        \draw[->] (mat-\ai-3) -- (mat-\aii-4);
    }
\end{tikzpicture}
\end{figure}
\end{document}

It looks like this:

enter image description here

1 Answer 1

4

It's bigger because there is text inside it, specifically |[halfinvisible]| \parbox{0.2cm}{\centering{ii}}. So if you remove that text (and modify the row sep a bit) you get this:

enter image description here

Here is a slightly modified version of your second code, where the first column is removed:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcommand{\ctab}[1]{\begin{tabular}{@{}c@{}} #1 \end{tabular}}

\begin{document}
    \begin{figure}[htp]
    \centering
    \begin{tikzpicture}[
plain/.style={
    rectangle, % <-- added
    draw=none,
    fill=none,
},
active/.style={
    draw=none,
    fill=green!20,
},
invisible/.style={
    draw=none,
    fill=none,
    text=white,
},
net/.style={
    matrix of nodes,
    nodes={
        draw,
        circle,
        inner sep=8.5pt
    },
    row 1/.style={ 
      nodes={plain} % set style for nodes in first row
    },
    nodes in empty cells,
    column sep=1.5cm, % <-- modified
    row sep=3pt % <-- modified
},
>=latex
]

\matrix[net] (mat)
{
 \ctab{Input\\layer} &
 \ctab{Hidden\\layer} &
 \ctab{Output\\layer} \\
   & &\\
   & & \\
   & \\
 |[invisible]| &  & |[invisible]| \\
};


\foreach \ai in {2,...,4}
    {\foreach \aii in {2,...,5}
        \draw[->] (mat-\ai-1) -- (mat-\aii-2); % decreased column numbers by 1
    }

\foreach \ai in {2,...,5}
    {\foreach \aii in {2,3}
        \draw[->] (mat-\ai-2) -- (mat-\aii-3); % decreased column numbers by 1
    }
\end{tikzpicture}
\end{figure}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.