OVERVIEW
WHY RNNs?
As soon as we introduce a new architecture, questions arises in the minds of everyone, that is why do we need it? What does it do better that the other models weren’t capable of? So, now we are going to answer these questions!
RNNs do sequence modelling like never before!
In various applications where we require our model to give an output based on a given sequence of information, it could be anything……
SPEECH RECOGNITION:
Quite a common application these days(everyone with smartphone will know about this)
SENTIMENT CLASSIFICATION:
Another popular application of sequence model.
The output can be in the form of ratings or can depict the sentiment of sentence.
Or a sequence of images in case of video analysis
Let’s take the case for language modelling to delve deeper into the architecture of RNNs.
8
hidden states
input sequence (any length)
…
…
…
Core idea: Apply the same weights repeatedly
outputs (optional)
1. The Simple RNN Language Model
9
the students
opened
their
words / one-hot vectors
books
laptops
word embeddings
a
zoo
output distribution
hidden states
is the initial hidden state
Training an RNN Language Model
for every step t.
10
Training an RNN Language Model
11
= negative log prob of “students”
Loss
Predicted prob dists
…
Corpus
the
students
opened
their
exams
…
Training an RNN Language Model
12
Loss
Predicted prob dists
…
= negative log prob of “opened”
Corpus
the
students
opened
their
exams
…
Training an RNN Language Model
13
Loss
Predicted prob dists
…
= negative log prob of “their”
Corpus
the
students
opened
their
exams
…
Training an RNN Language Model
14
Loss
Predicted prob dists
…
= negative log prob of “exams”
Corpus
the
students
opened
their
exams
…
Training an RNN Language Model
15
+
+
+
+ … =
…
Loss
Predicted prob dists
“Teacher forcing”
Corpus
the
students
opened
their
exams
…
Training a RNN Language Model
16
is too
Training the parameters of RNNs: Backpropagation for RNNs
17
…
…
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?
Multivariable Chain Rule
18
Gradients sum at outward branches!
Backpropagation for RNNs: Proof sketch
19
…
In our example:
equals
equals
equals
Apply the multivariable chain rule:
= 1
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
Generating text with a RNN Language Model
Just like a n-gram Language Model, you can use an RNN Language Model to generate text by repeated sampling. Sampled output becomes next step’s input.
my favorite
season
is
…
favorite
sample
season
sample
is
sample
spring
sample
spring
16
Generating text with an RNN Language Model
22
Let’s have some fun!
Source: https://medium.com/deep-writing/harry-potter-written-by-artificial-intelligence-8a9431803da6
Why should we care about Language Modeling?
23
Recap
24
2. Other RNN uses: RNNs can be used for sequence tagging
e.g., part-of-speech tagging, named entity recognition
25
knocked
over
the
vase
the startled cat
VBN
IN
DT
NN
DT
JJ
NN
RNNs can be used for sentence classification
e.g., sentiment classification
26
the
movie
a
lot
overall
I
enjoyed
positive
Sentence encoding
How to compute
sentence encoding?
RNNs can be used for sentence classification
27
the
movie
a
lot
overall
I
enjoyed
positive
Sentence encoding
equals
How to compute sentence encoding?
Basic way:
Use final hidden state
e.g., sentiment classification
RNNs can be used for sentence classification
28
the
movie
a
lot
overall
I
enjoyed
positive
Sentence encoding
How to compute sentence encoding?
Usually better: Take element-wise max or mean of all hidden states
e.g., sentiment classification
RNNs can be used as a language encoder module
29
e.g., question answering, machine translation, many other tasks!
Context: Ludwig van Beethoven was a German composer and pianist. A crucial figure …
Beethoven ?
Question: what nationality was
Here the RNN acts as an encoder for the Question (the hidden states represent the Question). The encoder is part of a larger neural system.
Answer: German
lots of neural
architecture
lots of neural
architecture
RNN-LMs can be used to generate text
30
e.g., speech recognition, machine translation, summarization
RNN-LM
what’s the
the weather
what’s
This is an example of a conditional language model.
We’ll see Machine Translation in much more detail next class.
Input (audio)
<START>
conditioning
3. Problems with Vanishing and Exploding Gradients
31
32
Vanishing gradient intuition
?
Vanishing gradient intuition
33
chain rule!
Vanishing gradient intuition
34
chain rule!
Vanishing gradient intuition
35
chain rule!
Vanishing gradient intuition
36
What happens if these are small?
Vanishing gradient problem: When these are small, the gradient signal gets smaller and smaller as it backpropagates further
Vanishing gradient proof sketch (linear case)
35
Source: “On the difficulty of training recurrent neural networks”, Pascanu et al, 2013. http://proceedings.mlr.press/v28/pascanu13.pdf
(and supplemental materials), at http://proceedings.mlr.press/v28/pascanu13-supp.pdf
If Wh is “small”, then this term gets exponentially problematic as becomes large
(chain rule)
(chain rule)
(value of )
Vanishing gradient proof sketch (linear case)
as a basis:
for some
dependent on dimensionality and
Source: “On the difficulty of training recurrent neural networks”, Pascanu et al, 2013. http://proceedings.mlr.press/v28/pascanu13.pdf
(and supplemental materials), at http://proceedings.mlr.press/v28/pascanu13-supp.pdf
36
(eigenvectors) using the eigenvectors of
Approaches 0 as
grows, so gradient vanishes
sufficient but
not necessary
Why is vanishing gradient a problem?
39
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
40
Why is exploding gradient a problem?
41
learning rate
gradient
Gradient clipping: solution for exploding gradient
40
Source: “On the difficulty of training recurrent neural networks”, Pascanu et al, 2013. http://proceedings.mlr.press/v28/pascanu13.pdf
How to fix the vanishing gradient problem?
43
4. Long Short-Term Memory RNNs (LSTMs)
44
“Long short-term memory”, Hochreiter and Schmidhuber, 1997. 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 (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
43
Gates are applied using element-wise (or Hadamard) product: ⊙
46
Long Short-Term Memory (LSTM)
You can think of the LSTM equations visually like this:
Long Short-Term Memory (LSTM)
47
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
Gated Recurrent Units (GRU)
28
"Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation", Cho et al. 2014, https://arxiv.org/pdf/1406.1078v3.pdf
Update gate: controls what parts of hidden state are updated vs preserved
Reset gate: controls what parts of previous hidden state are used to compute new content
Hidden state: update gate simultaneously controls what is kept from previous hidden state, and what is updated to new hidden state content
New hidden state content: reset gate selects useful parts of prev hidden state. Use this and current input to compute new hidden content.
How does this solve vanishing gradient? Like LSTM, GRU makes it easier to retain info long-term (e.g. by setting update gate to 0)
Is vanishing/exploding gradient just a RNN problem?
For example:
48
"Deep Residual Learning for Image Recognition", He et al, 2015. https://arxiv.org/pdf/1512.03385.pdf
Is vanishing/exploding gradient just a RNN problem?
49
Other methods:
”Densely Connected Convolutional Networks", Huang et al, 2017. https://arxiv.org/pdf/1608.06993.pdf
”Highway Networks", Srivastava et al, 2015. https://arxiv.org/pdf/1505.00387.pdf
Is vanishing/exploding gradient just a RNN problem?
53
”Learning Long-Term Dependencies with Gradient Descent is Difficult", Bengio et al. 1994, http://ai.dinfo.unifi.it/paolo//ps/tnn-94-gradient.pdf
5. Bidirectional and Multi-layer RNNs: motivation
54
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
55
was terribly
exciting
!
the
movie
Forward RNN
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
57
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
58
Multi-layer RNNs
59
Multi-layer RNNs
60
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
Multi-layer RNNs in practice
58
“Massive Exploration of Neural Machine Translation Architecutres”, Britz et al, 2017. https://arxiv.org/pdf/1703.03906.pdf
In summary
59
Lots of new information today! What are some of the practical takeaways?
1. LSTMs are powerful
2. Clip your gradients
3. Use bidirectionality when possible
4. Multi-layer RNNs are more powerful, but you might need skip connections if it’s deep