Recurrent Neural Networks: Stability analysis and LSTMs M. - - PowerPoint PPT Presentation

recurrent neural networks stability analysis and lstms
SMART_READER_LITE
LIVE PREVIEW

Recurrent Neural Networks: Stability analysis and LSTMs M. - - PowerPoint PPT Presentation

Recurrent Neural Networks: Stability analysis and LSTMs M. Soleymani Sharif University of Technology Spring 2020 Most slides have been adopted from Bhiksha Raj, 11-785, CMU 2019 and some from Fei Fei Li and colleagues lectures, cs231n,


slide-1
SLIDE 1

Recurrent Neural Networks: Stability analysis and LSTMs

  • M. Soleymani

Sharif University of Technology Spring 2020 Most slides have been adopted from Bhiksha Raj, 11-785, CMU 2019 and some from Fei Fei Li and colleagues lectures, cs231n, Stanford 2017.

slide-2
SLIDE 2

St Story so far

  • Iterated structures are good for analyzing time series data with short-

time dependence on the past

– These are “Time delay Neural Nets” (TDNNs), AKA convnets

Stock vector X(t) X(t+1) X(t+2) X(t+3) X(t+4) X(t+5) X(t+6) X(t+7) Y(t+6)

2

slide-3
SLIDE 3

St Story so far

  • Recurrent structures are good for analyzing time series data with

long-term dependence on the past

– These are recurrent neural networks

Time X(t) Y(t) t=0 h-1

3

slide-4
SLIDE 4

Re Recurrent structures can do what static structures cannot

  • The addition problem: Add two N-bit numbers to produce a N+1-bit

number

– Input is binary – Will require large number of training instances

  • Output must be specified for every pair of inputs
  • Weights that generalize will make errors

– Network trained for N-bit numbers will not work for N+1 bit numbers

1 0 0 0 1 1 0 0 1 0 1 1 0 0 1 0 1 1 0 0 MLP 1 0 1 0 1 0 1 1 1 1 0

4

slide-5
SLIDE 5
  • The addition problem: Add two N-bit numbers to produce a N+1-

bit number

  • RNN solution: Very simple, can add two numbers of any size

1 1 RNN unit Previous carry Carry

  • ut

5

MLP MLPs vs RNNs

slide-6
SLIDE 6

MLP: Th The parity problem

  • Is the number of “ones” even or odd
  • Network must be complex to capture all patterns

– XOR network, quite complex – Fixed input size

1 0 0 0 1 1 0 0 1 0 MLP 1

6

slide-7
SLIDE 7

RNN: Th The parity problem

  • Trivial solution
  • Generalizes to input of any size

1 1 RNN unit Previous

  • utput

7

slide-8
SLIDE 8
  • Recurrent structures can be trained by minimizing the loss between

the sequence of outputs and the sequence of desired outputs

– Through gradient descent and backpropagation

Time X(t) Y(t) t=0 h-1 Loss Ydesired(t)

8

St Story so far

slide-9
SLIDE 9

h0

𝑌(1) 𝑌(2) 𝑌(𝑈 − 2) 𝑌(𝑈 − 1) 𝑌(𝑈) 𝑍(1) 𝑍(2) 𝑍(𝑈 − 2) 𝑍(𝑈 − 1) 𝑍(𝑈) 𝑍)*+,-)(1. . 𝑈) 𝑀𝑝𝑡𝑡

  • The loss computed is between the sequence of outputs

by the network and the desired sequence of outputs

  • This is not just the sum of the divergences at individual times

§ Unless we explicitly define it that way

9

Back Propagation Th Through Ti Time

slide-10
SLIDE 10
  • Usual assumption: Sequence divergence is the sum of the

divergence at individual instants

𝑀𝑝𝑡𝑡 𝑍

)*+,-) 1 … 𝑈 , 𝑍(1 … 𝑈) = 5 𝑀𝑝𝑡𝑡 𝑍 )*+,-) 𝑢 , 𝑍(𝑢)

  • )

𝛼9())𝑀𝑝𝑡𝑡 𝑍

)*+,-) 1 … 𝑈 , 𝑍(1 … 𝑈) = 𝛼9())𝑀𝑝𝑡𝑡 𝑍 )*+,-) 𝑢 , 𝑍(𝑢)

Time X(t) Y(t) t=1 h0 Y(t) Loss

Ytarget(t)

10

Ti Time-sy synchronous recurrence

slide-11
SLIDE 11
  • In linear systems, long-term behavior depends entirely on the

eigenvalues of the hidden-layer weights matrix

– If the largest Eigen value is greater than 1, the system will “blow up” – If it is lesser than 1, the response will “vanish” very quickly

11

Long-term behavior of RNNs

slide-12
SLIDE 12

“B “BIB IBO” ” Stabi bility ty

  • “Bounded Input Bounded Output” stability

– This is a highly desirable characteristic

12

slide-13
SLIDE 13
  • Time-delay structures have bounded output if

– The function 𝑔() has bounded output for bounded input

  • Which is true of almost every activation function

– 𝑌(𝑢) is bounded

X(t+1) X(t+2) X(t+3) X(t+4) X(t+5) X(t+6) X(t+7) Y(t+5)

