1 of 29

Deep Learning (DEEP-0001)�

RNNs

2 of 29

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

3 of 29

Recurrent Neural Networks

  • Recurrent Neural Networks (RNNs) are a family of neural networks for processing sequential data.
  • Input consists in a sequence of values (X) and can process sequences of variable length.
  • RNNs have internal memory (h), which allows capturing and processing sequential information.

​

4 of 29

Recurrent Neural Network

RNN

Simple/Standard

​

MLP

input

​

output

​

5 of 29

Recurrent Neural Network Structure

  • Left: typical RNN structure.
  • Right: the unfolding version where the previous information is transformed to the later time step.

6 of 29

Recurrent Neural Network Structure

7 of 29

RNN Models

RNN

MLP

8 of 29

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

9 of 29

NLP Workflow

10 of 29

Time Series

11 of 29

Recurrent Neural Network

12 of 29

RNN Forward Pass

Wxh (hidden_size, vocab_size)

Whh (hidden_size, hidden_size)

Why (vocab_size, hidden_size)

13 of 29

Backpropagation Through Time

14 of 29

RNN – Challenges

  • Exploding gradients:
    • Gradient Clipping in truncated BPTT.

​

  • Vanishing Gradient problem (long-term dependency):
    • LSTM.

​

​

15 of 29

Long Short-Term Memory (LSTM)

  • LSTM is a type o RNN.
  • Designed to avoid the long-term dependency problem.
  • Gating Mechanism allows writing, reading, and erasing information from memory.

“I grew up in France… I speak fluent French.”

16 of 29

Long Short-Term Memory (LSTM)

​

17 of 29

Cell State (Long-Term)

​

  • Gates: remove or add information to the cell state.
  • An LSTM has three of these gates, to protect and control the cell state.

18 of 29

Forget gate layer

​

  • Example: predict the next word based on all the previous ones.
    • The cell state might include the gender of the present subject, so that the correct pronouns can be used.
    • When we see a new subject, we want to forget the gender of the old subject.

Maria moved to Tokyo last year, but her brother John stayed in Lisbon because he loved the city.

19 of 29

Input Gate Layer

​

  • Information to store in the cell state:
    • Sigmoid layer decides which values will be updated.
    • Tanh layer creates a vector of new candidate values.
  • In the example of our language model, we’d want to add the gender of the new subject to the cell state, to replace the old one we’re forgetting.

20 of 29

Input Gate Layer

​

  • It’s now time to update the old cell state, into the new cell state.
  • In the case of the language model, this is where we’d actually drop the information about the old subject’s gender and add the new information.

21 of 29

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.

22 of 29

Output Gate (Short-Term)

​

  • First, we run a sigmoid layer which decides what parts of the cell state we’re going to output.
  • Then, we put the cell state through tanh and multiply it by the output of the sigmoid gate, so that we only output the parts we decided to.
  • For the language model example, it might output whether the subject is singular or plural, so that we know what form a verb should be conjugated into if that’s what follows next.

23 of 29

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.

24 of 29

Dimensionality

​

25 of 29

Variations - Bidirectional RNN�

  • Two independent RNN together.
  • Allows the network to have both backward and forward information about the sequence at every time step.

26 of 29

Variations - Deep/Stacked RNN�

27 of 29

Convolutional RNN�

28 of 29

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.

29 of 29

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