我正在尝试从 neuronetworksanddeeplearning.com (第 6 章)复制下面的图像:
我得到了网格(虽然第二个没有居中)但我不知道如何在网格上方添加线条/箭头和标题。
我的代码:
\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}
答案1
如果使用\node
s 而不是命令,则可以方便地根据和\draw
的值标记每个圆圈。然后,您可以使用这些标签来绘制线条和放置标题:\x
\y
\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}