13

“B “BIB IBO” ” Stabi bility ty

  • Returning to an old model..

𝑍 𝑢 = 𝑔(𝑌 𝑢 − 𝑗 , 𝑗 = 1. . 𝐿)

  • When will the output “blow up”?
slide-14
SLIDE 14
  • Will RNN necessarily be BIBO?

Time X(t) Y(t) t=0 h-1

14

Is Is this this BIB IBO?

slide-15
SLIDE 15
  • Will this necessarily be BIBO?

– Guaranteed if output and hidden activations are bounded

  • But will it saturate (and where)

– What if the activations are linear?

Time X(t) Y(t) t=0 h-1

15

Is Is this this BIB IBO?

slide-16
SLIDE 16
  • Sufficient to analyze the behavior of the hidden layer ℎ? since

it carries the relevant information

– Will assume only a single hidden layer for simplicity

Time X(t) Y(t) t=0 h-1

16

Ana Analyzing ng recur urrenc nce

slide-17
SLIDE 17

Ana Analyzing ng Recur ursi sion

17

slide-18
SLIDE 18
  • Easier to analyze linear systems

– Will attempt to extrapolate to non-linear systems subsequently

  • All activations are identity functions

– 𝑨? = 𝑋

Bℎ?CD + 𝑋 F𝑦?, ℎ?= 𝑨?

Time X(t) Y(t) t=0 h-1

18

St Streetlight effect

slide-19
SLIDE 19

Li Linear r systems ms

  • ℎ? = 𝑋

Bℎ?CD + 𝑋 F𝑦?

– ℎ?CD = 𝑋

Bℎ?CH + 𝑋 F𝑦?CD

  • ℎ? = 𝑋

B Hℎ?CH + 𝑋 B𝑋 F𝑦?CD + 𝑋 F𝑦?

19

slide-20
SLIDE 20

Li Linear r systems ms

  • ℎ? = 𝑋

Bℎ?CD + 𝑋 F𝑦?

– ℎ?CD = 𝑋

Bℎ?CH + 𝑋 F𝑦?CD

  • ℎ? = 𝑋

B Hℎ?CH + 𝑋 B𝑋 F𝑦?CD + 𝑋 F𝑦?

  • ℎ? = 𝑋

B ?IDℎCD + 𝑋 B ?𝑋 F𝑦J + 𝑋 B ?CD𝑋 F𝑦D + ⋯

20

slide-21
SLIDE 21

Li Linear r systems ms

  • ℎ? = 𝑋

Bℎ?CD + 𝑋 F𝑦?

– ℎ?CD = 𝑋

Bℎ?CH + 𝑋 F𝑦?CD

  • ℎ? = 𝑋

B Hℎ?CH + 𝑋 B𝑋 F𝑦?CD + 𝑋 F𝑦?

  • ℎ? = 𝑋

B ?IDℎCD + 𝑋 B ?𝑋 F𝑦J + 𝑋 B ?CD𝑋 F𝑦D + ⋯

  • ℎ? = 𝐼? ℎCD + 𝐼? 𝑦J + 𝐼? 𝑦D + 𝐼? 𝑦H + ⋯

= ℎCD𝐼?(1CD) + 𝑦J𝐼?(1J) + 𝑦D𝐼?(1D) + 𝑦H𝐼?(1H) + ⋯

  • Where 𝐼?(1)) is the hidden response at time k when the input is

[0 0 0 … 1 0 . . 0] (where the 1 occurs in the t-th position)

21

slide-22
SLIDE 22

St Streetlight effect

  • Sufficient to analyze the response to a single input at 𝑢 = 0

– Principle of superposition in linear systems:

ℎ? = ℎCD𝐼?(1CD) + 𝑦J𝐼?(1J) + 𝑦D𝐼?(1D) + 𝑦H𝐼?(1H) + ⋯

Time X(t) Y(t) t=0 h-1

22

slide-23
SLIDE 23
  • Consider simple, scalar, linear recursion (note change of notation)

– ℎ 𝑢 = 𝑥Bℎ 𝑢 − 1 + 𝑥F𝑦(𝑢) – ℎ D 𝑢 = 𝑥B

)𝑥F𝑦 1

  • Response to a single input at 1

23

Li Linear r recursions

ℎ D 𝑙

slide-24
SLIDE 24
  • Vector linear recursion (note change of notation)

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Length of response vector to a single input at 1 is |ℎ{D} 𝑢 |
  • We can write 𝑋

B = 𝑉Λ𝑉CD

– 𝑋

B𝑣X = 𝜇X𝑣X

– For any vector 𝑤 we can write

  • 𝑤 = 𝑏D𝑣D + 𝑏H𝑣H + ⋯ + 𝑏\𝑣\
  • 𝑋

B𝑤 = 𝑏D𝜇D𝑣D + 𝑏H𝜇H𝑣H + ⋯ + 𝑏\𝜇\𝑣\

  • 𝑋

B )𝑤 = 𝑏D𝜇D )𝑣D + 𝑏H𝜇H ) 𝑣H + ⋯ + 𝑏\𝜇\ ) 𝑣\

