使用 TikZ 编写卷积神经网络

使用 TikZ 编写卷积神经网络

我想使用 TikZ 在图片中绘制这样的 CNN,有什么想法吗?这

答案1

这是为了给您一个开始(而不是尝试完全重现全屏截图)。此答案带有一种样式3d matrix,可让您相当方便地生成这些块,您只需填写条目即可。请确保为每个矩阵赋予不同的名称,否则一个矩阵可能会从另一个矩阵继承块大小。(您还需要运行代码两次。)我还展示了如何在矩阵和特定元素之间绘制箭头。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{eqparbox}
\usetikzlibrary{matrix,positioning,arrows.meta,bending}
\newbox\matrixcellbox
\tikzset{math center align per column/.style={nodes={execute at begin
 node={\setbox\matrixcellbox=\hbox\bgroup$},
 execute at end
 node={$\egroup\eqmakebox[\tikzmatrixname\the\pgfmatrixcurrentcolumn][c]{\copy\matrixcellbox}}}},
 math center align per matrix/.style={nodes={execute at begin
 node={\setbox\matrixcellbox=\hbox\bgroup$},
 execute at end
 node={$\egroup\eqmakebox[\tikzmatrixname][c]{\copy\matrixcellbox}}}},
 3d matrix/.style={matrix of nodes,nodes in empty cells,math center align per
 matrix,nodes={draw,anchor=center,outer sep=0pt,inner sep=1pt,
 text height={height("\raisebox{0.2ex}{A}")},text depth={depth("g")}},
 column sep=-\pgflinewidth,row sep=-\pgflinewidth,execute at end matrix={
 \foreach \XX in {1,...,\the\pgfmatrixcurrentcolumn}
 {\draw (\tikzmatrixname-1-\XX.north east) -- ++ (#1,#1);}
 \ifnum\the\pgfmatrixcurrentrow>1
 \foreach \XX in {1,...,\the\numexpr\the\pgfmatrixcurrentrow-1}
 {\draw (\tikzmatrixname-\XX-\the\pgfmatrixcurrentcolumn.south east) -- ++ (#1,#1);}
 \fi
 \draw (\tikzmatrixname-1-1.north west) -- ++ (#1,#1) -- 
  ([xshift=1ex,yshift=1ex]\tikzmatrixname-1-\the\pgfmatrixcurrentcolumn.north east) --
  ([xshift=1ex,yshift=1ex]\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east)  --
  (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east); 
 }},3d matrix/.default=1ex,
 Rightarrow/.style={double,double
     distance=#1,>={Implies[bend]},shorten <=0.4ex},Rightarrow/.default=1ex}
\begin{document}
\begin{tikzpicture}[node distance=4em]
 \node[3d matrix] (mat1){ 
    0 & 0 & 1 & 1 & 0 & 0 & -1 & -1 & 0 & 0 \\
 };
 \node[3d matrix,above=of mat1] (mat2){ 
    0 & 0 & 1 & 1 & 0 & 0 & -1 & -1 & 0 & 0\\
    0 & 0 & -1 & -1 & 0 & 0 & 1 & 1 & 0 & 0\\
 };
 \node[3d matrix,above=of mat2] (mat3){ 
    0 & 1 & 0 & -1 & 0 \\
    0 & -1 & 0 & 1 & 0 \\
 };
 \node[3d matrix,above=of mat3] (mat4){ 0 & -4 & 0 \\};
 \node[3d matrix,above=of mat4] (mat5){ 4 & -4 \\};
 \node[3d matrix,above=of mat5] (mat6){ 0.99 & 0.01 \\};
 \draw[-{Latex[bend]}] (mat1.east) to[out=0,in=0] 
  coordinate[near end](aux1) ([xshift=1ex]mat2-1-10.east);
 \path (aux1) node[right,above right,3d matrix]{-1 & 0 & 1\\}; 
 \draw[-{Latex[bend]}] (mat1.east) to[out=0,in=0] ([xshift=1ex]mat2-2-10.east);
 \draw[Rightarrow,->] (mat1) -- coordinate[midway,right=2em] (aux2) (mat2);
 \path (aux2) node[right,3d matrix] (mat1a){1 & 0 & -1 \\};
 \draw[Rightarrow,->] (mat2) -- (mat3);
 \draw[Rightarrow,->] (mat3) -- (mat4)
    coordinate[midway,right=1em] (aux3);
 \path (aux3) node[right,3d matrix] (mat3a){-1 & 0 & 1 \\
        1 & 0 & -1\\};
 \foreach \X in {1,2,3} {\foreach \Y in {1,2}
  {\draw[-latex] (mat4-1-\X) -- (mat5-1-\Y);}}
 \draw[Rightarrow,->] (mat5) -- (mat6);
\end{tikzpicture}
\end{document}

在此处输入图片描述

一些问题当尝试在路径构造中添加矩阵时。因此,我沿路径添加了坐标,并使用这些坐标来放置矩阵。

相关内容