3

I'm trying to replicate the image below from neuralnetworksanddeeplearning.com (chapter 6):

enter image description here

I got the grids (the 2nd isn't centered though) but I don't know how to add the lines/arrows and the captions above the grids.

My code:

\documentclass[11pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{xifthen}

\begin{document}

\begin{tikzpicture}
  \foreach \x in {0,...,27}
    \foreach \y in {0,...,27} {
        \ifthenelse{\x < 5 \AND \y > 22}{\draw[black] (0.2*\x, 0.2*\y) circle (0.05cm);}{\draw[gray] (0.2*\x, 0.2*\y) circle (0.05cm);}
    }

  \foreach \x in {0,...,23}
    \foreach \y in {0,...,23} {
        \ifthenelse{\x < 1 \AND \y > 22}{\draw[black] (8 + 0.2*\x, 0.2*\y) circle (0.05cm);}{\draw[gray] (8 + 0.2*\x, 0.2*\y) circle (0.05cm);}
    }
\end{tikzpicture}

\end{document}

enter image description here

2

If you use \nodes instead of \draw commands, then you can conveniently label each of the circles based on the values of \x and \y. You can then use those labels both to draw the lines and to place the captions:

\documentclass[11pt]{article}

\usepackage{tikz}
\usepackage{xifthen}

\begin{document}

\begin{tikzpicture}[inner sep=0.5mm]
  \foreach \x in {0,...,27}
    \foreach \y in {0,...,27} {
        \ifthenelse{\x < 5 \AND \y > 22}
        {\node[circle,draw=black] (A-\x-\y) at (0.2*\x, 0.2*\y) {};}
        {\node[circle,draw=gray] (A-\x-\y) at (0.2*\x, 0.2*\y) {};}
    }

  \foreach \x in {0,...,23}
    \foreach \y in {0,...,23} {
        \ifthenelse{\x < 1 \AND \y > 22}
        {\node[circle,draw=black] (B-\x-\y) at (8 + 0.2*\x, 0.4 + 0.2*\y) {};}
        {\node[circle,draw=gray] (B-\x-\y) at (8 + 0.2*\x, 0.4 + 0.2*\y) {};}
    }
    \draw[->] (A-0-27) -- (B-0-23);
    \draw[->] (A-0-26) -- (B-0-23);
    \draw[->] (A-0-25) -- (B-0-23);
    \draw[->] (A-0-24) -- (B-0-23);
    \draw[->] (A-0-23) -- (B-0-23);
    \node[above=6pt] at (A-14-27) {input neurons};
    \node[above=6pt] at (B-11-23) {first hidden layer};
\end{tikzpicture}

\end{document}

grid of circles with lines

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.