– lim

)→a 𝑋 B )𝑤 = 𝑏b𝜇b ) 𝑣b where 𝑛 = argmax h

𝜇h

24

Li Linear r recursions: : Vector r version

slide-25
SLIDE 25
  • Vector linear recursion (note change of notation)

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Length of response vector to a single input at 1 is |ℎ{D} 𝑢 |
  • We can write 𝑋

B = 𝑉Λ𝑉CD

– 𝑋

B𝑣X = 𝜇X𝑣X

– For any vector 𝑤 we can write

  • 𝑤 = 𝑏D𝑣D + 𝑏H𝑣H + ⋯ + 𝑏\𝑣\
  • 𝑋

B𝑤 = 𝑏D𝜇D𝑣D + 𝑏H𝜇H𝑣H + ⋯ + 𝑏\𝜇\𝑣\

  • 𝑋

B )𝑤 = 𝑏D𝜇D )𝑣D + 𝑏H𝜇H ) 𝑣H + ⋯ + 𝑏\𝜇\ ) 𝑣\

– lim

)→a 𝑋 B )𝑤 = 𝑏b𝜇b ) 𝑣b where 𝑛 = argmax h

𝜇h For any input, for large 𝑢 the length of the hidden vector will expand or contract according to the 𝑢 th power of the largest eigen value of the hidden-layer weight matrix

25

Li Linear r recursions: : Vector r version

slide-26
SLIDE 26
  • Vector linear recursion (note change of notation)

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Length of response vector to a single input at 1 is |ℎ{D} 𝑢 |
  • We can write 𝑋

B = 𝑉Λ𝑉CD

– 𝑋

B𝑣X = 𝜇X𝑣X

– For any vector 𝑤 we can write

  • 𝑤 = 𝑏D𝑣D + 𝑏H𝑣H + ⋯ + 𝑏\𝑣\
  • 𝑋

B𝑤 = 𝑏D𝜇D𝑣D + 𝑏H𝜇H𝑣H + ⋯ + 𝑏\𝜇\𝑣\

  • 𝑋

B )𝑤 = 𝑏D𝜇D )𝑣D + 𝑏H𝜇H ) 𝑣H + ⋯ + 𝑏\𝜇\ ) 𝑣\

– lim

)→a 𝑋 B )𝑤 = 𝑏b𝜇b ) 𝑣b where 𝑛 = argmax h

𝜇h

For any input, for large 𝑢, the length of the hidden vector will expand or contract according to the 𝑢 th power of the largest eigen value of the hidden-layer weight matrix

Unless it has no component along the eigen vector corresponding to the largest eigen value. In that case it will grow according to the second largest Eigen value.. And so on..

26

Li Linear r recursions: : Vector r version

slide-27
SLIDE 27
  • Vector linear recursion (note change of notation)

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Length of response vector to a single input at 1 is |ℎ{D} 𝑢 |
  • We can write 𝑋

B = 𝑉Λ𝑉CD

– 𝑋

B𝑣X = 𝜇X𝑣X

– For any vector 𝑤 we can write

  • 𝑤 = 𝑏D𝑣D + 𝑏H𝑣H + ⋯ + 𝑏\𝑣\
  • 𝑋

B𝑤 = 𝑏D𝜇D𝑣D + 𝑏H𝜇H𝑣H + ⋯ + 𝑏\𝜇\𝑣\

  • 𝑋

B )𝑤 = 𝑏D𝜇D )𝑣D + 𝑏H𝜇H ) 𝑣H + ⋯ + 𝑏\𝜇\ ) 𝑣\

– lim

)→a 𝑋 B )𝑤 = 𝑏b𝜇b ) 𝑣b where 𝑛 = argmax h

𝜇h

If 𝜇b*F > 1 it will blow up, otherwise it will contract and shrink to 0 rapidly

27

Li Linear r recursions: : Vector r version

For any input, for large 𝑢 the length of the hidden vector will expand or contract according to the 𝑢 th power of the largest eigen value of the hidden-layer weight matrix

Unless it has no component along the eigen vector corresponding to the largest eigen value. In that case it will grow according to the second largest Eigen value.. And so on..

slide-28
SLIDE 28
  • Vector linear recursion (note change of notation)

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Length of response vector to a single input at 1 is |ℎ{D} 𝑢 |
  • We can write 𝑋

B = 𝑉Λ𝑉CD

– 𝑋

B𝑣X = 𝜇X𝑣X

– For any vector 𝑤 we can write

  • 𝑤 = 𝑏D𝑣D + 𝑏H𝑣H + ⋯ + 𝑏\𝑣\
  • 𝑋

B𝑤 = 𝑏D𝜇D𝑣D + 𝑏H𝜇H𝑣H + ⋯ + 𝑏\𝜇\𝑣\

  • 𝑋

B )𝑤 = 𝑏D𝜇D )𝑣D + 𝑏H𝜇H ) 𝑣H + ⋯ + 𝑏\𝜇\ ) 𝑣\

– lim

)→a 𝑋 B )𝑤 = 𝑏b𝜇b ) 𝑣b where 𝑛 = argmax h

𝜇h

