Tikz 中的 2D CNN 网络

Tikz 中的 2D CNN 网络

我正在尝试绘制一个像这样的 CNN 网络: 在此处输入图片描述

我已经画了矩形,但我不知道如何在它们之间添加箭头和标签。

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{3d,decorations.text,shapes.arrows,positioning,fit,backgrounds}
\tikzset{pics/fake box/.style args={% #1=color, #2=x dimension, #3=y dimension, #4=z dimension
#1 with dimensions #2 and #3 and #4}{
code={
\draw[gray,ultra thin,fill=#1]  (0,0,0) coordinate(-front-bottom-left) to
++ (0,#3,0) coordinate(-front-top-right) --++
(#2,0,0) coordinate(-front-top-right) --++ (0,-#3,0) 
coordinate(-front-bottom-right) -- cycle;
\draw[gray,ultra thin,fill=#1] (0,#3,0)  --++ 
 (0,0,#4) coordinate(-back-top-left) --++ (#2,0,0) 
 coordinate(-back-top-right) --++ (0,0,-#4)  -- cycle;
\draw[gray,ultra thin,fill=#1!80!black] (#2,0,0) --++ (0,0,#4) coordinate(-back-bottom-right)
--++ (0,#3,0) --++ (0,0,-#4) -- cycle;
\path[gray,decorate,decoration={text effects along path,text={CONV}}] (#2/2,{2+(#3-2)/2},0) -- (#2/2,0,0);
}
}}
% from https://tex.stackexchange.com/a/52856/121799
\tikzset{circle dotted/.style={dash pattern=on .05mm off 2mm,
                                         line cap=round}}
\begin{document}

\begin{tikzpicture}[x={(1,0)},y={(0,1)},z={({cos(60)},{sin(60)})},
font=\sffamily\small,scale=2]

\foreach \X/\Col in {6.5/red,6.7/green,6.9/blue}
{\draw[canvas is yz plane at x = \X, transform shape, draw = red, fill =
\Col!50!white, opacity = 0.5] (0,0.5) rectangle (2,-1.5);}

\end{tikzpicture}
\end{document}

答案1

这些都不是特别困难。如果你愿意尝试理解代码而不是复制代码,你的生活就会变得简单得多。没有理由复制图片,fake box因为你所需的输出似乎不包含方框。是的,给予他人信任并没有什么坏处。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\definecolor{dgreen}{RGB}{63,127,0}
\definecolor{dred}{RGB}{144,14,3}
\begin{document}
\tdplotsetmaincoords{60}{50}
\begin{tikzpicture}[tdplot_main_coords,line join=miter,font=\sffamily]
\path[tdplot_screen_coords] (-2,0); 
\pgfmathsetmacro{\xstretch}{4}
\edef\Cols{red,dgreen,purple,yellow,orange}
\edef\LstCols{{"red","dgreen","purple","yellow","orange"}}
\foreach \XX in {1,2}
 {\begin{scope}[canvas is yz plane at x=-0.8+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=dred!\fullness!red]
   (-1,-1) rectangle (1,1);
  \end{scope}}
\foreach \Col [count=\X starting from 0,remember=\X as \LastX] in \Cols
 {\foreach \XX in {1,2,3,4}
 {\draw[thick,\Col] (\xstretch*\X+0.4*\XX-0.4,0,0) -- (\xstretch*\X+0.4*\XX,0,0);
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=\Col!\fullness] (-1,-1) rectangle (1,1);
  \end{scope}}
  \node[anchor=west] at (\xstretch*\X,1,1){$x_\X$};
  \draw[thick,\Col] (\xstretch*\X+0.4*4,0,0) coordinate(front-\X) 
   -- (\xstretch*\X+1+0.4*4,0,0) coordinate(back-\X);
  \begin{scope}[canvas is xy plane at z=0]
   \ifnum\X>0
    \foreach \Z in {0,...,\LastX}
    {\pgfmathsetmacro{\mycol}{\LstCols[\Z]}
    \draw[thick,\mycol,overlay] (front-\Z) to[out=-90,in=-90,looseness=1.5] (back-\X);}
   \fi
  \end{scope}
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+1+0.4*4,transform shape]
   \ifnum\X=4
    \node[draw,fill=white] (BN-\X) at (0,0) {Transition layer};
   \else
    \node[draw,fill=white] (BN-\X) at (0,0) {BN--RelU--Conv};
   \fi
  \end{scope}
  \pgfmathtruncatemacro{\NextX}{\X+1}
  \ifnum\X<4
   \node[anchor=south west] at (BN-\X.north east) {$H_\NextX$};
   \pgfmathsetmacro{\NextCol}{\LstCols[\NextX]}
  \else 
   \pgfmathsetmacro{\NextCol}{"black"} 
  \fi 
  \draw[thick,\NextCol] (\xstretch*\X+1+0.4*4,0,0) -- ({\xstretch*(\X+1)+0.4*4},0,0);
  }
\end{tikzpicture}
\end{document}

在此处输入图片描述

强制动画。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d}
\definecolor{dgreen}{RGB}{63,127,0}
\definecolor{dred}{RGB}{144,14,3}
\begin{document}
\foreach \Angle in {0,10,...,350}
{\tdplotsetmaincoords{60}{90-50*cos(\Angle)}
\begin{tikzpicture}[tdplot_main_coords,line join=miter,font=\sffamily]
\path[tdplot_screen_coords,use as bounding box] (-18,-12) rectangle (18,3); 
\pgfmathsetmacro{\xstretch}{4}
\edef\Cols{red,dgreen,purple,yellow,orange}
\edef\LstCols{{"red","dgreen","purple","yellow","orange"}}
\foreach \XX in {1,2}
 {\begin{scope}[canvas is yz plane at x=-0.8+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=dred!\fullness!red]
   (-1,-1) rectangle (1,1);
  \end{scope}}
\foreach \Col [count=\X starting from 0,remember=\X as \LastX] in \Cols
 {\foreach \XX in {1,2,3,4}
 {\draw[thick,\Col] (\xstretch*\X+0.4*\XX-0.4,0,0) -- (\xstretch*\X+0.4*\XX,0,0);
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+0.4*\XX]
   \pgfmathtruncatemacro{\fullness}{120-20*\XX}
   \draw[fill=\Col!\fullness] (-1,-1) rectangle (1,1);
  \end{scope}}
  \node[anchor=west] at (\xstretch*\X,1,1){$x_\X$};
  \draw[thick,\Col] (\xstretch*\X+0.4*4,0,0) coordinate(front-\X) 
   -- (\xstretch*\X+1+0.4*4,0,0) coordinate(back-\X);
  \begin{scope}[canvas is xy plane at z=0]
   \ifnum\X>0
    \foreach \Z in {0,...,\LastX}
    {\pgfmathsetmacro{\mycol}{\LstCols[\Z]}
    \draw[thick,\mycol,overlay] (front-\Z) to[out=-90,in=-90,looseness=1.5] (back-\X);}
   \fi
  \end{scope}
  \begin{scope}[canvas is yz plane at x=\xstretch*\X+1+0.4*4,transform shape]
   \ifnum\X=4
    \node[draw,fill=white] (BN-\X) at (0,0) {Transition layer};
   \else
    \node[draw,fill=white] (BN-\X) at (0,0) {BN--RelU--Conv};
   \fi
  \end{scope}
  \pgfmathtruncatemacro{\NextX}{\X+1}
  \ifnum\X<4
   \node[anchor=south west] at (BN-\X.north east) {$H_\NextX$};
   \pgfmathsetmacro{\NextCol}{\LstCols[\NextX]}
  \else 
   \pgfmathsetmacro{\NextCol}{"black"} 
  \fi 
  \draw[thick,\NextCol] (\xstretch*\X+1+0.4*4,0,0) -- ({\xstretch*(\X+1)+0.4*4},0,0);
  }
\end{tikzpicture}}
\end{document}

在此处输入图片描述

相关内容