figure-the-whole-of-LSTM.tex 7.17 KB
Newer Older
曹润柘 committed
1 2
  
  
曹润柘 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19



%--------------------------------------------------------------------------------
\begin{tikzpicture}
\setlength{\base}{0.6cm}

\tikzstyle{wordnode} = [font=\scriptsize]
\tikzstyle{auxnode} = [inner sep=0pt,outer sep=0pt,opacity=0,draw=red,fill=red,circle,minimum size=3pt]
\tikzstyle{opnode} = [inner sep=0pt,outer sep=0pt,draw,fill=green!30!white,font=\scriptsize,minimum size=10pt]
\tikzstyle{standard} = [rounded corners=5pt,thick]
\tikzstyle{emph} = [rounded corners=5pt,thick,draw=red]
\tikzstyle{formulanode} = [font=\scriptsize,align=left,draw=red,rectangle,fill=red!10!white,rounded corners=2pt,drop shadow]

% Skeleton
\begin{scope}[every label/.append style={label distance=1pt,font=\tiny,inner sep=0pt,opacity=0}]
\coordinate (aux12) at (0,0);
zengxin committed
20
%\node[auxnode,label={-45:12}] () at (aux12) {};
曹润柘 committed
21 22

\coordinate (aux22) at ([yshift=\base]aux12);
zengxin committed
23
%\node[auxnode,label={-45:22}] () at (aux22) {};
曹润柘 committed
24
\coordinate (aux21) at ([xshift=-2\base]aux22);
zengxin committed
25
%\node[auxnode,label={-45:21}] () at (aux21) {};
曹润柘 committed
26
\coordinate (aux23) at ([xshift=\base]aux22);
zengxin committed
27
%\node[auxnode,label={-45:23}] () at (aux23) {};
曹润柘 committed
28
\coordinate (aux24) at ([xshift=\base]aux23);
zengxin committed
29
%\node[auxnode,label={-45:24}] () at (aux24) {};
曹润柘 committed
30
\coordinate (aux25) at ([xshift=\base]aux24);
zengxin committed
31
%\node[auxnode,label={-45:25}] () at (aux25) {};
曹润柘 committed
32
\coordinate (aux26) at ([xshift=\base]aux25);
zengxin committed
33
%\node[auxnode,label={-45:26}] () at (aux26) {};
曹润柘 committed
34
\coordinate (aux27) at ([xshift=\base]aux26);
zengxin committed
35
%\node[auxnode,label={-45:27}] () at (aux27) {};
曹润柘 committed
36
\coordinate (aux28) at ([xshift=\base]aux27);
zengxin committed
37
%\node[auxnode,label={-45:28}] () at (aux28) {};
曹润柘 committed
38
\coordinate (aux29) at ([xshift=2\base]aux28);
zengxin committed
39
%\node[auxnode,label={-45:29}] () at (aux29) {};
曹润柘 committed
40 41

\coordinate (aux33) at ([yshift=\base]aux23);
zengxin committed
42
%\node[auxnode,label={-45:33}] () at (aux33) {};
曹润柘 committed
43
\coordinate (aux34) at ([yshift=\base]aux24);
zengxin committed
44
%\node[auxnode,label={-45:34}] () at (aux34) {};
曹润柘 committed
45
\coordinate (aux35) at ([yshift=\base]aux25);
zengxin committed
46
%\node[auxnode,label={-45:35}] () at (aux35) {};
曹润柘 committed
47
\coordinate (aux37) at ([yshift=\base]aux27);
zengxin committed
48
%\node[auxnode,label={-45:37}] () at (aux37) {};
曹润柘 committed
49 50

\coordinate (aux45) at ([yshift=\base]aux35);
zengxin committed
51
%\node[auxnode,label={-45:45}] () at (aux45) {};
曹润柘 committed
52 53