If 𝜇b*F > 1 it will blow up, otherwise it will contract and shrink to 0 rapidly What about at middling values of 𝑢? It will depend on the

  • ther eigen values

28

Linear recursions: Vector version

For any input, for large 𝑢 the length of the hidden vector will expand or contract according to the 𝑢 th power of the largest eigen value of the hidden-layer weight matrix

Unless it has no component along the eigen vector corresponding to the largest eigen value. In that case it will grow according to the second largest Eigen value.. And so on..

slide-29
SLIDE 29
  • Vector linear recursion

– ℎ 𝑢 = 𝑋

Bℎ 𝑢 − 1 + 𝑋 F𝑦(𝑢)

– ℎ D 𝑢 = 𝑋

B )𝑋 F𝑦 1

  • Response to a single input [1 1 1 1] at 1

𝜇b*F = 0.9 𝜇b*F = 1 𝜇b*F = 1.1 𝜇b*F = 1 𝜇b*F = 1.1 Complex Eigenvalues 𝜇H\k = 0.5 𝜇H\k = 0.1

29

Li Linear r recursions

slide-30
SLIDE 30
  • In linear systems, long-term behavior depends entirely on the

eigenvalues of the hidden-layer weights matrix

– If the largest Eigen value is greater than 1, the system will “blow up” – If it is lesser than 1, the response will “vanish” very quickly – Complex Eigen values cause oscillatory response

  • Which we may or may not want
  • For smooth behavior, must force the weights matrix to have real Eigen values
  • Symmetric weight matrix

30

Le Lesson..

slide-31
SLIDE 31
  • The behavior of scalar non-linearities
  • Left: Sigmoid, Middle: Tanh, Right: Relu

– Sigmoid: Saturates in a limited number of steps, regardless of 𝑥B – Tanh: Sensitive to 𝑥B, but eventually saturates

  • “Prefers” weights close to 1.0

– Relu: Sensitive to 𝑥B, can blow up

ℎ 𝑢 = 𝑔(𝑥Bℎ 𝑢 − 1 + 𝑥F𝑦 𝑢 )

31

