INF5820: Language technological applications Gated RNNs (3:2) - - PowerPoint PPT Presentation

inf5820 language technological applications gated rnns 3 2
SMART_READER_LITE
LIVE PREVIEW

INF5820: Language technological applications Gated RNNs (3:2) - - PowerPoint PPT Presentation

INF5820: Language technological applications Gated RNNs (3:2) Taraka Rama University of Oslo 30 October 2018 Agenda Break only for 5 minutes Lecture ends at 3:45 GRU LSTM Connections An analysis of why GRUs address


slide-1
SLIDE 1

INF5820: Language technological applications Gated RNNs (3:2)

Taraka Rama

University of Oslo

30 October 2018

slide-2
SLIDE 2

Agenda

◮ Break only for 5 minutes ◮ Lecture ends at 3:45 ◮ GRU ◮ LSTM ◮ Connections ◮ An analysis of why GRUs address vanishing gradients ◮ Applications of Gated RNNs

2

slide-3
SLIDE 3

Some external resources

Many online resources on Gated RNNs:

◮ Fourth part on GRU and LSTM: https://tinyurl.com/z9j4ws9 ◮ Understanding Long-Short Term Memory Network:

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

3

slide-4
SLIDE 4

Recap

Equations of basic RNN

◮ ht = f(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

4

slide-5
SLIDE 5

Recap

Equations of basic RNN

◮ ht = f(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ xt is the input vector at time t ◮ ht is the hidden state or memory at time t ◮ f(·) is the non-linearity such as tanh or sigmoid function ◮ ˆ

yt is the output transformation that maps ht to the number of output classes at each time step

◮ pt is the output of softmax function that transforms the values in ˆ

yt to probabilities

4

slide-6
SLIDE 6

Recap: Vanishing/Exploding Gradient

◮ Fix k = 18 and S = 20 ◮ All W matrices and inputs are of size 1 × 1. (Simplication!!!)

5

slide-7
SLIDE 7

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt

5

slide-8
SLIDE 8

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18

5

slide-9
SLIDE 9

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18 ◮ ≈ A18 + B18Whhdhraw19

5

slide-10
SLIDE 10

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18 ◮ ≈ A18 + B18Whhdhraw19 ◮ ≈ A18 + B18Whh(A19 + B19dhnext19)

5

slide-11
SLIDE 11

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18 ◮ ≈ A18 + B18Whhdhraw19 ◮ ≈ A18 + B18Whh(A19 + B19dhnext19) ◮ ≈ A18 + B18Whh(A19 + B19Whhdhraw20)

5

slide-12
SLIDE 12

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18 ◮ ≈ A18 + B18Whhdhraw19 ◮ ≈ A18 + B18Whh(A19 + B19dhnext19) ◮ ≈ A18 + B18Whh(A19 + B19Whhdhraw20) ◮ The right most term: dhraw20 is always left multiplied by Whh

5

slide-13
SLIDE 13

Recap: Vanishing/Exploding Gradient

Basic recursion

◮ dhrawt = At + Btdhnextt ◮ dhnextt−1 = Whhdhrawt ◮ dhraw18 = A18 + B17dhnext18 ◮ ≈ A18 + B18Whhdhraw19 ◮ ≈ A18 + B18Whh(A19 + B19dhnext19) ◮ ≈ A18 + B18Whh(A19 + B19Whhdhraw20) ◮ The right most term: dhraw20 is always left multiplied by Whh ◮ When k = 1 then dWhh is computed using (Whh)19

5

slide-14
SLIDE 14

Problem with RNNs

◮ Vanishing/Exploding gradients ◮ Not sure if

◮ There is no dependency between different time steps ◮ Wrong initilization of parameters

◮ Many solutions (Pascanau 2012). ◮ Today: We will analyze why Gates address Vanishing gradient problem. ◮ Main solution is Gated RNNs

6

slide-15
SLIDE 15

Gated RNNs are better than RNNs at MT

Sutskever et al. (2014) Sequence to Sequence Learning with Neural Networks (4903 citations according to Google Scholar as of today)

7

slide-16
SLIDE 16

LSTM vs. GRU vs. CNN?

Yin et al. 2017: Comparative Study of CNN and RNN for Natural Language Processing https://arxiv.org/pdf/1702.01923.pdf

8

slide-17
SLIDE 17

Why the name gated?

◮ Analogous to gates in circuits

9

slide-18
SLIDE 18

Why the name gated?

◮ Analogous to gates in circuits ◮ Allow parts of the input vector or hidden state to pass over to the next

state

9

slide-19
SLIDE 19

Why the name gated?

◮ Analogous to gates in circuits ◮ Allow parts of the input vector or hidden state to pass over to the next

state

◮ x = g × y + (1 − g) × z where g ∈ {0, 1}

9

slide-20
SLIDE 20

Why the name gated?

◮ Analogous to gates in circuits ◮ Allow parts of the input vector or hidden state to pass over to the next

state

◮ x = g × y + (1 − g) × z where g ∈ {0, 1} ◮ In vector notation: xi = gi × yi + (1 − gi) × zi where gi ∈ {0, 1}

9

slide-21
SLIDE 21

Why the name gated?

◮ Analogous to gates in circuits ◮ Allow parts of the input vector or hidden state to pass over to the next

state

◮ x = g × y + (1 − g) × z where g ∈ {0, 1} ◮ In vector notation: xi = gi × yi + (1 − gi) × zi where gi ∈ {0, 1} ◮ A simpler way to write: Hadamard Product

9

slide-22
SLIDE 22

Hadamard product

◮ A cool name for elementwise matrix multiplication ◮ Can be represented as (in Machine Learning or Physics) or ◦ (usually

in NLP)

◮ C = A B where Ci,j = Ai,j × Bi,j

◮ Matrix dimensions do not change ◮ Can be performed only between identical dimension matrices 10

slide-23
SLIDE 23

0/1 problem

◮ 0/1 gate variable is not differentiable

11

slide-24
SLIDE 24

0/1 problem

◮ 0/1 gate variable is not differentiable ◮ Non-differentiable variable blocks back propagation

11

slide-25
SLIDE 25

0/1 problem

◮ 0/1 gate variable is not differentiable ◮ Non-differentiable variable blocks back propagation ◮ No back propagation implies no gradient descent

11

slide-26
SLIDE 26

0/1 problem

◮ 0/1 gate variable is not differentiable ◮ Non-differentiable variable blocks back propagation ◮ No back propagation implies no gradient descent ◮ What is the solution?

11

slide-27
SLIDE 27

0/1 problem

◮ 0/1 gate variable is not differentiable ◮ Non-differentiable variable blocks back propagation ◮ No back propagation implies no gradient descent ◮ What is the solution? ◮ sigmoid or tanh function

11

slide-28
SLIDE 28

Gated Recurrent Unit (GRU)

◮ GRU has two gates when compared to RNN

12

slide-29
SLIDE 29

Gated Recurrent Unit (GRU)

◮ GRU has two gates when compared to RNN ◮ Update gate z and Reset gate r

12

slide-30
SLIDE 30

Gated Recurrent Unit (GRU)

◮ GRU has two gates when compared to RNN ◮ Update gate z and Reset gate r ◮ Both gates are computed using different weights just like RNN at each

time step

12

slide-31
SLIDE 31

Gated Recurrent Unit (GRU)

◮ GRU has two gates when compared to RNN ◮ Update gate z and Reset gate r ◮ Both gates are computed using different weights just like RNN at each

time step

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1)

12

slide-32
SLIDE 32

Gated Recurrent Unit (GRU)

◮ GRU has two gates when compared to RNN ◮ Update gate z and Reset gate r ◮ Both gates are computed using different weights just like RNN at each

time step

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1)