\coordinate (aux55) at ([yshift=\base]aux45);
zengxin committed
54
%\node[auxnode,label={-45:55}] () at (aux55) {};
曹润柘 committed
55 56 57
\ExtractX{$(aux21)$}
\ExtractY{$(aux55)$}
\coordinate (aux51) at (\XCoord,\YCoord);
zengxin committed
58
%\node[auxnode,label={-45:51}] () at (aux51) {};
曹润柘 committed
59 60 61
\ExtractX{$(aux23)$}
\ExtractY{$(aux55)$}
\coordinate (aux53) at (\XCoord,\YCoord);
zengxin committed
62
%\node[auxnode,label={-45:53}] () at (aux53) {};
曹润柘 committed
63 64 65
\ExtractX{$(aux28)$}
\ExtractY{$(aux55)$}
\coordinate (aux58) at (\XCoord,\YCoord);
zengxin committed
66
%\node[auxnode,label={-45:58}] () at (aux58) {};
曹润柘 committed
67 68 69
\ExtractX{$(aux29)$}
\ExtractY{$(aux55)$}
\coordinate (aux59) at (\XCoord,\YCoord);
zengxin committed
70
%\node[auxnode,label={-45:59}] () at (aux59) {};
曹润柘 committed
71 72

\coordinate (aux68) at ([yshift=\base]aux58);
zengxin committed
73
%\node[auxnode,label={-45:68}] () at (aux68) {};
曹润柘 committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
\end{scope}

\begin{scope}
\node[opnode,circle,opacity=0] (f53) at (aux53) {};
\node[opnode,circle,opacity=0] (u55) at (aux55) {};
% forget gate
{
\draw[emph] (aux21) -- (aux23) -- (aux33);
\draw[-latex,emph] (aux12) -- (aux22) -- (aux23) -- (f53);
\node[opnode,circle,draw=red,thick] () at (aux33) {$\sigma$};
}
{
\draw[standard] (aux21) -- (aux23) -- (aux33);
\draw[-latex,standard] (aux12) -- (aux22) -- (aux23) -- (f53);
\node[opnode,circle] () at (aux33) {$\sigma$};
}
% input gate
{
\node[opnode,circle] (i45) at (aux45) {};
\draw[-latex,emph] (aux21) -- (aux24) |- (i45);
\draw[-latex,emph] (aux21) -- (aux25) -- (u55);
\draw[emph] (aux12) -- (aux22) -- (aux23);
\node[opnode,circle,draw=red,thick] () at (aux34) {$\sigma$};
\node[opnode,rectangle,rounded corners=2pt,inner sep=2pt,draw=red,thick] () at (aux35) {$\mathrm{tanh}$};
98
\node[opnode,circle,draw=red,thick] (i45) at (aux45) {};
曹润柘 committed
99 100 101 102 103 104 105
}
{
\node[opnode,circle] (i45) at (aux45) {};
\draw[-latex,standard] (aux21) -- (aux24) |- (i45);
\draw[-latex,standard] (aux21) -- (aux25) -- (u55);
\node[opnode,circle] () at (aux34) {$\sigma$};
\node[opnode,rectangle,rounded corners=2pt,inner sep=2pt] () at (aux35) {$\mathrm{tanh}$};
106
\node[opnode,circle] (i45) at (aux45) {};
曹润柘 committed
107 108 109 110
}
% cell update
{
\draw[-latex,emph] (aux51) -- (aux59);
111 112
\node[opnode,circle,draw=red,thick] (f53) at (aux53) {};
\node[opnode,circle,draw=red,thick] (u55) at (aux55) {};
曹润柘 committed
113 114 115
}
{
\draw[-latex,standard] (aux51) -- (aux59);
116 117
\node[opnode,circle] (f53) at (aux53) {};
\node[opnode,circle] (u55) at (aux55) {};
曹润柘 committed
118 119 120
}
% output gate
{
121
\node[opnode,circle,draw=red,thick] (o27) at (aux27) {};
曹润柘 committed
122 123 124 125 126 127 128 129 130 131
\draw[-latex,emph] (u55) -| (o27);
\draw[-latex,emph] (aux21) -- (o27);
\draw[emph] (aux12) -- (aux22) -- (aux23);
\node[opnode,circle,draw=red,thick] () at (aux26) {$\sigma$};
\node[opnode,rectangle,rounded corners=2pt,inner sep=2pt,draw=red,thick] () at (aux37) {$\mathrm{tanh}$};

\draw[-latex,emph] (o27) -- (aux29);
\draw[-latex,emph] (o27) -| (aux68);
}
{
132
\node[opnode,circle] (o27) at (aux27) {};
曹润柘 committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
\draw[-latex,standard] (u55) -| (o27);
\draw[-latex,standard] (aux21) -- (o27);
\node[opnode,circle] () at (aux26) {$\sigma$};
\node[opnode,rectangle,rounded corners=2pt,inner sep=2pt] () at (aux37) {$\mathrm{tanh}$};

\draw[-latex,standard] (o27) -- (aux29);
\draw[-latex,standard] (o27) -| (aux68);
}
\end{scope}