Ho How w abo about ut no non-lin linear earities ities (sc (scalar)

slide-32
SLIDE 32
  • With a negative start
  • Left: Sigmoid, Middle: Tanh, Right: Relu

– Sigmoid: Saturates in a limited number of steps, regardless of 𝑥B – Tanh: Sensitive to 𝑥B, but eventually saturates – Relu: For negative starts, has no response

ℎ 𝑢 = 𝑔(𝑥Bℎ 𝑢 − 1 + 𝑥F𝑦 𝑢 )

32

Ho How about non-linearities (sc (scalar)

slide-33
SLIDE 33
  • Assuming a uniform unit vector initialization

– 1,1,1, … / 𝑂

  • – Behavior similar to scalar recursion

– Interestingly, RELU is more prone to blowing up (why?)

  • Eigenvalues less than 1.0 retain the most “memory”

ℎ 𝑢 = 𝑔(𝑥Bℎ 𝑢 − 1 + 𝑥F𝑦 𝑢 )

sigmoid tanh relu

33

Ve Vector Process

slide-34
SLIDE 34

Ve Vector Process

  • Assuming a uniform unit vector initialization

– −1, −1, −1, … / 𝑂

  • – Behavior similar to scalar recursion

– Interestingly, RELU is more prone to blowing up (why?)

ℎ 𝑢 = 𝑔(𝑋ℎ 𝑢 − 1 + 𝐷𝑦 𝑢 )

sigmoid tanh relu

34

slide-35
SLIDE 35

St Stability Analysis

  • Formal stability analysis considers convergence of “Lyapunov” functions

– Alternately, Routh’s criterion and/or pole-zero analysis – Positive definite functions evaluated at ℎ – Conclusions are similar: only the tanh activation gives us any reasonable behavior

  • And still has very short “memory”
  • Lessons:

– Bipolar activations (e.g. tanh) have the best memory behavior – Still sensitive to Eigenvalues of 𝑋 – Best case memory is short – Exponential memory behavior

  • “Forgets” in exponential manner

35

slide-36
SLIDE 36
  • Recurrent networks retain information from the infinite past in principle
  • In practice, they tend to blow up or forget

– If the largest Eigen value of the recurrent weights matrix is greater than 1, the network response may blow up – If its less than one, the response dies down very quickly

  • The “memory” of the network also depends on the activation of the hidden

units

– Sigmoid activations saturate and the network becomes unable to retain new information – RELUs blow up – Tanh activations are the most effective at storing memory

  • But still, for not very long

36

St Story so far

slide-37
SLIDE 37

RN RNNs.. s..

  • Excellent models for time-series analysis tasks

– Time-series prediction – Time-series classification – Sequence prediction.. – They can even simplify problems that are difficult for MLPs

  • But the memory isn’t all that great..

– Also..

37

slide-38
SLIDE 38

Th The vanishing gradient problem

  • A particular problem with training deep networks..

– (Any deep network, not just recurrent nets) – The gradient of the error with respect to weights is unstable..

38

slide-39
SLIDE 39

Re Reminder: Tr Training deep networks

𝑃𝑣𝑢𝑞𝑣𝑢 = 𝑏[u] = 𝑔 𝑨[u] = 𝑔 𝑋[u]𝑏[uCD] = 𝑔 𝑋[u]𝑔(𝑋[uCD]𝑏[uCH] = 𝑔 𝑋[u]𝑔 𝑋[uCD] … 𝑔 𝑋[H]𝑔 𝑋[D]𝑦

For convenience, we use the same activation functions for all layers. However, output layer neurons most commonly do not need activation function (they show class scores or real-valued targets.)

𝑋[D] 𝑦 × 𝑔 𝑋[H] × 𝑔 𝑋[u] × 𝑔 𝑨[D] 𝑏[D] 𝑨[H] 𝑏[uCD] 𝑨[u] 𝑏[u] 𝑏[u] = 𝑝𝑣𝑢𝑞𝑣𝑢

39

slide-40
SLIDE 40
  • For

𝑀𝑝𝑡𝑡(𝑦) = 𝐹 𝑔[u] 𝑋[u]𝑔[uCD] 𝑋[uCD]𝑔[uCH] … 𝑋[D]𝑦

  • We get:

𝛼

x[y]𝑀𝑝𝑡𝑡 = 𝛼 x[z]𝑀𝑝𝑡𝑡. 𝛼𝑔[u]. 𝑋[u]. 𝛼𝑔[uCD]. 𝑋[uCD] … 𝛼𝑔[{ID]. 𝑋[{ID]

  • Where

– 𝛼

x[y]𝑀𝑝𝑡𝑡 is the gradient of the error w.r.t the output of the l-th layer of the network

  • Needed to compute the gradient of the error w.r.t 𝑋[{]

– 𝛼𝑔[{] is jacobian of 𝑔[{] w.r.t. to its current input – All blue terms are matrices

40

Re Reminder: Training deep deep ne networks

slide-41
SLIDE 41

Re Reminder: Gradient pr probl blems i in de n deep ne p netw twork rks

  • The gradients in the lower/earlier layers can explode or vanish

– Resulting in insignificant or unstable gradient descent updates – Problem gets worse as network depth increases

𝛼

x[y]𝑀𝑝𝑡𝑡 = 𝛼 x[z]𝑀𝑝𝑡𝑡. 𝛼𝑔[u]. 𝑋[u]. 𝛼𝑔[uCD]. 𝑋[uCD] … 𝛼𝑔[{ID]. 𝑋[{ID]

41

slide-42
SLIDE 42
  • As we go back in layers, the Jacobians of the activations constantly

shrink the derivative

– After a few layers the derivative of the loss at any time is totally “forgotten”

𝛼

x[y]𝑀𝑝𝑡𝑡 = 𝛼 x[z]𝑀𝑝𝑡𝑡. 𝛼𝑔[u]. 𝑋[u]. 𝛼𝑔[uCD]. 𝑋[uCD] … 𝛼𝑔[{ID]. 𝑋[{ID]

42

Re Reminder: Training deep deep ne networks

slide-43
SLIDE 43

Expl Explodi ding ng/Vani nishi shing ng gr gradi dients

𝛼

x|𝑀𝑝𝑡𝑡 = 𝛼𝑀𝑝𝑡𝑡. 𝛼𝑔 }. 𝑋 }CD. 𝛼𝑔 }CD. 𝑋 }CH … 𝛼𝑔 ?ID𝑋 ?

  • Every blue term is a matrix
  • The chain product for 𝛼

x|𝑀𝑝𝑡𝑡 will

– Expand 𝛼𝑀𝑝𝑡𝑡 in directions where each stage has singular values greater than 1 – Shrink 𝛼𝑀𝑝𝑡𝑡 in directions where each stage has singular values less than 1

43

slide-44
SLIDE 44

Va Vanishing gradients

  • ELU activations maintain gradients longest
  • But in all cases gradients effectively vanish after about 10 layers!

– Your results may vary

  • Both batch gradients and gradients for individual instances disappear

– In reality a tiny number will actually blow up.

44

slide-45
SLIDE 45
  • 𝛼𝑔() is the derivative of the output of the (layer of) hidden

recurrent neurons with respect to their input

– For vector activations: A full matrix – For scalar activations: A matrix where the diagonal entries are the derivatives of the activation of the recurrent hidden layer

ℎX(𝑢) = 𝑔 𝑨X 𝑢 𝑌 ℎ 𝑍 𝛼𝑔 𝑨(𝑢) = 𝑔~(𝑨),D) ⋯ 𝑔~(𝑨),H) ⋯ ⋮ ⋮ ⋱ ⋮ ⋯ 𝑔~(𝑨),})

45

Th The Jacobian of the hidden layers fo for an an RNN

slide-46
SLIDE 46
  • The derivative (or subgradient) of the activation function is always bounded

– The diagonals (or singular values) of the Jacobian are bounded

  • There is a limit on how much multiplying a vector by the Jacobian will scale it

46

Th The Jacobian

ℎX(𝑢) = 𝑔 𝑨X 𝑢 𝑌 ℎ 𝑍 𝛼𝑔 𝑨(𝑢) = 𝑔~(𝑨),D) ⋯ 𝑔~(𝑨),H) ⋯ ⋮ ⋮ ⋱ ⋮ ⋯ 𝑔~(𝑨),})

