You are on page 1of 99

Recurrent Neural Networks

M. Soleymani
Sharif University of Technology
Fall 2017

Most slides have been adopted from Fei Fei Li and colleagues lectures, cs231n, Stanford 2017
and some from Socher lectures, cs224d, Stanford, 2017. .
“Vanilla” Neural Networks

“Vanilla” NN
Recurrent Neural Networks: Process Sequences

“Vanilla” NN e.g. Image Captioning e.g. Sentiment e.g. Machine Translation e.g. Video
image -> seq of words Classification seq of words -> seq of words classification on
seq of words -> sentiment frame level
Recurrent Neural Network

usually want to
y
predict a vector at
some time steps

RNN

x
Recurrent Neural Network
We can process a sequence of vectors x by
applying a recurrence formula at every y
time step:

RNN

new state old state input vector at


some time step
some function
x
with parameters W
Recurrent Neural Network
We can process a sequence of vectors x by
applying a recurrence formula at every y
time step:

RNN

Notice: the same function and the same x


set of parameters are used at every time
step.
(Vanilla) Recurrent Neural Network
The state consists of a single “hidden” vector h:

RNN

x
RNN: Computational Graph
RNN: Computational Graph

Re-use the same weight matrix at every time-step


RNN: Computational Graph: Many to One
RNN: Computational Graph: Many to Many
RNN: Computational Graph: Many to Many
RNN: Computational Graph: Many to Many
RNN: Computational Graph: One to Many
Sequence to Sequence: Many-to-one + one-to-many
Character-level language model example
Vocabulary:
[h,e,l,o]
y
Example training
sequence:
“hello” RNN

x
Character-level language model example
Vocabulary:
[h,e,l,o]

Example training
sequence:
“hello”
Character-level language model example
Vocabulary:
[h,e,l,o]

Example training
sequence:
“hello”
Character-level language model example
Vocabulary:
[h,e,l,o]

Example training
sequence:
“hello”
Example: Character-level Language Model Sampling
• Vocabulary: [h,e,l,o]
• At test-time sample
characters one at a time,
feed back to model
Example: Character-level Language Model Sampling
• Vocabulary: [h,e,l,o]
• At test-time sample
characters one at a time,
feed back to model
Example: Character-level Language Model Sampling
• Vocabulary: [h,e,l,o]
• At test-time sample
characters one at a time,
feed back to model
Example: Character-level Language Model Sampling
• Vocabulary: [h,e,l,o]
• At test-time sample
characters one at a time,
feed back to model
min-char-rnn.py gist:
112 lines of Python

(https://gist.github.com/karpathy
/d4dee566867f8291f086)
Language Modeling: Example I

RNN

x
at first:

train more

train more

train more
open source textbook on algebraic geometry

Latex source
Generated
C code
Searching for interpretable cells
Searching for interpretable cells
Searching for interpretable cells

quote detection cell


Searching for interpretable cells

line length tracking cell


Searching for interpretable cells

if statement cell
Searching for interpretable cells

quote/comment
if statement cell cell
Searching for interpretable cells

code depth cell


Backpropagation through time
Forward through entire sequence to compute loss, then
backward through entire sequence to compute gradient
Truncated Backpropagation through time

Run forward and backward through


chunks of the sequence instead of whole
sequence
Truncated Backpropagation through time

Carry hidden states


forward in time forever,
but only backpropagate
for some smaller
number of steps
Truncated Backpropagation through time
Example: Language Models
• A language model computes a probability for a sequence of words:
𝑃(𝑤1 , … , 𝑤𝑇 )

• Useful for machine translation, spelling correction, and …


– Word ordering: p(the cat is small) > p(small the is cat)
– Word choice: p(walking home after school) > p(walking house after school)
Example: RNN language model
• Given list of word vectors:
𝑥1 , 𝑥2 , … , 𝑥𝑇
• At a single time step:
ℎ𝑡 = tanh 𝑊𝑥ℎ 𝑥𝑡 + 𝑊ℎℎ ℎ𝑡−1
• Output:
𝑦𝑡 = softmax 𝑊ℎ𝑦 ℎ𝑡

𝑃(𝑦𝑡 = 𝑣𝑗 |𝑥1 , … , 𝑥𝑡 ) ≈ 𝑦𝑡,𝑗


Example: RNN language model
• Given list of word vectors:
𝑥1 , 𝑥2 , … , 𝑥𝑇
• At a single time step:
ℎ𝑡 = tanh 𝑊𝑥ℎ 𝑥𝑡 + 𝑊ℎℎ ℎ𝑡−1
• Output:
𝑦𝑡 = softmax 𝑊ℎ𝑦 ℎ𝑡

𝑃(𝑦𝑡 = 𝑣𝑗 |𝑥1 , … , 𝑥𝑡 ) ≈ 𝑦𝑡,𝑗

ℎ0 is some initialization vector for the hidden layer at time step 0

𝑥𝑡 is the column vector at time step t


