Deep Learning (DEEP-0001)�
RNNs
Biological sequence
Sequence learning problems
In sequence problems, the current output is dependent on the previous input and the length of the input may be not fixed.
Time series
Natural Language Processing
Computer Vision
Protein secondary structure prediction
stock market price predictions
Machine Translation, Language generation
Human Action Recognition
Recurrent Neural Networks
Recurrent Neural Network
RNN
Simple/Standard
MLP
input
output
Recurrent Neural Network Structure
Recurrent Neural Network Structure
RNN Models
RNN
MLP
NLP Workflow
Example sentence: “The cat sat.” (lowercased). The bold row follows “cat” through each step.
1 Tokenize
the
cat
sat
.
Split text into words, subwords or characters
2 Vocabulary
the → 0
cat → 1
sat → 2
. → 3
Give each unique token an index; |V| = 4
3 One-hot
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
|V|-long vector with a single 1
4 Embedding
[0.2 −0.5]
[0.9 0.1]
[0.8 0.3]
[−0.1 0.0]
Dense d-dim vector learned with the model. Not required. Can also use word2vec or GloVe.
5 RNN input
x1 → h1
x2 → h2
x3 → h3
x4 → h4
One vector per time step
NLP Workflow
Time Series
Recurrent Neural Network
RNN Forward Pass
Wxh (hidden_size, vocab_size)
Whh (hidden_size, hidden_size)
Why (vocab_size, hidden_size)
Backpropagation Through Time
RNN – Challenges
Long Short-Term Memory (LSTM)
“I grew up in France… I speak fluent French.”
Long Short-Term Memory (LSTM)
Cell State (Long-Term)
Forget gate layer
Maria moved to Tokyo last year, but her brother John stayed in Lisbon because he loved the city.
Input Gate Layer
Input Gate Layer
Forget Gate vs. Input Gate
Ct = ft * Ct−1 + it * C̃t ft: how much old memory to keep it: how much new candidate to write
ft | it | What the cell does |
1 | 0 | Hold: keep the memory, ignore the input |
0 | 1 | Replace: drop the old, write the new |
1 | 1 | Accumulate: keep the old, add the new |
0 | 0 | Reset: clear the memory |
Language model: a new subject is the Replace case. The old gender is dropped and the new one is written.
Output Gate (Short-Term)
Output Gate: Memory vs. Output
ht = ot * tanh(Ct) ot: how much of the memory to show at this step
ot | What ht shows |
≈ 1 | Expose: the memory flows into the output |
≈ 0 | Hide: the memory is kept but not shown |
mixed | Select: only the units needed right now |
ot is a vector: each memory unit has its own gate.
Language model: in “I grew up in France… I speak fluent ___”, the cell keeps “France” for many steps. ot opens that unit only when the next word needs it.
Why not output Ct directly?
Memory ≠ current output
Ct stores long-term facts. ht feeds the next prediction, so it needs only what matters now.
Dimensionality
Variations - Bidirectional RNN�
Variations - Deep/Stacked RNN�
Convolutional RNN�
GRU (Gated Recurrent Unit) vs. LSTM
Update gate
zt = σ(Wz·[ht−1, xt])
Reset gate
rt = σ(Wr·[ht−1, xt])
Candidate
h̃t = tanh(W·[rt * ht−1, xt])
New state
ht = (1 − zt) * ht−1 + zt * h̃t
GRU: merges the cell state into ht and uses two gates instead of three.
GRU vs. LSTM
zt ≈ 0 acts like ft = 1, it = 0 in an LSTM.
| LSTM | GRU |
Gates | 3 (ft, it, ot) | 2 (zt, rt) |
Memory | Ct plus ht | ht only |
Keep/write | separate ft, it | coupled: 1 − zt, zt |
Output | filtered by ot | whole state |
Weight sets | 4 | 3 (25% fewer) |
Accuracy | baseline | comparable |