slide-47
SLIDE 47

Th The derivative of the hidden state activation

  • Most common activation functions, such as sigmoid, tanh() and RELU

have derivatives that are always less than 1

  • The most common activation for the hidden units in an RNN is the tanh()

– The derivative of tanh ()is never greater than 1 (and mostly less than 1)

  • Multiplication by the Jacobian is always a shrinking operation

𝛼𝑔

) 𝑨X =

𝑔

),D ~ (𝑨D)

⋯ 𝑔

),H ~ (𝑨H)

⋯ ⋮ ⋮ ⋱ ⋮ ⋯ 𝑔

),} ~ (𝑨})

47

slide-48
SLIDE 48

Th The derivative of the hidden state activation

  • Most common activation functions, such as sigmoid, tanh() and RELU have

derivatives that are always less than 1

  • The most common activation for the hidden units in an RNN is the tanh()

– The derivative of tanh ()is never greater than 1 (and mostly less than 1)

  • Multiplication by the Jacobian is always a shrinking operation

48

slide-49
SLIDE 49

𝛼

x[•]𝑀𝑝𝑡𝑡 = 𝛼 x[‚]𝑀𝑝𝑡𝑡. 𝛼𝑔[ƒ]. 𝑋. 𝛼𝑔[ƒCD]. 𝑋 … 𝛼𝑔[)ID]. 𝑋

  • In a single-layer RNN, the weight matrices are identical

– The conclusion below holds for any deep network, though

  • The chain product for 𝛼

x[•]𝑀𝑝𝑡𝑡 will

– Expand 𝛼

x[‚]𝑀𝑝𝑡𝑡 along directions in which the singular values of the weight

matrices are greater than 1 – Shrink 𝛼

x[‚]𝑀𝑝𝑡𝑡 in directions where the singular values are less than 1

– Repeated multiplication by the weights matrix will result in Exploding or vanishing gradients

49

Wha What abo bout ut the he weigh ghts

slide-50
SLIDE 50
  • The relation between 𝑌(1) and 𝑍(𝑈) is one of a very deep network

– Gradients from errors at t = 𝑈 will vanish by the time they’re propagated to 𝑢 = 1

X(1)

hf(0)

Y(T)

50

Re Recurrent nets are very deep nets

slide-51
SLIDE 51

Re Recall: Vanishing stuff..

  • Stuff gets forgotten in the forward pass too

– Each weights matrix and activation can shrink components of the input

h-1

𝑌(0) 𝑌(1) 𝑌(2) 𝑌(𝑈 − 2) 𝑌(𝑈 − 1) 𝑌(𝑈) 𝑍(0) 𝑍(1) 𝑍(2) 𝑍(𝑈 − 2) 𝑍(𝑈 − 1) 𝑍(𝑈)

51

slide-52
SLIDE 52

Training RNNs is hard

  • The unrolled network can be very deep and inputs from many time

steps ago can modify output

– Unrolled network is very deep

  • Multiply the same matrix at each time step during forward prop

52

slide-53
SLIDE 53

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 ____

This slide has been adopted from Socher lectures, cs224d, Stanford, 2017

53

slide-54
SLIDE 54

Th The long-te term dependency problem

  • Must know to “remember” for extended periods of time and “recall”

when necessary

– Can be performed with a multi-tap recursion, but how many taps? – Need an alternate way to “remember” stuff

54

slide-55
SLIDE 55
  • Recurrent networks retain information from the infinite past in principle
  • In practice, they are poor at memorization

– The hidden outputs can blow up, or shrink to zero depending on the Eigen values of the recurrent weights matrix – The memory is also a function of the activation of the hidden units

  • Tanh activations are the most effective at retaining memory, but even they don’t hold it very long
  • Deep networks also suffer from a “vanishing or exploding gradient” problem

– The gradient of the error at the output gets concentrated into a small number of parameters in the earlier layers, and goes to zero for others

55

St Story so far

slide-56
SLIDE 56

Th The long-te term dependency problem

  • Any other pattern of any length can happen between pattern 1 and pattern 2

– RNN will “forget” pattern 1 if intermediate stuff is too long – “Jane” à the next pronoun referring to her will be “she”

  • Must know to “remember” for extended periods of time and “recall” when necessary

– Can be performed with a multi-tap recursion, but how many taps? – Need an alternate way to “remember” stuff

PATTERN1 […………………………..] PATTERN 2

1

Jane had a quick lunch in the bistro. Then she...

56

slide-57
SLIDE 57

St Standard RNN

  • Recurrent neurons receive past recurrent outputs and current input as inputs
  • Processed through a tanh() activation function

– As mentioned earlier, tanh() is the generally used activation for the hidden layer

  • Current recurrent output passed to next higher layer and next time instant

57

slide-58
SLIDE 58

Vanilla RNN Gradient Flow

58

slide-59
SLIDE 59

Vanilla RNN Gradient Flow

59

slide-60
SLIDE 60

