I am a newcomer to Tikz and have been trying to draw an recurrent neural network Long-Short Term Memory (LSTM) cell in Tikz, but have trouble correctly aligning the boxes I need inside the cell. The LSTM cell looks as follows
I have the following attempt, but clearly it's a far way from being done.
The code is
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, arrows.meta, shapes}
\begin{document}
\begin{tikzpicture}[
elementwiseoperation/.style={circle, draw, inner sep=0pt},
elementwisefunction/.style={ellipse, draw, inner sep=1pt},
ct/.style={circle, draw, minimum width=1cm, inner sep=1pt},
gt/.style={rectangle, draw, minimum width=4mm, minimum height=3mm, inner sep=1pt},
filter/.style={circle, draw, minimum width=8mm, inner sep=1pt, path picture={\draw[thick, rounded corners] (path picture bounding box.center)--++(65:2mm)--++(0:1mm);
\draw[thick, rounded corners] (path picture bounding box.center)--++(245:2mm)--++(180:1mm);}},
mylabel/.style={font=\scriptsize\sffamily},
>=LaTeX
]
% Input cell
\node[ct, label={[mylabel]Cell}] (ct1) {$c^{t-1}$};
% Input hidden
\node[ct, below=3cm of ct1.south, label={[mylabel]Hidden}] (ht1) {$h^{t-1}$};3
% Input x
\node[ct, below right=1cm and 1 cm of ht1, label={[mylabel]left:Input}] (xt1) {$x^{t}$};
% Elementwise operations on cell
\node[elementwiseoperation, right=1.5cm of ct1] (mul1) {$\times$};
\node[elementwiseoperation, right=of mul1] (add1) {$+$};
%
\coordinate[left of=mul1] (celllinesplit0);
\coordinate[right of=add1] (celllinesplit1);
\coordinate[right of=celllinesplit1] (celllinesplit2);
\coordinate[above=of xt1, right=of ht1] (h and x join);
% New cell
\node[elementwisefunction, below=of celllinesplit1] (tanh) {tanh};
\node[elementwiseoperation, below of=add1] (mul2) {$\times$};
\node[ct, right of=celllinesplit1, label={[mylabel]Cell}] (ct2) {$c^{t}$};
\node[gt, below of=mul2] (cellbox) {tanh};
\node[gt, left=2mm of cellbox] (inputbox) {$\sigma$};
\node[gt, left=2mm of inputbox, below=of mul1] (forgetbox) {$\sigma$};
\node[gt, right=2mm of cellbox] (outputbox) {$\sigma
\draw[->] (ct1) to (mul1);
\draw[->] (mul1) to (add1);
\draw[->] (mul2) to (add1);
\draw[->] (add1) to (ct2);
\draw[->] (add1) to[out=0,in=90] (tanh);
\draw[->] (forgetbox) to (mul1);
\draw[-] (xt1) to (h and x join)[in=0];
\draw[-] (ht1) to (h and x join)[in=0];
\end{tikzpicture}
\end{document}
Thanks in advance for any attempt at this, it is much appreciated.
Best Answer
Just for fun, and to prove that the arrow paths with corners can be rounded. An option using absolute positioning, and labeled coordinates, with intersectións
(A|-B)
, and displacements++(a,b)
.RESULT:
MWE: