Advanced RNNs
Slides mostly from the Stanford NLP course by Prof. Chris Manning and others
1. The Simple RNN Language Model
4
the students
opened
their
words / one-hot vectors
books
laptops
word embeddings
a
zoo
output distribution
Note: this input sequence could be much longer now!
hidden states
is the initial hidden state
Training the parameters of RNNs: Backpropagation for RNNs
12
…
…
Question: What’s the derivative of
w.r.t. the repeated weight matrix ?
Answer:
“The gradient w.r.t. a repeated weight is the sum of the gradient w.r.t. each time it appears”
Why?
Backpropagation for RNNs
…
…
Question: How do we calculate this?
Answer: Backpropagate over timesteps i=t,…,0, summing gradients as you go.
This algorithm is called “backpropagation through time” [Werbos, P.G., 1988, Neural Networks 1, and others]
15
3. Problems with Vanishing and Exploding Gradients
29
We will backpropagate the overall loss.
Vanishing gradient intuition
30
?
We will backpropagate the overall loss.
Vanishing gradient intuition
31
chain rule!
Vanishing gradient intuition
32
chain rule!
Vanishing gradient intuition
33
chain rule!
Vanishing gradient intuition
34
What happens if these are small?
Vanishing gradient problem: When these are small, the gradient signal gets smaller and smaller as it backpropagates further
Why is vanishing gradient a problem?
37
Gradient signal from far away is lost because it’s much smaller than gradient signal from close-by.
So, model weights are updated only with respect to near effects, not long-term effects.
Effect of vanishing gradient on RNN-LM
38
Another example of errors by RNN LMs due to vanishing gradient:
The writer of the books
The LM is asked to predict the next word between ‘is’ and ‘are’
Due to vanishing gradients, RNN-LMs are prone to make mistakes in identifying such subject-verb dependencies [Linzen et al 2016]
How to fix the vanishing gradient problem?
41
We want this separate memory to be more flexible, so that we can use it to preserve some specific information (instead of being rewritten at every stage).
4. Long Short-Term Memory RNNs (LSTMs)
42
problem.
https://www.bioinf.jku.at/publications/older/2604.pdf “Learning to Forget: Continual Prediction with LSTM”, Gers, Schmidhuber, and Cummins, 2000. https://dl.acm.org/doi/10.1162/089976600300015015 “Long short-term memory”, Hochreiter and Schmidhuber, 1997.
Probabilistic gates: each element of a gate is open with a probability
Long Short-Term Memory (LSTM)
We have a sequence of inputs 𝑥(𝑡), and we will compute a sequence of hidden states ℎ(𝑡) and cell states
𝑐(𝑡). On timestep t:
All these are vectors of same length n
Forget gate: controls what is kept vs forgotten, from previous cell state
Input gate: controls what parts of the new cell content are written to cell
Output gate: controls what parts of cell are output to hidden state
New cell content: this is the new content to be written to the cell
Cell state: erase (“forget”) some content from last cell state, and write (“input”) some new cell content
Hidden state: read (“output”) some content from the cell
Sigmoid function: all gate values are between 0 and 1
Gates are applied using element-wise
These 3 equations are exactly similar to the eqn. for hidden state in vanilla RNN
(or Hadamard) product: ⊙
43
Each gate has its own set of parameters W, U, b (which will be learned)
Long Short-Term Memory (LSTM)
We have a sequence of inputs 𝑥(𝑡), and we will compute a sequence of hidden states ℎ(𝑡) and cell states
𝑐(𝑡). On timestep t:
All these are vectors of same length n
Forget gate: controls what is kept vs forgotten, from previous cell state
Input gate: controls what parts of the new cell content are written to cell
Output gate: controls what parts of cell are output to hidden state
New cell content: this is the new content to be written to the cell
Cell state: erase (“forget”) some content from last cell state, and write (“input”) some new cell content
Hidden state: read (“output”) some content from the cell
Sigmoid function: all gate values are between 0 and 1
Gates are applied using element-wise
For the new cell state, we want to remember some, but probably not all, of the previous cell state
(or Hadamard) product: ⊙
43
A candidate cell update (to be used to compute the new cell state)
To get the new cell state c(t), sum
The new hidden state, obtained from new cell state
Long Short-Term Memory (LSTM)
44
You can think of the LSTM equations visually like this:
Long Short-Term Memory (LSTM)
45
ct-1
ht-1
c
t
ht
f
t
it
ot
ct
t
c~
You can think of the LSTM equations visually like this:
Compute the forget gate
Forget some cell content
Compute the input gate
Compute the new cell content
Compute the output gate
Write some new cell content
Output some cell content to the hidden state
The + sign is the secret!
How does LSTM solve vanishing gradients?
46
LSTMs: real-world success
47
Source: "Findings of the 2016 Conference on Machine Translation (WMT16)", Bojar et al. 2016, http://www.statmt.org/wmt16/pdf/W16-2301.pdf Source: "Findings of the 2018 Conference on Machine Translation (WMT18)", Bojar et al. 2018, http://www.statmt.org/wmt18/pdf/WMT028.pdf Source: "Findings of the 2019Conference on Machine Translation (WMT19)", Barrault et al. 2019, http://www.statmt.org/wmt18/pdf/WMT028.pdf
Another RNN architecture to address vanishing gradients: GRU (Gated Recurrent Unit)
GRUs are simplified versions of LSTMs
LSTM - 3 gates - input gate, output gate, forget gate
GRU - 2 gates - update gate and reset gate. The update gate controls how much of the previous hidden state should be retained, and the reset gate determines how much of the past information to forget.
LSTM has two memories (hidden state and cell state) while GRU has just one memory (hidden state). GRU has fewer parameters, can train faster. GRU and LST are comparable performance-wise.
Your task: learn the details of GRU
We have now got a better representation of words than Word embeddings: Contextual or context-sensitive representations
The boat was tied on the river bank …
I deposited the account cheque in the bank …
Will the RNN / LSTM representation of the word “bank” be the same in the two sentences?
We have now got a better representation of words than Word embeddings: Contextual or context-sensitive representations
RNN
Word
embedding
an ant went to
The boat was tied
the river
on the
bank
river
bank …
I deposited the account cheque in the bank …
Will the RNN / LSTM representation of the word “bank” be the same in the two sentences?
5. Bidirectional and Multi-layer RNNs: motivation
51
terribly
exciting
!
the
movie was
positive
Sentence encoding
element-wise mean/max
element-wise mean/max
We can regard this hidden state as a representation of the word “terribly” in the context of this sentence. We call this a contextual representation.
These contextual representations only contain information about the left context (e.g. “the movie was”).
What about right context?
In this example, “exciting” is in the right context and this modifies the meaning of “terribly” (from negative to positive)
Task: Sentiment Classification
Bidirectional RNNs
52
was terribly
exciting
!
the
movie
Forward RNN
A separate RNN, with its own parameters, learning a backward representation of the sentence
Backward RNN
Concatenated hidden states
This contextual representation of “terribly” has both left and right context!
Bidirectional RNNs
53
Forward RNN
Backward RNN Concatenated hidden states
This is a general notation to mean “compute one forward step of the RNN” – it could be a vanilla, LSTM or GRU computation.
We regard this as “the hidden state” of a bidirectional RNN. This is what we pass on to the next parts of the network.
Generally, these two RNNs have separate weights
On timestep t:
Bidirectional RNNs: simplified diagram
54
terribly
exciting
!
the
movie was
The two-way arrows indicate bidirectionality and the depicted hidden states are assumed to be the concatenated forwards+backwards states
Bidirectional RNNs
55
Multi-layer RNNs
56
compute higher-level features.
Examples:
Lower-level features - word properties, such as POS or whether the word is part of a named entiry
Higher-level features - about the overall structure of a sentence / phrase, e.g., the meaning, associated sentiment, etc.
Multi-layer RNNs
57
was terribly
exciting
!
the
movie
RNN layer 1
RNN layer 2
RNN layer 3
The hidden states from RNN layer i are the inputs to RNN layer i+1
There is only one unit at each layer, i.e., only one set of parameter matrices at each layer.
The images are shown unfolded / unrolled over time.
Multilayer RNN
h”
2
h*
•••
Multi-layer RNNs in practice
feed-forward networks)
58
“Massive Exploration of Neural Machine Translation Architecutres”, Britz et al, 2017. https://arxiv.org/pdf/1703.03906.pdf
Multi-layer or stacked RNNs allow a network
to compute more complex representations of text.