Vanilla RNN Gradient Flow

Largest singular value > 1: Exploding gradients Largest singular value < 1: Vanishing gradients

60

slide-61
SLIDE 61

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.

61

slide-62
SLIDE 62

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

62

slide-63
SLIDE 63

Vanilla RNN Gradient Flow

Computing gradient of h0 involves many factors

  • f W (and repeated tanh)

Largest singular value > 1: Exploding gradients Largest singular value < 1: Vanishing gradients Gradient clipping: Scale Computing gradient if its norm is too big Change RNN architecture

63

slide-64
SLIDE 64

En Enter the he LSTM TM

  • Long Short-Term Memory
  • Explicitly latch information to prevent decay / blowup
  • Following notes borrow liberally from
  • http://colah.github.io/posts/2015-08-Understanding-LSTMs/

64

slide-65
SLIDE 65

Better units for recurrent models

  • More complex hidden unit computation in recurrence!

– ℎ) = 𝑀𝑇𝑈𝑁(𝑦), ℎ)CD) – ℎ) = 𝐻𝑆𝑉(𝑦), ℎ)CD)

  • Main ideas:

–keep around memories to capture long distance dependencies –allow error messages to flow at different strengths depending on the inputs

65

slide-66
SLIDE 66
  • Can we replace this with something that doesn’t fade or blow up?
  • Can we have a network that just “remembers” arbitrarily long, to be

recalled on demand?

– Not be directly dependent on vagaries of network parameters, but rather on input-based determination of whether it must be remembered – Replace them, e.g., by a function of the input that decides if things must be forgotten or not

66

Expl Explodi ding ng/Vani nishi shing ng gr gradi dients

slide-67
SLIDE 67

St Standard RNN

  • Recurrent neurons receive past recurrent outputs and current input as inputs
  • Processed through a tanh() activation function

– As mentioned earlier, tanh() is the generally used activation for the hidden layer

  • Current recurrent output passed to next higher layer and next time instant

67

slide-68
SLIDE 68

Some visualization

68

slide-69
SLIDE 69

Lo Long Sh Short rt-Te Term Memory

  • The 𝜏() are multiplicative gates that decide if something is important
  • r not
  • Remember, every line actually represents a vector

69

slide-70
SLIDE 70

LSTM TM: Constant Error Carousel

  • Key component: a remembered cell state

70

slide-71
SLIDE 71

LSTM TM: CEC

  • 𝐷) is the linear history
  • Carries information through, only affected by a gate

– And addition of history, which too is gated...

71

slide-72
SLIDE 72

LSTM TM: Gates

  • Gates are simple sigmoidal units with outputs in the range (0,1)
  • Controls how much of the information is to be let through

72

slide-73
SLIDE 73
  • The first gate determines whether to carry over the history or to forget it

– More precisely, how much of the history to carry over – Also called the “forget” gate – Note, we’re actually distinguishing between the cell memory 𝐷 and the state ℎ that is coming over time! They’re related though

73

LSTM TM: Forget gate

slide-74
SLIDE 74

LSTM TM: Input gate

  • The second input has two parts

– A perceptron layer that determines if there’s something new and interesting in the input – A gate that decides if its worth remembering – If so its added to the current memory cell

74

slide-75
SLIDE 75

LSTM TM: Memory cell update

  • The second input has two parts

– A perceptron layer that determines if there’s something interesting in the input – A gate that decides if its worth remembering – If so its added to the current memory cell

75

slide-76
SLIDE 76

LSTM TM: Output and Output gate

  • The output of the cell

– Simply compress it with tanh to make it lie between 1 and -1

  • Note that this compression no longer affects our ability to carry memory forward

– Controlled by an output gate

  • To decide if the memory contents are worth reporting at this time

76

slide-77
SLIDE 77

77

  • 𝒋𝒖: input gate, how much of the new information will be let through the memory cell.
  • 𝒈𝒖: forget gate, responsible for information should be thrown away from memory cell.
  • 𝒑𝒖: output gate, how much of the information will be passed to expose to the next time step.
  • 𝒉𝒖 or 𝒅
  • 𝒖: self-recurrent which is equal to standard RNN
  • 𝒅𝒖: internal memory of the memory cell
  • 𝒊𝒖: hidden state

LSTM TM Equations

slide-78
SLIDE 78

Long-short-term-memories (LSTMs)

  • Gates

– Input gate (current cell matter): 𝑗) = 𝜏 𝑋

X

ℎ)CD 𝑦) + 𝑐X – Forget (gate 0, forget past): 𝑔

) = 𝜏 𝑋 x

ℎ)CD 𝑦) + 𝑐x – Output (how much cell is exposed): 𝑝) = 𝜏 𝑋

ℎ)CD 𝑦) + 𝑐“

  • Variables

– New memory cell: 𝐷 ”) = tanh 𝑋

  • ℎ)CD

𝑦) + 𝑐• – Final memory cell: 𝐷) = 𝑗) ∘ 𝐷 ”) + 𝑔

) ∘ 𝐷)CD

– Final hidden state: ℎ) = 𝑝) ∘ tanh 𝐷)

78

slide-79
SLIDE 79

