答案1
\documentclass{article}
\usepackage{amsmath} % needed for \text
\usepackage{tikz} % needed for all the drawing
\usetikzlibrary{calc} % needed for coordinate calculations
\newcommand{\grid}[3]{%
\begin{tikzpicture}[baseline=(current bounding box)]
\foreach \x in {1,...,#2} {
\foreach \y in {1,...,#1} {
\draw[fill=#3, color=#3] ($(0.4*\x, 0.4*\y)$) circle[radius=0.15cm];
}
}
\draw (0.2, 0.2) rectangle ($(0.4*#2, 0.4*#1) + (0.2, 0.2)$);
\end{tikzpicture}
}
\begin{document}
\[
\text{score} =
\underbrace{\grid{1}{3}{blue}}_{\mathbf{w}^\top}
\sigma \left(\underbrace{\grid{3}{4}{blue}}_{\mathbf{U}}
\sigma\left( \underbrace{\grid{4}{6}{blue}}_{\mathbf{V}}
\underbrace{\grid{6}{1}{red}}_{x}\right)\right)
\]
\end{document}
以下是我的做法。粗略地说,定义的宏\grid{rows}{cols}{colour}
会绘制一个给定颜色的圆圈rows
xcols
矩阵。它会自动放入一个框中,然后由于每个矩阵都是自己的tikzpicture
环境,因此可以像任何其他数学命令一样在方程中使用。