12

slide-33
SLIDE 33

GRU computations

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1)

13

slide-34
SLIDE 34

GRU computations

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1)

13

slide-35
SLIDE 35

GRU computations

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

13

slide-36
SLIDE 36

GRU computations

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht

13

slide-37
SLIDE 37

GRU (Visualization)

(http://web.stanford.edu/class/cs224n/lectures/lecture9.pdf)

14

slide-38
SLIDE 38

Is it complex?

◮ Think of all weights, variables and input as 1 × 1 matrix.

15

slide-39
SLIDE 39

Is it complex?

◮ Think of all weights, variables and input as 1 × 1 matrix. ◮ Easy to analyze

15

slide-40
SLIDE 40

Is it complex?

◮ Think of all weights, variables and input as 1 × 1 matrix. ◮ Easy to analyze ◮ Boils down to single variable mathematics.

15

slide-41
SLIDE 41

Reset Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If rt = 0

◮ Ignore previous hidden memory ht−1. Equivalently, forget the past.

16

slide-42
SLIDE 42

Reset Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If rt = 0

◮ Ignore previous hidden memory ht−1. Equivalently, forget the past. ◮ Current hidden memory ht is partly dependent on current input xt

16

slide-43
SLIDE 43

Reset Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If rt = 1 Use everything in the previous hidden memory ht−1. Looks like RNN is back.

16

slide-44
SLIDE 44

Update Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If zt = 0

◮ Ignore previous hidden memory ht−1

17

slide-45
SLIDE 45

Update Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If zt = 0

◮ Ignore previous hidden memory ht−1 ◮ Current hidden memory ht is only dependent on the new memory state

˜ ht

17

slide-46
SLIDE 46

Update Gate and GRU

◮ Update gate: zt = σ(W (z)xt + U(z)ht−1) ◮ Reset gate: rt = σ(W (r)xt + U(r)ht−1) ◮ Internal memory content: ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ Final memory: ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht If zt = 1

◮ Copy the previous hidden memory ht−1 ◮ Current input is ignored completely

17

slide-47
SLIDE 47

What is it for NLP?

◮ Short term dependencies means older history should be ignored (rt → 0)

18

slide-48
SLIDE 48

What is it for NLP?

◮ Short term dependencies means older history should be ignored (rt → 0) ◮ Long term dependencies means older history should be retained as

much as possible (zt → 1)

18

slide-49
SLIDE 49

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

19

slide-50
SLIDE 50

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

19

slide-51
SLIDE 51

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

19

slide-52
SLIDE 52

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

19

slide-53
SLIDE 53

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

◮ ht = ˜

ht

19

slide-54
SLIDE 54

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

◮ ht = ˜

ht

◮ W is Wxh and U is Whh 19

slide-55
SLIDE 55

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

◮ ht = ˜

ht

◮ W is Wxh and U is Whh

◮ Back to Vanilla RNN... Is it a problem?

19

slide-56
SLIDE 56

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

◮ ht = ˜

ht

◮ W is Wxh and U is Whh

◮ Back to Vanilla RNN... Is it a problem? ◮ rt = 1 and zt = 0 should happen at all the time steps. Very low chance

  • f happening!!

19

slide-57
SLIDE 57

Is RNN a special case of GRU?

Can you think when RNN is a special case of GRU?

◮ When rt = 1 and zt = 0:

◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = 0 ∗ ht−1 + 1 ∗ ˜

ht

◮ ht = ˜

ht

◮ W is Wxh and U is Whh

◮ Back to Vanilla RNN... Is it a problem? ◮ rt = 1 and zt = 0 should happen at all the time steps. Very low chance

  • f happening!!

◮ So we are fine...

19

slide-58
SLIDE 58

Does GRU fixes vanishing gradient?

Two ways to analyze this:

◮ What does the hidden state look like in GRU after few time steps?

20

slide-59
SLIDE 59

Does GRU fixes vanishing gradient?

Two ways to analyze this:

◮ What does the hidden state look like in GRU after few time steps? ◮ How does gradient calculation look like?

20

slide-60
SLIDE 60

Does GRU fixes vanishing gradient?

Two ways to analyze this:

◮ What does the hidden state look like in GRU after few time steps? ◮ How does gradient calculation look like?

◮ Requires multivariate chain rule!!! 20

slide-61
SLIDE 61

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

21

slide-62
SLIDE 62

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

21

slide-63
SLIDE 63

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

◮ Therefore, h20 = z20z19h18 + z20(1 − z19)˜

h19 + z20(1 − z20)˜ h20

21

slide-64
SLIDE 64

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

◮ Therefore, h20 = z20z19h18 + z20(1 − z19)˜

h19 + z20(1 − z20)˜ h20

◮ If z19 → 0 then only part of h18 is relevant through ˜

h19

21

slide-65
SLIDE 65

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

◮ Therefore, h20 = z20z19h18 + z20(1 − z19)˜

h19 + z20(1 − z20)˜ h20

◮ If z19 → 0 then only part of h18 is relevant through ˜

h19

◮ If z19 → 1 then ˜

h19 is ignored completely. It is as if the input x19 is completely ignored.

21

slide-66
SLIDE 66

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

◮ Therefore, h20 = z20z19h18 + z20(1 − z19)˜

h19 + z20(1 − z20)˜ h20

◮ If z19 → 0 then only part of h18 is relevant through ˜

h19

◮ If z19 → 1 then ˜

h19 is ignored completely. It is as if the input x19 is completely ignored.

◮ There is a jump between timesteps 18 and 20.

21

slide-67
SLIDE 67

GRU: Hidden state calculation

We will expand ht from t = 20 to t = 19. Again, all our matrices are 1 × 1 to simplify.

◮ h20 = z20h19 + (1 − z20)˜

h20

◮ h19 = z19h18 + (1 − z19)˜

h19

◮ Therefore, h20 = z20z19h18 + z20(1 − z19)˜

h19 + z20(1 − z20)˜ h20

◮ If z19 → 0 then only part of h18 is relevant through ˜

h19

◮ If z19 → 1 then ˜

h19 is ignored completely. It is as if the input x19 is completely ignored.

◮ There is a jump between timesteps 18 and 20. ◮ It is a shortcut across time.

21

slide-68
SLIDE 68

Single variable chain rule

Question If z is dependent on y and y is dependent on x what is dz

dx?

22

slide-69
SLIDE 69

Single variable chain rule

Question If z is dependent on y and y is dependent on x what is dz

dx?

Answer

dz dx = dz dy dy dx

22

slide-70
SLIDE 70

Single variable chain rule

Question If z is dependent on y and y is dependent on x what is dz

dx?

Answer

dz dx = dz dy dy dx

Example

◮ z = exp(y) ◮ y = x2 ◮ dz dy = exp(y) ◮ dy dx = 2x ◮ dz dx = exp(y) × 2x

22

slide-71
SLIDE 71

Multivariate chain rule (Very Important!)

23

slide-72
SLIDE 72

Multivariate chain rule example

◮ z = x + y

24

slide-73
SLIDE 73

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t)

