我正在尝试在 Tikz 中创建下图。我有一些部分的代码,但我对一些基础知识和将它们全部连接起来感到很困惑。
我主要想了解如何创建带有指向下一部分的箭头的垂直和水平向量。最好使用一个边界框,角落处标有模块的名称。到目前为止,我使用表格和表格“绘制”了向量,并且只使用了单元格边框。
我完成图表唯一缺少的是向量、边界框和连接各部分的箭头。我目前拥有的网络代码位于图片下方。
有人可以帮我完成我的图表吗?连接各部分的向量、边界框和箭头。
LSTM 网络。
\documentclass[crop, tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\tikzstyle{place}=[circle, draw=black, minimum size = 8mm]
% Input
\foreach \x in {1,...,3}
\draw node at (0, -\x*1.25) [place] (first_\x) {$x_\x$};
\foreach \x in {1,...,3}
\fill (0, -4.5 -\x*0.3) circle (2pt);
\draw node at (0, -5*1.25) [place] (first_n) {$x_n$};
% Hidden 1
\foreach \x in {1,...,3}
\node at (4, -\x*1.25) [place] (second_\x){$a_\x$};
\foreach \x in {1,...,3}
\fill (4, -4.5 -\x*0.3) circle (2pt);
\draw node at (4, -5*1.25) [place] (second_m) {$a_m$};
% Output
\foreach \x in {1,...,3}
\node at (8, -\x*1.25) [place] (fourth_\x){$y_\x$};
\foreach \x in {1,...,3}
\fill (8, -4.5 -\x*0.3) circle (2pt);
\node at (8, -5*1.25) [place] (fourth_m) {$y_k$};
% Input -> Hidden
\foreach \i in {1,...,3}
\foreach \j in {1,...,3}
\draw [->] (first_\i) to (second_\j);
\foreach \i in {1,...,3}
\draw [->] (first_\i) to (second_m);
\foreach \i in {1,...,3}
\draw [->] (first_n) to (second_\i);
\draw [->] (first_n) to (second_m);
% Hidden -> Output
\foreach \i in {1,...,3}
\foreach \j in {1,...,3}
\draw [->] (second_\i) to (fourth_\j);
\foreach \i in {1,...,3}
\draw [->] (second_\i) to (fourth_m);
\draw [->] (second_m) to (fourth_m);
% Text
\node at (0, -8) [black, ] {Input Layer};
\node at (4, -8) [black, ] {Hidden Layer};
\node at (8, -8) [black, ] {Output Layer};
\end{tikzpicture}
\end{center}
\end{figure}
\begin{tikzpicture}
\node[rectangle] (Y0) at (0, 0) {$\dots$};
\node[rectangle, draw, right=2em of Y0, minimum height=1cm, minimum width=1cm] (RNN) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN, draw, minimum height=1cm, minimum width=1cm] (RNN2) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN2, draw, minimum height=1cm, minimum width=1cm] (RNN3) {LSTM$_\rightarrow$};
\node[rectangle, right= of RNN3, draw, minimum height=1cm, minimum width=1cm] (RNN4) {LSTM$_\rightarrow$};
\node[rectangle, right=2em of RNN4] (RNN5) {$\dots$};
\node[rectangle, above=of RNN4, draw, minimum height=1cm, minimum width=1cm] (R25) {LSTM$_\leftarrow$};
\node[rectangle, left=of R25, minimum height=1cm, minimum width=1cm, draw] (R24) {LSTM$_\leftarrow$};
\node[rectangle, left=of R24, draw, minimum height=1cm, minimum width=1cm] (R23) {LSTM$_\leftarrow$};
\node[rectangle, left=of R23, draw, minimum height=1cm, minimum width=1cm] (R22) {LSTM$_\leftarrow$};
\node[rectangle, left=2em of R22] (R21) {$\dots$};
\node[right=2em of R25] (Y20) {$\dots$};
\node[below=of RNN] (X1) {$\vec{x}_2$};
\node[below=of RNN2] (X2) {$\vec{x}_3$};
\node[below=of RNN3] (X3) {$\vec{x}_4$};
\node[below=of RNN4] (X4) {$\vec{x}_5$};
\node[above=of R25] (Y5) {$\vec{h}_5$};
\node[above=of R24] (Y4) {$\vec{h}_4$};
\node[above=of R23] (Y3) {$\vec{h}_3$};
\node[above=of R22] (Y2) {$\vec{h}_2$};
\draw[-stealth, thick] (X1) -- (RNN);
\draw[-stealth, thick] (X2) -- (RNN2);
\draw[-stealth, thick] (X3) -- (RNN3);
\draw[-stealth, thick] (X4) -- (RNN4);
\draw[-stealth, thick, densely dotted] (Y0) -- (RNN);
\draw[-stealth, thick] (RNN) -- node[above, pos=0.35] {$\vec{h}_2^\rightarrow$} (RNN2);
\draw[-stealth, thick] (RNN2) -- node[above, pos=0.35] {$\vec{h}_3^\rightarrow$} (RNN3);
\draw[-stealth, thick] (RNN3) -- node[above, pos=0.35] {$\vec{h}_4^\rightarrow$} (RNN4);
\draw[-stealth, densely dotted, thick] (RNN4) -- (RNN5);
\node[below=4em of Y0] (d) {\dots};
\node[below=4em of RNN5] (d) {\dots};
\path[-stealth, ultra thick, white] (X1) edge[bend left=45] (R22);
\path[-stealth, thick] (X1) edge[bend left=45] (R22);
\path[-stealth, ultra thick, white] (X2) edge[bend left=45] (R23);
\path[-stealth, thick] (X2) edge[bend left=45] (R23);
\path[-stealth, ultra thick, white] (X3) edge[bend left=45] (R24);
\path[-stealth, thick] (X3) edge[bend left=45] (R24);
\path[-stealth, ultra thick, white] (X4) edge[bend left=45] (R25);
\path[-stealth, thick] (X4) edge[bend left=45] (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\draw[-stealth, thick] (R22) -- (Y2);
\draw[-stealth, thick] (R23) -- (Y3);
\draw[-stealth, thick] (R24) -- (Y4);
\draw[-stealth, thick] (R25) -- (Y5);
\draw[stealth-, densely dotted, thick] (R21) -- (R22);
\draw[stealth-, thick] (R22) -- node[above, pos=0.65] {$\vec{h}_3^\leftarrow$} (R23);
\draw[stealth-, thick] (R23) -- node[above, pos=0.65] {$\vec{h}_4^\leftarrow$} (R24);
\draw[stealth-, thick] (R24) -- node[above, pos=0.65] {$\vec{h}_5^\leftarrow$} (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\path[-stealth, ultra thick, white] (RNN) edge[bend right=45] (Y2);
\path[-stealth, thick] (RNN) edge[bend right=45] (Y2);
\path[-stealth, ultra thick, white] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, thick] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, ultra thick, white] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, thick] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, ultra thick, white] (RNN4) edge[bend right=45] (Y5);
\path[-stealth, thick] (RNN4) edge[bend right=45] (Y5);
\end{tikzpicture}
\end{document}
答案1
raster
这是一个非常复杂的图表。可以使用中的库进行安排tcolorbox
。以下是已经接近的内容。
\documentclass{article}
\usepackage[margin=1.5in]{geometry}
\usepackage[raster,skins]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,decorations.pathreplacing,calligraphy}
\colorlet{dgreen}{green!60!black}
\newtcolorbox{myboxR}[1][]{enhanced,
attach boxed title to top right={yshift=-5mm,yshifttext=-2mm,xshift=-1mm},
coltitle=black,fonttitle=\sffamily\tiny,
boxed title style={boxrule=0pt,
colback=white},#1}
\newtcolorbox{myboxL}[1][]{enhanced,
attach boxed title to top left={yshift=-5mm,yshifttext=-2mm,xshift=1mm},
coltitle=black,fonttitle=\sffamily\tiny,
boxed title style={boxrule=0pt,
colback=white},#1}
\begin{document}
\tikzset{every picture/.append style={remember picture}}
\begin{tcbraster}[raster columns=2, %raster equal height,
raster every box/.style={size=small,colframe=red!50!black,colback=white,
valign=center,halign=center}]
\begin{myboxR}[raster multicolumn=2,title=Neural network]
\begin{tikzpicture}[scale=0.8,place/.style={circle, draw=black, minimum size=8mm}]
% Input
\foreach \x in {1,...,3}
\draw node at (0, -\x*1.25) [place] (first_\x) {$x_\x$};
\foreach \x in {1,...,3}
\fill (0, -4.5 -\x*0.3) circle (2pt);
\draw node at (0, -5*1.25) [place] (first_n) {$x_n$};
% Hidden 1
\foreach \x in {1,...,3}
\node at (4, -\x*1.25) [place] (second_\x){$a_\x$};
\foreach \x in {1,...,3}
\fill (4, -4.5 -\x*0.3) circle (2pt);
\draw node at (4, -5*1.25) [place] (second_m) {$a_m$};
% Output
\foreach \x in {1,...,3}
\node at (8, -\x*1.25) [place] (fourth_\x){$y_\x$};
\foreach \x in {1,...,3}
\fill (8, -4.5 -\x*0.3) circle (2pt);
\node at (8, -5*1.25) [place] (fourth_m) {$y_k$};
% Input -> Hidden
\foreach \i in {1,...,3}
\foreach \j in {1,...,3}
\draw [->] (first_\i) to (second_\j);
\foreach \i in {1,...,3}
\draw [->] (first_\i) to (second_m);
\foreach \i in {1,...,3}
\draw [->] (first_n) to (second_\i);
\draw [->] (first_n) to (second_m);
% Hidden -> Output
\foreach \i in {1,...,3}
\foreach \j in {1,...,3}
\draw [->] (second_\i) to (fourth_\j);
\foreach \i in {1,...,3}
\draw [->] (second_\i) to (fourth_m);
\draw [->] (second_m) to (fourth_m);
% Text
\node at (0, -8) [black, ] {Input Layer};
\node at (4, -8) [black, ] {Hidden Layer};
\node at (8, -8) [black, ] {Output Layer};
\path[overlay] (current bounding box.south) coordinate (Neural1);
\end{tikzpicture}
\end{myboxR}
\begin{myboxR}[raster multicolumn=2,title=Congratulations cat]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[red] (0,0) rectangle ++ (-3,1);
\fill[dgreen] (-3,0) rectangle ++ (-3,1);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-16,1);
\path[overlay] (current bounding box.south) coordinate (ConCat1)
(current bounding box.north) coordinate (ConCat2);
\end{tikzpicture}
\end{myboxR}
\begin{tcboxedraster}[raster columns=1]{blankest}
\begin{myboxL}[title=pft]
\begin{tikzpicture}[scale=0.43,transform shape]
\node[rectangle] (Y0) at (0, 0) {$\dots$};
\node[rectangle, draw, right=2em of Y0, minimum height=1cm, minimum width=1cm] (RNN) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN, draw, minimum height=1cm, minimum width=1cm] (RNN2) {LSTM$_\rightarrow$};
\node[rectangle, right=of RNN2, draw, minimum height=1cm, minimum width=1cm] (RNN3) {LSTM$_\rightarrow$};
\node[rectangle, right= of RNN3, draw, minimum height=1cm, minimum width=1cm] (RNN4) {LSTM$_\rightarrow$};
\node[rectangle, right=2em of RNN4] (RNN5) {$\dots$};
\node[rectangle, above=of RNN4, draw, minimum height=1cm, minimum width=1cm] (R25) {LSTM$_\leftarrow$};
\node[rectangle, left=of R25, minimum height=1cm, minimum width=1cm, draw] (R24) {LSTM$_\leftarrow$};
\node[rectangle, left=of R24, draw, minimum height=1cm, minimum width=1cm] (R23) {LSTM$_\leftarrow$};
\node[rectangle, left=of R23, draw, minimum height=1cm, minimum width=1cm] (R22) {LSTM$_\leftarrow$};
\node[rectangle, left=2em of R22] (R21) {$\dots$};
\node[right=2em of R25] (Y20) {$\dots$};
\node[below=of RNN] (X1) {$\vec{x}_2$};
\node[below=of RNN2] (X2) {$\vec{x}_3$};
\node[below=of RNN3] (X3) {$\vec{x}_4$};
\node[below=of RNN4] (X4) {$\vec{x}_5$};
\node[above=of R25] (Y5) {$\vec{h}_5$};
\node[above=of R24] (Y4) {$\vec{h}_4$};
\node[above=of R23] (Y3) {$\vec{h}_3$};
\node[above=of R22] (Y2) {$\vec{h}_2$};
\draw[-stealth, thick] (X1) -- (RNN);
\draw[-stealth, thick] (X2) -- (RNN2);
\draw[-stealth, thick] (X3) -- (RNN3);
\draw[-stealth, thick] (X4) -- (RNN4);
\draw[-stealth, thick, densely dotted] (Y0) -- (RNN);
\draw[-stealth, thick] (RNN) -- node[above, pos=0.35] {$\vec{h}_2^\rightarrow$} (RNN2);
\draw[-stealth, thick] (RNN2) -- node[above, pos=0.35] {$\vec{h}_3^\rightarrow$} (RNN3);
\draw[-stealth, thick] (RNN3) -- node[above, pos=0.35] {$\vec{h}_4^\rightarrow$} (RNN4);
\draw[-stealth, densely dotted, thick] (RNN4) -- (RNN5);
\node[below=4em of Y0] (d) {\dots};
\node[below=4em of RNN5] (d) {\dots};
\path[-stealth, ultra thick, white] (X1) edge[bend left=45] (R22);
\path[-stealth, thick] (X1) edge[bend left=45] (R22);
\path[-stealth, ultra thick, white] (X2) edge[bend left=45] (R23);
\path[-stealth, thick] (X2) edge[bend left=45] (R23);
\path[-stealth, ultra thick, white] (X3) edge[bend left=45] (R24);
\path[-stealth, thick] (X3) edge[bend left=45] (R24);
\path[-stealth, ultra thick, white] (X4) edge[bend left=45] (R25);
\path[-stealth, thick] (X4) edge[bend left=45] (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\draw[-stealth, thick] (R22) -- (Y2);
\draw[-stealth, thick] (R23) -- (Y3);
\draw[-stealth, thick] (R24) -- (Y4);
\draw[-stealth, thick] (R25) -- (Y5);
\draw[stealth-, densely dotted, thick] (R21) -- (R22);
\draw[stealth-, thick] (R22) -- node[above, pos=0.65] {$\vec{h}_3^\leftarrow$} (R23);
\draw[stealth-, thick] (R23) -- node[above, pos=0.65] {$\vec{h}_4^\leftarrow$} (R24);
\draw[stealth-, thick] (R24) -- node[above, pos=0.65] {$\vec{h}_5^\leftarrow$} (R25);
\draw[-stealth, densely dotted, thick] (Y20) -- (R25);
\path[-stealth, ultra thick, white] (RNN) edge[bend right=45] (Y2);
\path[-stealth, thick] (RNN) edge[bend right=45] (Y2);
\path[-stealth, ultra thick, white] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, thick] (RNN2) edge[bend right=45] (Y3);
\path[-stealth, ultra thick, white] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, thick] (RNN3) edge[bend right=45] (Y4);
\path[-stealth, ultra thick, white] (RNN4) edge[bend right=45] (Y5);
\path[-stealth, thick] (RNN4) edge[bend right=45] (Y5);
\end{tikzpicture}
\end{myboxL}
\begin{myboxL}[title=pft]
\begin{tikzpicture}
\foreach \X in {1,2,3,4}
{\draw (\X-0.01,0) grid[step=5mm] ++ (0.51,3);}
\end{tikzpicture}
\end{myboxL}
\begin{myboxL}[title=pft]
\begin{tikzpicture}
\foreach \X in {1,2,3,4}
{\draw (\X-0.01,0) grid[step=5mm] ++ (0.51,3);}
\end{tikzpicture}
\end{myboxL}
\end{tcboxedraster}
%
\begin{tcboxedraster}[raster columns=2]{blankest}
\begin{myboxR}[raster multicolumn=2,title=Occident]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[red] (0,0) rectangle ++ (-3,1);
\fill[dgreen] (-3,0) rectangle ++ (-3,1);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-6,1);
\path[overlay] (current bounding box.south) coordinate (Occident1)
([yshift=3mm]current bounding box.north) coordinate (Occident2);
\end{tikzpicture}
\end{myboxR}
%
\begin{myboxR}[title=Flatten]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[dgreen] (0,0) rectangle ++ (-6,-1);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-6,-1);
\path (0,1);
\path[overlay] (current bounding box.south) coordinate (Flatten1)
([yshift=3mm]current bounding box.north) coordinate (Flatten3);
\end{tikzpicture}
\end{myboxR}
%
\begin{myboxR}[title=Flatten]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[red] (0,0) rectangle ++ (-6,-1);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-6,-1);
\path (0,1);
\path[overlay] (current bounding box.south) coordinate (Flatten2)
([yshift=3mm]current bounding box.north) coordinate (Flatten4);
\end{tikzpicture}
\end{myboxR}
%
\begin{myboxR}[title=CNN]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[dgreen] (0,0) rectangle ++ (-6,-6);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-6,-6);
\path[overlay] (current bounding box.south west) + (1,-0.4) coordinate (CNN1)
(current bounding box.north) coordinate (CNN3) ;
\path (0,1);
\end{tikzpicture}
\end{myboxR}
%
\begin{myboxR}[title=CNN]
\begin{tikzpicture}[x=2.5mm,y=5mm]
\fill[red] (0,0) rectangle ++ (-6,-6);
\draw (0,0) grid[xstep=2.5mm,ystep=5mm] ++ (-6,-6);
\path[overlay] (current bounding box.south east) + (-1,-0.4) coordinate (CNN2)
(current bounding box.north) coordinate (CNN4);
\path (0,1);
\end{tikzpicture}
\end{myboxR}
%
\begin{myboxR}[raster multicolumn=2,title=pft]
\begin{tikzpicture}
\foreach \X in {1,2,3,4}
{\draw (\X-0.01,0) grid[step=5mm] ++ (0.51,3);}
\path (1.25,3) coordinate (br1) (4.25,3) coordinate (br2);
\end{tikzpicture}
\end{myboxR}
\end{tcboxedraster}
\end{tcbraster}
\begin{tikzpicture}[overlay,semithick,>=stealth]
\draw[decorate,decoration=calligraphic brace]
(Flatten3) -- (Flatten4);
\draw[decorate,decoration=calligraphic brace]
let \p1=($(Occident2)-(ConCat1)$) in
([xshift=-2*\x1,yshift=1pt]Occident2) -- ([yshift=1pt]Occident2)
coordinate[midway,yshift=3pt] (aux);
\draw (br1) -- (CNN1) (br2) -- (CNN2)
(CNN3) edge[->] (Flatten1)
(CNN4) edge[->] (Flatten2)
(aux) edge[->] (ConCat1)
(ConCat2) edge[->] (Neural1);
\end{tikzpicture}
\end{document}
评论:
- 您可以使用 pgf 键更改外观。对于框来说尤其如此。
- 您的屏幕截图上的某些内容无法读取,因此我用通用文本替换了它们。
- 箭头和支架需要进行一些调整。
- 我只是照
tikzpicture
原样复制了你的代码。有很多方法可以优化它们。 - 还有许多其他可能的变化。例如使用
tcboxeditemize
。或者在这里或那里更改一些小细节。