1

I am trying to create the following diagram in Tikz. And i've got some code for some parts but im struggling with a few basics and connecting it all.

Mostly I'm looking to understand how to create vertical and horizontal vectors with arrows leading to the next part. Preferably with a bounding box with the name of the module in the corner. So far I "drew" the vectors using a table and tabular, and just use the cell borders.

The only things that are missing for me to complete my diagram is the vectors, bounding box and some arrows connecting the parts. The code for the networks I have so far is below the image.

Could somebody help me complete my diagram? the vectors, bounding boxes and arrows connecting the parts.

enter image description here

The LSTM network.

\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

This is a very complex diagram. One can make the arrangement with the raster library from tcolorbox. Here is something that comes already close.

\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}

enter image description here

Remarks:

  1. You can change the appearance with pgf keys. This is in particular true for the boxes.
  2. Several things on your screen shot are unreadable so I replaced them with generic texts.
  3. The arrows and braces require some adjustment.
  4. I just copied your tikzpictures as they were. There are many ways to optimize them.
  5. There are many other variations possible. E.g. the use of tcboxeditemize. Or changing some minor detail here and there.
| improve this answer | |
  • 1
    I hope not that not the usual thing happens: someone comes, copies this code, makes some minor adjustments and gets the full credit. – user194703 Apr 12 at 18:43
  • Holy moley baloney! you're my personal hero. This is beyond my wildest dreams of what I expected somebody could help me with. Thank you, this also provides me with all the building blocks needed. The arrows are indeed all off by a few centimetres to the bottom, but I'm sure this is an offset problem somewhere? that i can just fix myself, ill figure it out. Thanks a bunch! – Rien Apr 12 at 19:02
  • 2
    @Rien You have to compile three times to make remember picture work properly. And you're welcome! – user194703 Apr 12 at 19:05
  • I'm afraid that re-compiling the document 3 times did not solve it – Rien Apr 12 at 19:08
  • 1
    @Rien As far as I know lualatex is even better for fonts. – user194703 Apr 12 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.