24

slide-74
SLIDE 74

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt

24

slide-75
SLIDE 75

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt ◮ ∂z ∂y = 1

24

slide-76
SLIDE 76

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt ◮ ∂z ∂y = 1 ◮ ∂z ∂x = 1

24

slide-77
SLIDE 77

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt ◮ ∂z ∂y = 1 ◮ ∂z ∂x = 1 ◮ dy dt = exp(t)

24

slide-78
SLIDE 78

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt ◮ ∂z ∂y = 1 ◮ ∂z ∂x = 1 ◮ dy dt = exp(t) ◮ dx dt = 2t

24

slide-79
SLIDE 79

Multivariate chain rule example

◮ z = x + y ◮ x = t2, y = exp(t) ◮ dz dt = ∂z ∂y dy dt + ∂z ∂x dx dt ◮ ∂z ∂y = 1 ◮ ∂z ∂x = 1 ◮ dy dt = exp(t) ◮ dx dt = 2t ◮ Finally, dz dt = exp(t) + 2t

24

slide-80
SLIDE 80

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh )

25

slide-81
SLIDE 81

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives

25

slide-82
SLIDE 82

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives ◮ A arrow between two nodes is derivative between the two nodes.

25

slide-83
SLIDE 83

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives ◮ A arrow between two nodes is derivative between the two nodes.

  • 1. Whh → h3 directly