Example: RNN language model loss
• 𝑦 ∈ ℝ 𝑉 is a probability distribution over the vocabulary
• Cross entropy loss function at location t of the sequence:
|𝑉|
𝑦𝑡,𝑗 = 1 when 𝑤𝑡 must be
𝐸𝑡 = − 𝑦𝑡,𝑗 log 𝑦𝑡,𝑗 the word 𝑗 of vocabulary
𝑗=1
• Cost function over the entire sequence:
𝑇 |𝑉|
1
𝐸=− 𝑦𝑡,𝑗 log 𝑦𝑡,𝑗
𝑇
𝑡=1 𝑗=1
Training RNN
Training RNN
Training RNN
ℎ𝑗 = 𝑊ℎℎ 𝑓 ℎ𝑗−1 + 𝑊𝑥ℎ 𝑥𝑗

𝜕ℎ𝑗 𝑇 𝜕ℎ𝑗,𝑚
= 𝑊ℎℎ diag 𝑓 ′ ℎ𝑗−1 𝜕ℎ𝑗−1,𝑛
𝑇
= 𝑊ℎℎ 𝑛,.
𝑓′ 𝑚
𝜕ℎ𝑗−1

𝜕ℎ𝑗 𝑇
≤ 𝑊ℎℎ 𝑓 ′ ℎ𝑗−1 ≤ 𝛽𝑊 𝛽ℎ
𝜕ℎ𝑗−1
𝜕ℎ𝑡 𝑡 𝜕ℎ𝑗 𝑡
𝑇
= = 𝑊ℎℎ diag 𝑓 ′ ℎ𝑗−1 ≤ 𝛽𝑊 𝛽ℎ 𝑡−𝑘
𝜕ℎ𝑘 𝑗=𝑘+1 𝜕ℎ𝑗−1 𝑗=𝑘+1

• This can become very small or very large quickly (vanishing/exploding gradients)
[Bengio et al 1994].
Training RNNs is hard
• Multiply the same matrix at each time step during forward prop

• Ideally inputs from many time steps ago can modify output y
The vanishing gradient problem: Example
• In the case of language modeling words from time steps far away are
not taken into consideration when training to predict the next word

• Example: Jane walked into the room. John walked in too. It was late in
the day. Jane said hi to ____
Vanilla RNN Gradient Flow
Vanilla RNN Gradient Flow
Vanilla RNN Gradient Flow

Computing gradient of h0 involves many factors of W


(and repeated tanh)

Largest singular value > 1: Exploding gradients


Largest singular value < 1: Vanishing gradients
Trick for exploding gradient: clipping trick
• The solution first introduced by Mikolov is to clip gradients to a
maximum value.

• Makes a big difference in RNNs.


Gradient clipping intuition

• Error surface of a single hidden unit RNN


– High curvature walls
• Solid lines: standard gradient descent trajectories
• Dashed lines gradients rescaled to fixed size
Vanilla RNN Gradient Flow

Computing gradient of h0 involves many factors Gradient clipping: Scale Computing


of W (and repeated tanh) gradient if its norm is too big

Largest singular value > 1: Exploding gradients

Largest singular value < 1: Vanishing gradients


Change RNN architecture
For vanishing gradients: Initialization + ReLus!
• Initialize Ws to identity matrix I and activations to RelU
• New experiments with recurrent neural nets.

Le et al. A Simple Way to Initialize Recurrent Networks of


Rectified Linear Unit, 2015.
Better units for recurrent models
• More complex hidden unit computation in recurrence!
– ℎ𝑡 = 𝐿𝑆𝑇𝑀(𝑥𝑡 , ℎ𝑡−1 )

– ℎ𝑡 = 𝐺𝑅𝑈(𝑥𝑡 , ℎ𝑡−1 )