LSTM TM: Th The “Peephole” co connection

  • The raw memory is informative by itself and can also be input

– Note, we’re using both 𝐷 and ℎ

79

slide-80
SLIDE 80
  • Forward rules:

𝑦) ℎ)CD ℎ) 𝐷)CD 𝐷) 𝑔

)

𝑗) 𝑝) 𝐷 ”)

s() s() s()

tanh tanh

Gates Variables

80

LSTM TM: Th The “Peephole” connection

slide-81
SLIDE 81

# Assuming h(0,*) is known and C(0,*)=0 # Assuming L hidden-state layers and an output layer # Note: LSTM_cell is an indexed class with functions # [W{l},b{l}] are the entire set of weights and biases # for the lth hidden layer # Wo and bo are output layer weights and biases for t = 1:T # Including both ends of the index h(t,0) = x(t) # Vectors. Initialize hidden layer h(0) to input for l = 1:L # hidden layers operate at time t [C(t,l),h(t,l)] = LSTM_cell(t,l).forward(C(t-1,l),h(t-1,l) ,h(t,l-1)[W{l},b{l}]) zo(t) = Woh(t,L) + bo Y(t) = softmax( zo(t) )

81

LSTM TM network forward

slide-82
SLIDE 82

# Continuing from previous slide # Note: [W,h] is a set of parameters, whose individual elements are # shown in red within the code. These are passed in # Static local variables which aren’t required outside this cell static local zf, zi, zc, f, i, o, Ci function [Co, ho] = LSTM_cell.forward(C,h,x, [W,h]) zf = WfcC + Wfhh + Wfxx + bf f = sigmoid(zf) # forget gate zi = WicC + Wihh + Wixx + bi i = sigmoid(zi) # input gate zc = WccC + Wchh + Wcxx + bc Ci = tanh(zc) # Detecting input pattern Co = f∘C + i∘Ci # “∘” is component-wise multiply zo = WocCo + Wohh + Woxx + bo

  • = sigmoid(zo) # output gate

ho = o∘tanh(C) # “∘” is component-wise multiply return Co,ho

82

LSTM TM cell forward

slide-83
SLIDE 83

Long Short Term Memory (LSTM)

g in the previous slides was called 𝑑̃

83

slide-84
SLIDE 84

Long Short Term Memory (LSTM)

g in the previous slides was called 𝑑̃

84

Wi Wo Wf Wg

slide-85
SLIDE 85

Long Short Term Memory (LSTM)

[Hochreiter et al., 1997]

85

slide-86
SLIDE 86

Long Short Term Memory (LSTM)

[Hochreiter et al., 1997]

86

slide-87
SLIDE 87

Long Short Term Memory (LSTM)

[Hochreiter et al., 1997]

87

slide-88
SLIDE 88

Ga Gated Recu current t Un Units its: Lets simplify the LSTM TM

  • Don’t bother to separately maintain compressed and regular memories

– Pointless computation!

  • But compress it before using it to decide on the usefulness of the current

input!

88

slide-89
SLIDE 89

GRUs

  • Gated Recurrent Units (GRU) introduced by Cho et al. 2014
  • Update gate

𝑨) = 𝜏 𝑋

ℎ)CD 𝑦) + 𝑐™

  • Reset gate

𝑠

) = 𝜏 𝑋 +

ℎ)CD 𝑦) + 𝑐+

  • Memory

ℎ ›) = tanh 𝑋

b

𝑠

) ∘ ℎ)CD

𝑦) + 𝑐b

  • Final Memory

ℎ) = 𝑨) ∘ ℎ)CD + 1 − 𝑨) ∘ ℎ ›)

If reset gate unit is ~0, then this ignores previous memory and

  • nly stores the new input

89

slide-90
SLIDE 90

GRU intuition

  • Units with long term dependencies have active update gates z
  • Illustration:

90

This slide has been adopted from Socher lectures, cs224d, Stanford, 2017

slide-91
SLIDE 91

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

91

This slide has been adopted from Socher lectures, cs224d, Stanford, 2017

slide-92
SLIDE 92

Other RNN Varients

92

slide-93
SLIDE 93

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.

93

slide-94
SLIDE 94

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

94

slide-95
SLIDE 95

Multi-layer RNN

95

slide-96
SLIDE 96
  • Each green box is now an entire LSTM or GRU unit
  • Also keep in mind each box is an array of units

Time X(t) Y(t)

96

Mu Multi-layer LSTM TM architecture

slide-97
SLIDE 97
  • Recurrent networks are poor at memorization

– Memory can explode or vanish depending on the weights and activation

  • They also suffer from the vanishing gradient problem during training

– Error at any time cannot affect parameter updates in the too-distant past – E.g. seeing a “close bracket” cannot affect its ability to predict an “open bracket” if it happened too long ago in the input

  • LSTMs are an alternative formalism where memory is made more directly

dependent on the input, rather than network parameters/structure

– Through a memory structure with no weights or activations, but instead direct switching and “increment/decrement” from pattern recognizers – Do not suffer from a vanishing gradient problem but do suffer from exploding gradient issue

97

St Story so far

slide-98
SLIDE 98

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.

98