25

slide-84
SLIDE 84

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives ◮ A arrow between two nodes is derivative between the two nodes.

  • 1. Whh → h3 directly
  • 2. Whh → h2 → h3

25

slide-85
SLIDE 85

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives ◮ A arrow between two nodes is derivative between the two nodes.

  • 1. Whh → h3 directly
  • 2. Whh → h2 → h3
  • 3. Whh → h1 → h2 → h3

25

slide-86
SLIDE 86

Multivariate chain rule in summary

What is the derivative of h3 with respect to Whh? ( ∂h3

∂Whh ) ◮ Enumerate all paths from Whh to h3 and sum the derivatives ◮ A arrow between two nodes is derivative between the two nodes.

  • 1. Whh → h3 directly
  • 2. Whh → h2 → h3
  • 3. Whh → h1 → h2 → h3

◮ h2 → h3 means ∂h3 ∂h2

25

slide-87
SLIDE 87

What does it mean?

What is the derivative of h3 with respect to Whh?

26

slide-88
SLIDE 88

What does it mean?

What is the derivative of h3 with respect to Whh?

◮ ∂h3 ∂Whh is sum of the following

26

slide-89
SLIDE 89

What does it mean?

What is the derivative of h3 with respect to Whh?

◮ ∂h3 ∂Whh is sum of the following

