-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tex
133 lines (89 loc) · 4.11 KB
/
main.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
\documentclass[tikz]{standalone}
\standaloneconfig{border=1cm}
\usepackage[utf8]{inputenc}
\usetikzlibrary{positioning}
\title{GRU}
\author{James Allingham and Taliesin Beynon}
\date{May 2019}
\begin{document}
\def\vec#1{\mathbf{#1}}
\tikzset{nicecolor/.style={draw=#1!50!black, top color=#1!70!black!20!white, bottom color=#1!70!black!20!white}}
% colors
\standaloneconfig{border=0.5ex}
\colorlet{GateColor}{red}
\colorlet{NonLinearColor}{green}
\colorlet{OperatorColor}{blue}
\colorlet{FrameColor}{gray!10!white}
% internal node shape
\tikzset{opnode/.style={circle, minimum width=5ex}}
\tikzset{sigma/.style={opnode, nicecolor=NonLinearColor, font={$\sigma$}}}
\tikzset{tanh/.style={opnode, nicecolor=NonLinearColor, label=center:{\small tanh}}}
\tikzset{plus/.style ={opnode, nicecolor=OperatorColor, font={$+$}}}
\tikzset{times/.style={opnode, nicecolor=OperatorColor, font={$\times$}}}
\tikzset{oneminus/.style ={opnode, nicecolor=OperatorColor, label=center:{\small{$-1$}}}}
% gate shape
\tikzset{gate/.style={rectangle, minimum width=18ex, minimum height=0.6cm, nicecolor=GateColor, label={center:{\textsc{#1}}}}}
% arrow style
\tikzset{arrow/.style={-stealth, thick, draw=gray!80!black}}
\begin{tikzpicture}
% \draw[help lines](0,-5) grid (10,5);
% overall frame
\draw[solid, nicecolor=FrameColor] (-1.25, -1.75) rectangle (7.3, 1.75);
\def\narrowdot{\hspace{-0.2ex}\cdot \hspace{-0.2ex}}
\def\narrowminus{\hspace{-0.2ex}-\hspace{-0.2ex}}
% gates
\tikzset{smallgate/.style={minimum width=11ex}};
\node[gate={reset}, smallgate] (RG1) {};
\node[gate={memory}, smallgate, below=3ex of RG1] [xshift=16ex] (MG1) {};
\node[gate={input}, smallgate, above=3ex of RG1] (IG1) {};
% inputs
\node[left=8ex of IG1, yshift=1ex] (X) {$\vec{x}_t$};
\node[anchor=east] at (IG1.west |- MG1) [xshift=-8ex, yshift=-1ex] (Y) {$\vec{y}_{t-1}$};
% connections between inputs and gates
\foreach \dst in {MG1, RG1, IG1} {
\draw[arrow] (X.east) -- ([xshift=2ex]X.east) |- ([yshift=1ex]\dst.west);
\draw[arrow] (Y.east) -- ([xshift=3.5ex]Y.east) |- ([yshift=-1ex]\dst.west);
}
% gate non-linearities
\node[sigma, right=3ex of IG1] (IG) {}; \draw[arrow] (IG1) -- (IG);
\node[tanh, right=3ex of MG1] (MG) {}; \draw[arrow] (MG1) -- (MG);
\node[sigma, right=3ex of RG1] (RG) {}; \draw[arrow] (RG1) -- (RG);
% reset gate to memory gate
\draw[arrow] (RG.east) -| (MG1.north);
% mixing ops
\node[times, right=3ex of MG] (IP) {}; % inverted product
\node[oneminus] at (RG -| IP) (II) {}; % inverted input
\node[plus, right=3ex of IP] (ADD) {}; % addition of product and inverted product
\node[times] at (IG -| ADD) (P) {}; % product
\draw[arrow] (MG) -- (IP);
\draw[arrow] (II) -- (IP);
\draw[arrow] (IG) -| (II);
\draw[arrow] (IG) -- (P) -- (ADD);
\draw[arrow] (IP) -- (ADD);
% output node and top Y
\node[right=of ADD] (Y1) {$\vec{y}_t$} {};
\draw[arrow] (ADD.east) -- (Y1.west);
\node[above=4ex of P] (Y2) {$\vec{y}_{t-1}$};
\draw[arrow] (Y2) -- (P);
% input/reset gate inlay
\node[draw=none] (gatetitle) at (0.1, -3.6) {\textsc{input/reset gate}};
\node[gate={}, minimum height=4ex, above=0cm of gatetitle.north] (gate) {$W_1 \vec x + W_2 \vec y + \vec b$};
\node[] at [left=5ex of gate] [yshift=2ex] (xgate) {$\vec{x}$};
\node[] at [left=5ex of gate] [yshift=-2ex] (ygate) {$\vec{y}$};
\node[] at [right=3ex of gate] (zgate) {};
\draw[arrow] (xgate) -- ([yshift=1ex]gate);
\draw[arrow] (ygate) -- ([yshift=1ex]gate);
\draw[arrow] (gate) -- (zgate);
% memory gate inlay
\node[draw=none] (gatetitle) at (5.65, -3.6) {\textsc{memory gate}};
\node[gate={}, minimum height=4ex, above=0cm of gatetitle.north] (gate) {$W_1 \vec x + \vec r \circ W_2 \vec y + \vec b $};
\node[] at [left=5ex of gate] [yshift=2ex] (xgate) {$\vec{x}$};
\node[] at [left=5ex of gate] [yshift=-2ex] (ygate) {$\vec{y}$};
\node[] at [above=2ex of gate] (rgate) {$\vec{r}$};
\node[] at [right=3ex of gate] (zgate) {};
\draw[arrow] (xgate) -- ([yshift=1ex]gate);
\draw[arrow] (ygate) -- ([yshift=1ex]gate);
\draw[arrow] (rgate) -- ([yshift=1ex]gate);
\draw[arrow] (gate) -- (zgate);
\end{tikzpicture}
\end{document}