\begin{scope}
\node[wordnode,anchor=south] () at ([xshift=0.5\base]aux21) {$\mathbf{h}_{t-1}$};
\node[wordnode,anchor=west] () at (aux12) {$\mathbf{x}_t$};
\node[wordnode,anchor=south] () at ([xshift=0.5\base]aux51) {$\mathbf{c}_{t-1}$};
{
\node[wordnode,anchor=south] () at ([xshift=-0.5\base]aux59) {$\mathbf{c}_{t}$};
}
{
\node[wordnode,anchor=east] () at (aux68) {$\mathbf{h}_{t}$};
\node[wordnode,anchor=south] () at ([xshift=-0.5\base]aux29) {$\mathbf{h}_{t}$};
}
\end{scope}

\begin{pgfonlayer}{background}
曹润柘 committed
157
\node[draw,very thick,rectangle,fill=blue!10!white,rounded corners=5pt,inner sep=4pt,fit=(aux22) (aux58) (u55) (o27)] (LSTM) {};
曹润柘 committed
158 159
\end{pgfonlayer}

160 161 162 163 164 165 166 167 168 169
  %%new
 \draw[-] (i45.north west) -- (i45.south east);
 \draw[-] (i45.north east) -- (i45.south west);
 \draw[-] (f53.north west) -- (f53.south east);
 \draw[-] (f53.north east) -- (f53.south west);
 \draw [-,line width=0.8pt] (u55.north) -- (u55.south);
\draw [-,line width=0.8pt] (u55.west) -- (u55.east);
\draw[-] (o27.north west) -- (o27.south east);
\draw[-] (o27.north east) -- (o27.south west);

曹润柘 committed
170 171 172
\begin{scope}
{
% forget gate formula
曹润柘 committed
173
\node[formulanode,anchor=south east,text width=10em] () at ([shift={(4\base,1.5\base)}]aux51) {遗忘门\\$\mathbf{f}_t=\sigma(\mathbf{W}_f[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_f)$};
曹润柘 committed
174 175 176
}
{
% input gate formula
曹润柘 committed
177
\node[formulanode,anchor=north east,text width=10em] () at ([shift={(4\base,-1.5\base)}]aux21) {输入门\\$\mathbf{i}_t=\sigma(\mathbf{W}_i[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_i)$\\$\hat{\mathbf{c}}_t=\mathrm{tanh}(\mathbf{W}_c[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_c)$};
曹润柘 committed
178 179 180
}
{
% cell update formula
曹润柘 committed
181
\node[formulanode,anchor=south west,text width=10em] () at ([shift={(-4\base,1.5\base)}]aux59) {记忆更新\\$\mathbf{c}_{t}=\mathbf{f}_t\cdot \mathbf{c}_{t-1}+\mathbf{i}_t\cdot \hat{\mathbf{c}}_t$};
曹润柘 committed
182 183 184
}
{
% output gate formula
曹润柘 committed
185
\node[formulanode,anchor=north west,text width=10em] () at ([shift={(-4\base,-1.5\base)}]aux29) {输出门\\$\mathbf{o}_t=\sigma(\mathbf{W}_o[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_o)$\\$\mathbf{h}_{t}=\mathbf{o}_t\cdot \mathrm{tanh}(\mathbf{c}_{t})$};
曹润柘 committed
186 187 188 189
}
\end{scope}
\end{tikzpicture}