1.

∂h3 ∂Whh

26

slide-90
SLIDE 90

What does it mean?

What is the derivative of h3 with respect to Whh?

◮ ∂h3 ∂Whh is sum of the following

1.

∂h3 ∂Whh

2.

∂h3 ∂h2 ∂h2 ∂Whh

26

slide-91
SLIDE 91

What does it mean?

What is the derivative of h3 with respect to Whh?

◮ ∂h3 ∂Whh is sum of the following

1.

∂h3 ∂Whh

2.

∂h3 ∂h2 ∂h2 ∂Whh

3.

∂h3 ∂h2 ∂h2 ∂h1 ∂h1 ∂Whh

26

slide-92
SLIDE 92

RNN expansion

Equations of basic RNN

◮ ht = tanh(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ ∂L ∂Whh = T t=1 ∂Lt ∂Whh (Sum the loss over each time step t)

27

slide-93
SLIDE 93

RNN expansion

Equations of basic RNN

◮ ht = tanh(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ ∂L ∂Whh = T t=1 ∂Lt ∂Whh (Sum the loss over each time step t) ◮ ∂Lt ∂Whh = ∂Lt ∂ˆ yt ∂ˆ yt ∂ht

t

k=1 ∂ht ∂hk ∂hk ∂Whh (Each ht is computed using Whh.

Apply multivariate chain rule.)

27

slide-94
SLIDE 94

RNN expansion

Equations of basic RNN

◮ ht = tanh(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ ∂L ∂Whh = T t=1 ∂Lt ∂Whh (Sum the loss over each time step t) ◮ ∂Lt ∂Whh = ∂Lt ∂ˆ yt ∂ˆ yt ∂ht

t

k=1 ∂ht ∂hk ∂hk ∂Whh (Each ht is computed using Whh.

Apply multivariate chain rule.)

◮ ∂ht ∂hk = t j=k+1 ∂hj ∂hj−1 (Each hj is immediately dependent on hj−1.

Apply single variable chain rule.)

27

slide-95
SLIDE 95

RNN expansion

Equations of basic RNN

◮ ht = tanh(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ ∂L ∂Whh = T t=1 ∂Lt ∂Whh (Sum the loss over each time step t) ◮ ∂Lt ∂Whh = ∂Lt ∂ˆ yt ∂ˆ yt ∂ht

t

k=1 ∂ht ∂hk ∂hk ∂Whh (Each ht is computed using Whh.

Apply multivariate chain rule.)

◮ ∂ht ∂hk = t j=k+1 ∂hj ∂hj−1 (Each hj is immediately dependent on hj−1.

Apply single variable chain rule.)

◮ ∂hj ∂hj−1 = W(1 − (hj)2) (Derivative of tanh(x) function is

1 − (tanh(x))2)

27

slide-96
SLIDE 96

RNN expansion

Equations of basic RNN

◮ ht = tanh(Wxhxt + Whhht−1 + bh) ◮ ˆ

yt = Whyht + by

◮ pt = softmax( ˆ

yt)

◮ ∂L ∂Whh = T t=1 ∂Lt ∂Whh (Sum the loss over each time step t) ◮ ∂Lt ∂Whh = ∂Lt ∂ˆ yt ∂ˆ yt ∂ht

t

k=1 ∂ht ∂hk ∂hk ∂Whh (Each ht is computed using Whh.

Apply multivariate chain rule.)

◮ ∂ht ∂hk = t j=k+1 ∂hj ∂hj−1 (Each hj is immediately dependent on hj−1.

Apply single variable chain rule.)

◮ ∂hj ∂hj−1 = W(1 − (hj)2) (Derivative of tanh(x) function is

1 − (tanh(x))2)

◮ Repeated multiplication of Whh when t >> k causes vanishing gradient

  • r exploding gradient

27

slide-97
SLIDE 97

GRU expansion

How does

∂hj ∂hj−1 look in the case of GRU? ◮ Is not so simple as in the case of RNN. Why?

28

slide-98
SLIDE 98

GRU expansion

How does

∂hj ∂hj−1 look in the case of GRU? ◮ Is not so simple as in the case of RNN. Why? ◮ GRU has two gates that play a role in the computation of hidden state

ht

28

slide-99
SLIDE 99

GRU expansion

How does

∂hj ∂hj−1 look in the case of GRU? ◮ Is not so simple as in the case of RNN. Why? ◮ GRU has two gates that play a role in the computation of hidden state

ht

◮ How does it look?

28

slide-100
SLIDE 100

GRU expansion

How does

∂hj ∂hj−1 look in the case of GRU? ◮ Is not so simple as in the case of RNN. Why? ◮ GRU has two gates that play a role in the computation of hidden state

ht

◮ How does it look? ◮ [z((1 − z)W z(1 − h) + 1)] + [r(1 − z)(1 − h2)U(1 + ht−1(1 − r)W r)]

28

slide-101
SLIDE 101

Analysis of the partial derivative

[z((1 − z)W z(1 − h) + 1)] + [r(1 − z)(1 − h2)U(1 + ht−1(1 − r)W r)]

◮ When z → 0: First term vanishes. Only dependent on reset gate.

29

slide-102
SLIDE 102

Analysis of the partial derivative

[z((1 − z)W z(1 − h) + 1)] + [r(1 − z)(1 − h2)U(1 + ht−1(1 − r)W r)]

◮ When z → 0: First term vanishes. Only dependent on reset gate. ◮ When z → 1: 1. Repeated multiplication by 1 does not effect the

  • gradient. (Good for long-term dependencies)

29

slide-103
SLIDE 103

Analysis of the partial derivative

[z((1 − z)W z(1 − h) + 1)] + [r(1 − z)(1 − h2)U(1 + ht−1(1 − r)W r)]

◮ When z → 0: First term vanishes. Only dependent on reset gate. ◮ When z → 1: 1. Repeated multiplication by 1 does not effect the

  • gradient. (Good for long-term dependencies)

◮ When r → 0: Second term vanishes. (Good for short term

dependencies)

29

slide-104
SLIDE 104

Analysis of the partial derivative

[z((1 − z)W z(1 − h) + 1)] + [r(1 − z)(1 − h2)U(1 + ht−1(1 − r)W r)]

◮ When z → 0: First term vanishes. Only dependent on reset gate. ◮ When z → 1: 1. Repeated multiplication by 1 does not effect the

  • gradient. (Good for long-term dependencies)

◮ When r → 0: Second term vanishes. (Good for short term

dependencies)

◮ No guarantee that the gradient is always going to be multiplied by a

weight matrix.

29

slide-105
SLIDE 105

Conclusion

When r = 1 and z = 0, we get back the RNN derivative leading to gradient problems. What is the chance for that to happen?

30

slide-106
SLIDE 106

Conclusion

When r = 1 and z = 0, we get back the RNN derivative leading to gradient problems. What is the chance for that to happen? No need to worry. It has to happen at each timestep. Very low chance.

30

slide-107
SLIDE 107

Conclusion

When r = 1 and z = 0, we get back the RNN derivative leading to gradient problems. What is the chance for that to happen? No need to worry. It has to happen at each timestep. Very low chance. Clearly GRU addresses vanishing gradient problem

30

slide-108
SLIDE 108

Long-Short Term Memory Network

◮ Is more complex than GRU. Has one extra gate and one extra internal

state.

◮ Input gate: it = σ(W (i)xt + U(i)ht−1) ◮ Forget gate: ft = σ(W (f)xt + U(f)ht−1) ◮ Output gate: ot = σ(W (o)xt + U(o)ht−1) ◮ Candidate gate: gt = σ(W (g)xt + U(g)ht−1) ◮ Internal memory content: ct = ft ◦ ct−1 + it ◦ gt ◮ Final hidden state: ht = ot ◦ tanh(ct)

31

slide-109
SLIDE 109

Visualizations

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

32

slide-110
SLIDE 110

Visualizations

http://deeplearning.net/tutorial/lstm.html

32

slide-111
SLIDE 111

Visualizations

Chung et al. 2014: https://arxiv.org/abs/1412.3555

32

slide-112
SLIDE 112

Visualizations

Chung et al. 2014: https://arxiv.org/abs/1412.3555

32

slide-113
SLIDE 113

Visualizations

Visualization by Tim Rocktäschel

32

slide-114
SLIDE 114

Comparison between LSTM and GRU

LSTM

◮ it = σ(W (i)xt + U(i)ht−1) ◮ ft = σ(W (f)xt + U(f)ht−1) ◮ ot = σ(W (o)xt + U(o)ht−1) ◮ gt = σ(W (g)xt + U(g)ht−1) ◮ ct = ft ◦ ct−1 + it ◦ gt ◮ ht = ot ◦ tanh(ct)

GRU

◮ zt = σ(W (z)xt + U(z)ht−1) ◮ rt = σ(W (r)xt + U(r)ht−1) ◮ ˜

ht = tanh(Wxt + rt ◦ Uht−1)

◮ ht = zt ◦ ht−1 + (1 − zt) ◦ ˜

ht Differences

◮ Forget gate is like Update gate ◮ Input gate is like Reset gate. ◮ Input gate does not act

directly on the previous hidden state.

◮ Lesser number of parameters.

33

slide-115
SLIDE 115

Is RNN is a special case of LSTM?

◮ Not very straightforward.

34

slide-116
SLIDE 116

Is RNN is a special case of LSTM?

◮ Not very straightforward. ◮ Input gate is all 1

34

slide-117
SLIDE 117

Is RNN is a special case of LSTM?

◮ Not very straightforward. ◮ Input gate is all 1 ◮ Forget gate is all 0

34

slide-118
SLIDE 118

Is RNN is a special case of LSTM?

◮ Not very straightforward. ◮ Input gate is all 1 ◮ Forget gate is all 0 ◮ Output gate is all 1

34

slide-119
SLIDE 119

Is RNN is a special case of LSTM?

◮ Not very straightforward. ◮ Input gate is all 1 ◮ Forget gate is all 0 ◮ Output gate is all 1 ◮ Remove the final tanh covering the internal memory

34