• Main ideas:
– keep around memories to capture long distance dependencies
– allow error messages to flow at different strengths depending on
the inputs
Long Short Term Memory (LSTM)
Long-short-term-memories (LSTMs)
ℎ𝑡−1
• Input gate (current cell matterst 𝑖𝑡 = 𝜎 𝑊𝑖 + 𝑏𝑖
𝑥𝑡
ℎ𝑡−1
• Forget (gate 0, forget past): 𝑓𝑡 = 𝜎 𝑊𝑓 + 𝑏𝑓
𝑥𝑡
ℎ𝑡−1
• Output (how much cell is exposed): 𝑜𝑡 = 𝜎 𝑊𝑜 + 𝑏𝑜
𝑥𝑡
ℎ𝑡−1
• New memory cell: 𝑔𝑡 = tanh 𝑊𝑔 + 𝑏𝑔
𝑥𝑡
• Final memory cell: 𝑐𝑡 = 𝑖𝑡 ∘ 𝑔𝑡 + 𝑓𝑡 ∘ 𝑐𝑡−1
• Final hidden state: ℎ𝑡 = 𝑜𝑡 ∘ tanh 𝑐𝑡
Some visualization

By Chris Ola: http://colah.github.io/posts/2015-08-Understanding-LSTMs/


LSTM Gates
• Gates are ways to let information through (or not):
– Forget gate: look at previous cell state and current input, and decide which
information to throw away.
– Input gate: see which information in the current state we want to update.
– Output: Filter cell state and output the filtered result.
– Gate or update gate: propose new values for the cell state.

• For instance: store gender of subject until another subject is seen.


Long Short Term Memory (LSTM)
Long Short Term Memory (LSTM)
[Hochreiter et al., 1997]
Long Short Term Memory (LSTM)
[Hochreiter et al., 1997]
Long Short Term Memory (LSTM)
[Hochreiter et al., 1997]
Long Short Term Memory (LSTM)
[Hochreiter et al., 1997]
Derivatives for LSTM
𝑧 = 𝑓 𝑥1 , 𝑥2 = 𝑥1 ∘ 𝑥2

𝜕𝐸 𝜕𝐸 𝜕𝑧
=
𝜕𝑥1 𝜕𝑧 𝜕𝑥1
𝜕𝐸
= ∘ 𝑥2
𝜕𝑧
GRUs
• Gated Recurrent Units (GRU) introduced by Cho et al. 2014

• Update gate
ℎ𝑡−1
𝑧𝑡 = 𝜎 𝑊𝑧 + 𝑏𝑧
𝑥𝑡
• Reset gate
ℎ𝑡−1
𝑟𝑡 = 𝜎 𝑊𝑟 + 𝑏𝑟
𝑥𝑡
• Memory
𝑟𝑡 ∘ ℎ𝑡−1
ℎ𝑡 = tanh 𝑊𝑚 + 𝑏𝑚
𝑥𝑡
• Final Memory
ℎ𝑡 = 𝑧𝑡 ∘ ℎ𝑡−1 + 1 − 𝑧𝑡 ∘ ℎ𝑡

If reset gate unit is ~0, then this ignores previous memory and
only stores the new input
GRU intuition
• Units with long term dependencies have active update gates z
• Illustration:
GRU intuition
• If reset is close to 0, ignore previous hidden state
– Allows model to drop information that is irrelevant in the future

• Update gate z controls how much of past state should matter now.
– If z close to 1, then we can copy information in that unit through many time
steps! Less vanishing gradient!

• Units with short-term dependencies often have reset gates very


active
Other RNN Varients
Which of these variants is best?
• Do the differences matter?
– Greff et al. (2015), perform comparison of popular variants, finding that
they’re all about the same.
– Jozefowicz et al. (2015) tested more than ten thousand RNN architectures,
finding some that worked better than LSTMs on certain tasks.
LSTM Achievements
• LSTMs have essentially replaced n-grams as language models for
speech.
• Image captioning and other multi-modal tasks which were very
difficult with previous methods are now feasible.
• Many traditional NLP tasks work very well with LSTMs, but not
necessarily the top performers: e.g., POS tagging and NER: Choi 2016.
• Neural MT: broken away from plateau of SMT, especially for
grammaticality (partly because of characters/subwords), but not yet
industry strength.

[Ann Copestake, Overview of LSTMs and word2vec, 2016.]


https://arxiv.org/ftp/arxiv/papers/1611/1611.00068.pdf
Multi-layer RNN
Bidirectional RNN
• h is the memory, computed from the past memory and current word.
It summarizes the sentence up to that time.
Bidirectional RNN
• Problem: For classification you want to incorporate information from
words both preceding and following
Multilayer bidirectional RNN
• Each memory layer passes an intermediate sequential representation
to the next.
Image Captioning

• Explain Images with Multimodal Recurrent Neural Networks, Mao et al.


• Deep Visual-Semantic Alignments for Generating Image Descriptions, Karpathy and Fei-Fei
• Show and Tell: A Neural Image Caption Generator, Vinyals et al.
• Long-term Recurrent Convolutional Networks for Visual Recognition and Description, Donahue et al.
• Learning a Recurrent Visual Representation for Image Caption Generation, Chen and Zitnick
Recurrent Neural Network

Convolutional Neural Network


test image
test image
test image

X
test image

x0
<START
>

<START>
test image

y0

before:
h = tanh(Wxh * x + Whh * h)
h0

Wih now:
h = tanh(Wxh * x + Whh * h + Wih * v)

x0
<START
>

v
<START>
test image

y0

sample!
h0

x0
<START straw
>

<START>
test image

y0 y1

h0 h1

x0
<START straw
>

<START>
test image

y0 y1

sample!
h0 h1

x0
<START straw hat
>

<START>
test image

y0 y1 y2

h0 h1 h2

x0
<START straw hat
>

<START>
test image

y0 y1 y2

sample
<END> token
=> finish.
h0 h1 h2

x0
<START straw hat
>

<START>
Image Sentence Datasets
Microsoft COCO
[Tsung-Yi Lin et al. 2014]
mscoco.org

currently:
~120K images
~5 sentences each
Image Captioning: Example Results
Image Captioning: Failure Cases
RNN: Summary
• RNNs allow a lot of flexibility in architecture design
• Vanilla RNNs are simple but don’t work very well
• Backward flow of gradients in RNN can explode or vanish.
– Exploding is controlled with gradient clipping.
– Vanishing is controlled with additive interactions (LSTM)
• Common to use LSTM or GRU: their additive interactions improve
gradient flow
• Better/simpler architectures are a hot topic of current research
• Better understanding (both theoretical and empirical) is needed.

You might also like