recurrent neural networks and long-short term memory...

35
3/3/2020 1 Recurrent neural networks and Long-short term memory (LSTM) Jeong Min Lee CS3750, University of Pittsburgh Outline RNN RNN Unfolding Computational Graph Backpropagation and weight update Explode / Vanishing gradient problem LSTM GRU Tasks with RNN Software Packages

Upload: others

Post on 21-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

1

Recurrent neural networks and Long-short term memory (LSTM)

Jeong Min Lee

CS3750, University of Pittsburgh

Outline

• RNN• RNN

• Unfolding Computational Graph

• Backpropagation and weight update

• Explode / Vanishing gradient problem

• LSTM

• GRU

• Tasks with RNN

• Software Packages

Page 2: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

2

So far we are

• Modeling sequence (time-series) and predicting future values by probabilistic models (AR, HMM, LDS, Particle Filtering, Hawkes Process, etc)

• E.g. LDS• Observation 𝑥𝑡 is modeled as emission

matrix 𝐶, hidden state 𝑧𝑡 with Gaussian noise 𝑤𝑡

• The hidden state is also probabilistically computed with transition matrix 𝐴 and Gaussian noise 𝑣𝑡

𝑧𝑡−1

𝑥𝑡−1

𝑧𝑡

𝑥𝑡

𝑧𝑡+1

𝑥𝑡+1

𝑥𝑡 = 𝐶𝑧𝑡 + 𝑤𝑡 ; 𝑤𝑡~𝑁 𝑤 0, Σ

𝑧𝑡 = 𝐴𝑧𝑡−1 + 𝑣𝑡 ; 𝑣𝑡~𝑁(𝑤|0, Γ)

Paradigm Shift to RNN

• We are moving into a new world where no probabilistic component exists in a model

• That is, we may not need to inference like in LDS and HMM• In RNN, hidden states bear no probabilistic form or assumption

• Given fixed input and target from data, RNN is to learn intermediate association between them and also the real-valued vector representation

Page 3: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

3

RNN

• RNN’s input, output, and internal representation (hidden states) are all real-valued vectors

ℎ𝑡 = tanh 𝑈𝑥𝑡 +𝑊ℎ𝑡−1

ො𝑦 = λ(𝑉ℎ𝑡)

• ℎ𝑡: hidden states; real-valued vector• 𝑥𝑡: input vector (real-valued)• 𝑉ℎ𝑡: real-valued vector• ො𝑦 : output vector (real-valued)

RNN

• RNN consists of three parameter matrices (𝑈, 𝑊,𝑉) with activation functions

ℎ𝑡 = tanh 𝑈𝑥𝑡 +𝑊ℎ𝑡−1

ො𝑦 = λ(𝑉ℎ𝑡)

• 𝑈: input-hidden matrix• 𝑊: hidden-hidden matrix• 𝑉: hidden-output matrix

Page 4: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

4

RNN

• tanh ∙ is a tangent hyperbolic function. It models non-linearity.

ℎ𝑡 = tanh 𝑈𝑥𝑡 +𝑊ℎ𝑡−1

ො𝑦 = λ(𝑉ℎ𝑡)

zta

nh

(z)

RNN

• λ ∙ is output transformation function

• It can be any function and selected for a task and type of target in data

• It can be even another feed-forward neural network and it makes RNN to model anything, without any restriction

ℎ𝑡 = tanh 𝑈𝑥𝑡 +𝑊ℎ𝑡−1

ො𝑦 = λ(𝑉ℎ𝑡)

• Sigmoid: binary probability distribution• Softmax: categorical probability distribution• ReLU: positive real-value output• Identity function: real-value output

Page 5: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

5

Make a prediction

• Let’s see how it makes a prediction

• In the beginning, initial hidden state ℎ0 is filled with zero or random value

• Also we assume the model is already trained. (we will see how it is trained soon)

𝑥1

ℎ0

Make a prediction

• Assume we currently have observation 𝑥1 and want to predict 𝑥2

• We compute hidden states ℎ1 first

ℎ1 = tanh 𝑈𝑥1+𝑊ℎ0

𝑥1

ℎ1ℎ0𝑊

𝑈

Page 6: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

6

Make a prediction

• Then we generate prediction:

• 𝑉ℎ1 is a real-valued vector or scalar value (depends on the size of output matrix 𝑉)

ℎ1 = tanh 𝑈𝑥1+𝑊ℎ0

ො𝑥2 = ො𝑦 = λ(𝑉ℎ1)

𝑥1 ො𝑥2

ℎ1ℎ0𝑊

𝑈

𝑉, λ()

ො𝑥2

Make a prediction multiple steps

• In prediction for multiple steps a head, predicted value ො𝑥2 from previous step is considered as input 𝑥2 at time step 2

ℎ2 = tanh 𝑈ො𝑥2 +𝑊ℎ1

ො𝑥3 = ො𝑦 = λ(𝑉ℎ2)

𝑥1 ො𝑥3

ℎ1ℎ0𝑊

𝑈

𝑉, λ()

ො𝑥2

ℎ2𝑊

𝑈

𝑉, λ()

ො𝑥3

ො𝑥2

Page 7: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

7

Make a prediction multiple steps

• Same mechanism applies forward in time..

ℎ3 = tanh 𝑈ො𝑥3 +𝑊ℎ2

ො𝑥4 = ො𝑦 = λ(𝑉ℎ3)

𝑥1 ො𝑥3

ℎ1ℎ0𝑊

𝑈

𝑉, λ()

ො𝑥2

ℎ2𝑊

𝑈

𝑉, λ()

ො𝑥3

ො𝑥2 ො𝑥4

ℎ3𝑊

𝑈

𝑉, λ()

ො𝑥4

RNN Characteristic

• You might observed that…

• Parameters 𝑈, 𝑉,𝑊 are shared across all time steps

• No probabilistic component (random number generation) is involved

• So, everything is deterministic

𝑥1 ො𝑥3

ℎ1ℎ0𝑊

𝑈

𝑉, λ()

ො𝑥2

ℎ2𝑊

𝑈

𝑉, λ()

ො𝑥3

ො𝑥2 ො𝑥4

ℎ3𝑊

𝑈

𝑉, λ()

ො𝑥4

Page 8: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

8

Another way to see RNN

• RNN is a type of neural network

Neural Network

• Cascading several linear weights with nonlinear activation functions in between them

• 𝑦: output

• 𝑉: Hidden-Output matrix

• ℎ: hidden units (states)

• 𝑈: Input-Hidden matrix

• 𝑥: input

𝑥

𝑦

𝑈

𝑉

Page 9: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

9

Neural Network

• In traditional NN, it is assumed that every input is independent each other

• But with sequential data, input in current time step is highly likely depends on input in previous time step

• We need some additional structure that can model dependencies of inputs over time

𝑥

𝑦

𝑈

𝑉

Recurrent Neural Network

• A type of a neural network that has a recurrence structure

• The recurrence structure allows us to operate over a sequence of vectors

𝑥

𝑦

𝑈

𝑉𝑊

Page 10: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

10

RNN as an Unfolding Computational Graph

𝑥

𝑦

𝑈

𝑉𝑊

𝑥𝑡−1

ℎ𝑡−1

ො𝑦𝑡−1

𝑈

𝑉

𝑥𝑡

ℎ𝑡

ො𝑦𝑡

𝑈

𝑉

𝑊

𝑥𝑡+1

ℎ𝑡+1

ො𝑦𝑡+1

𝑈

𝑉

ℎ…

ℎ…

𝑊𝑊 𝑊

Unfold

RNN as an Unfolding Computational Graph

RNN can be converted into a feed-forward neural network by unfolding over time

𝑥

𝑦

𝑈

𝑉𝑊

𝑥𝑡−1

ℎ𝑡−1

ො𝑦𝑡−1

𝑈

𝑉

𝑥𝑡

ℎ𝑡

ො𝑦𝑡

𝑈

𝑉

𝑊

𝑥𝑡+1

ℎ𝑡+1

ො𝑦𝑡+1

𝑈

𝑉

ℎ…

ℎ…

𝑊𝑊 𝑊

Unfold

Page 11: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

11

How to train RNN?

• Before make train happen, we need to define these:

• 𝑦𝑡: true target

• ො𝑦𝑡: output of RNN (=prediction for true target)

• 𝐸𝑡: error (loss); difference between the true target and the output

• As the output transformation function 𝜆 is selected by the task and data, so does the loss:

• Binary Classification: Binary Cross Entropy• Categorical Classification: Cross Entropy• Regression: Mean Squared Error

With the loss, the RNN will be like:

Unfold

𝑥𝑡−1

ℎ𝑡−1

ො𝑦𝑡−1

𝐸𝑡−1

𝑦𝑡−1

𝑈

𝑉

𝑥𝑡

ℎ𝑡

ො𝑦𝑡

𝐸𝑡

𝑦𝑡

𝑈

𝑉

𝑊

𝑥𝑡+1

ℎ𝑡+1

ො𝑦𝑡+1

𝐸𝑡+1

𝑦𝑡+1

𝑈

𝑉

ℎ…

ℎ…

𝑊𝑊 𝑊

𝑥

ො𝑦

𝐸

𝑦

𝑈

𝑉𝑊

Page 12: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

12

Back Propagation Through Time (BPTT)

• Extension of standard backpropagation that performs gradient descent on an unfolded network

• Goal is to calculate gradients of the error with respect to parameters U, V, and Wand learn desired parameters using Stochastic Gradient Descent

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉

𝑦1 𝑦3

Back Propagation Through Time (BPTT)

• To update in one training example (sequence), we sum up the gradients at each time of the sequence:

𝜕𝐸

𝜕𝑊=

𝑡

𝜕𝐸𝑡

𝜕𝑊

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉

𝑦1 𝑦3

Page 13: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

13

Learning Parameters

• Let

ℎ𝑡 = tanh(𝑈𝑥𝑡 +𝑊ℎ𝑡−1)𝑧𝑡 = 𝑈𝑥𝑡 +𝑊ℎ𝑡−1ℎ𝑡 = tanh(𝑧𝑡)

𝜆𝑘 =𝜕ℎ𝑘𝜕𝑊

𝛼𝑘 =𝜕ℎ𝑘𝜕𝑧𝑘

= 1 − ℎ𝑘2

𝛽𝑘 =𝜕𝐸𝑘

𝜕ℎ𝑘= 𝑜𝑘 − 𝑦𝑘 𝑉

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉

𝑦1 𝑦3

Learning Parameters

𝜕𝐸𝑘

𝜕𝑊=𝜕𝐸𝑘

𝜕ℎ𝑘

𝜕ℎ𝑘𝜕𝑊

= 𝛽𝑘𝜆𝑘

𝜓𝑘 =𝜕ℎ𝑘𝜕𝑈

= 𝛼𝑘𝜕𝑧𝑘𝜕𝑈

= 𝛼𝑘(𝑥𝑘 +𝑊𝜓𝑘−1)

𝜆𝑘 =𝜕ℎ𝑘𝜕𝑊

=𝜕ℎ𝑘𝜕𝑧𝑘

𝜕𝑧𝑘𝜕𝑊

= 𝛼𝑘(ℎ𝑘−1 +𝑊𝜆𝑘−1)

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉

𝑦1 𝑦3

Page 14: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

14

𝜓𝑘 = 𝛼𝑘(𝑥𝑘 +𝑊𝜓𝑘−1)

𝛼0 = 1 − ℎ02; 𝜆0= 0; 𝜓0 = 𝛼0 ∙ 𝑥0

Δ𝑤 = 0 ; Δ𝑢 = 0 ; Δ𝑣 = 0

For k= 1...T (T; length of a sequence):

𝛼𝑘 = 1 − ℎ𝑘2

𝜆𝑘 = 𝛼𝑘(ℎ𝑘−1 +𝑊𝜆𝑘−1)

𝛽𝑘 = 𝑜𝑘 − 𝑦𝑘 𝑉

Δ𝑤 = Δ𝑤 + 𝛽𝑘𝜆𝑘

Δ𝑢 = Δ𝑢 + 𝛽𝑘𝜓𝑘

Δ𝑣 = Δ𝑣 + 𝑜𝑘 − 𝑦𝑘 ⊗ℎ𝑘

Initialization:

𝑉𝑛𝑒𝑤 = 𝑉𝑜𝑙𝑑 − 𝛼Δ𝑣𝑊𝑛𝑒𝑤 = 𝑊𝑜𝑙𝑑 − 𝛼Δ𝑤𝑈𝑛𝑒𝑤 = 𝑈𝑜𝑙𝑑 − 𝛼Δ𝑢

𝛼: learning rate⊗: element-wise multiplication

Then,

Exploding and Vanishing Gradient Problem

• In RNN, we repeatedly multiply W along with a input sequence

• The recurrence multiplication can result in difficulties called exploding and vanishing gradient problem

ℎ𝑡 = tanh(𝑈𝑥𝑡 +𝑊ℎ𝑡−1)

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉

𝑦1 𝑦3

Page 15: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

15

Exploding and Vanishing Gradient Problem

• For example, we can think of simple RNN with lacking inputs 𝒙

• It can be simplified to

• If 𝑊 has an Eigen decomposition, we can decompose 𝑊 into 𝑉 (consists of eigen vectors) and a diagonal matrix of eigen values: diag 𝜆

ℎ𝑡 = (𝑊𝑡)ℎ0

ℎ𝑡 = 𝑊ℎ𝑡−1

𝑊 = 𝑉 diag 𝜆 𝑉−1

𝑊𝑡 = (𝑉 diag 𝜆 𝑉−1)𝑡= 𝑉 diag 𝜆𝑡 𝑉−1

Exploding and Vanishing Gradient Problem

• Any eigenvalues 𝜆𝑖 that are not near an absolute value of 1 will either

• explode if they are greater than 1 in magnitude

• vanish if they are less than 1 in magnitude

• The gradients through such a graph are also scaled according to diag 𝜆𝑡

ℎ1

𝑊

ℎ2

𝑊

ℎ3

ℎ𝑡 = 𝑉 diag 𝜆𝑡 𝑉−1ℎ0

ℎ𝑡 = (𝑊𝑡)ℎ0

Page 16: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

16

Exploding and Vanishing Gradient Problem

• Whenever the model is able to represent long-term dependencies, the gradient of a long-term interaction has exponentially smaller magnitude than the gradient of a short-term interaction

• That is, it is not impossible to learn, but that it might take a very long time to learn long-term dependencies:

• Because the signal about these dependencies will tend to be hiddenby the smallest fluctuations arising from short-term dependencies

ℎ𝑡 = 𝑉 diag 𝜆𝑡 𝑉−1ℎ0

Vanishing Gradient • Tanh function has derivatives of 0 at both

ends. (They approach a flat line)• When this happens we say the

corresponding neurons are saturated. • They have a zero gradient and drive other

gradients in previous layers towards 0. • Thus, with small values in the matrix and

multiple matrix multiplications the gradient values are shrinking exponentially fast, eventually vanishing completely after a few time steps. [WildML 2015]

Tanh f(x) and its derivative

Page 17: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

17

Solution1: Truncated BPTT

• Run forward as it is, but run the backward in the chunk of the sequence instead of the whole sequence

𝑦2

𝑥1

ℎ1

ො𝑦1

𝐸1

𝑈

𝑉𝑊

𝑥2

ℎ2

ො𝑦2

𝐸2

𝑈

𝑉𝑊

𝑥3

ℎ3

ො𝑦3

𝐸3

𝑈

𝑉𝑊

𝑦1 𝑦3 𝑦5

𝑥4

ℎ4

ො𝑦4

𝐸4

𝑈

𝑉𝑊

𝑥5

ℎ5

ො𝑦5

𝐸5

𝑈

𝑉𝑊

𝑥6

ℎ6

ො𝑦6

𝐸6

𝑈

𝑉

𝑦4 𝑦6

Solution2: Gating mechanism (LSTM;GRU)

• Add gates to produce paths where gradients can flow more constantly in longer-term without vanishing nor exploding

• We’ll see in next chapter

Page 18: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

18

Outline

• RNN

• LSTM

• GRU

• Tasks with RNN

• Software Packages

Long Short-term Memory (LSTM)

• Capable of modeling longer term dependencies by having memory cells and gates that controls the information flow along with the memory cells

Page 19: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

19

Long Short-term Memory (LSTM)

• Capable of modeling longer term dependencies by having memory cells and gates that controls the information flow along with the memory cells

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

Long Short-term Memory (LSTM)

• The contents of the memory cells 𝐶𝑡 are regulated by various gates:

• Forget gate 𝑓𝑡

• Input gate 𝑖𝑡

• Reset gate 𝑟𝑡

• Output gate 𝑜𝑡

• Each gates are composed of affine transformation with Sigmoid activation function

Page 20: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

20

Forget Gate

• It determines how much contents from previous cell 𝐶𝑡−1 will be erased (we will see how it works in next a few slides)

• Linear transformation of concatenated previous hidden states and input are followed by Sigmoid function

• The sigmoid generates values 0 and 1:

• 0 : completely remove info in the dimension

• 1 : completely keep info in the dimension

𝑓𝑡 = 𝜎(𝑊𝑓 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑓)

New Candidate Cell and Input Gate

• New candidate cell states ሚ𝐶𝑡 are created as a function of ℎ𝑡−1 and 𝑥𝑡

• Input gates 𝑖𝑡 decides how much of values of the new candidate cell states ሚ𝐶𝑡 are combined into the cell states

ሚ𝐶𝑡 = tanh(𝑊𝐶 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝐶)𝑖𝑡 = 𝜎(𝑊𝑖 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑖)

Page 21: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

21

Update Cell States

• The previous cell states 𝐶𝑡−1 are updated to the new cell states 𝐶𝑡 by using the input and forget gates with new candidate cell states

𝐶𝑡 = 𝑓𝑡 ∗ 𝐶𝑡−1 + 𝑖𝑡 ∗ ሚ𝐶𝑡

Generate Output

• Output will be based on cell state 𝐶𝑡with filter from output gate 𝑜𝑡

• The output gate 𝑜𝑡 decides which part of cell state 𝐶𝑡 will be in the output

• Then the final output is generated from tanh-ed cell states filtered by 𝑜𝑡

ℎ𝑡 = 𝑜𝑡 ∗ tanh 𝐶𝑡

𝑜𝑡 = 𝜎(𝑊𝑜 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑜)

Page 22: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

22

Outline

• RNN

• LSTM

• GRU

• Tasks with RNN

• Software Packages

Gated Recurrent Unit (GRU)

• Simplify LSTM by combining forget and input gate into update gate 𝑧𝑡• 𝑧𝑡 controls the forgetting factor and the decision to update the state unit

ℎ𝑡 = 1 − 𝑧𝑡 ∗ ℎ𝑡−1 + 𝑧𝑡 ∗ ෩ℎ𝑡

𝑧𝑡 = 𝜎(𝑊𝑧 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑧)

𝑟𝑡 = 𝜎(𝑊𝑟 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑟)

෩ℎ𝑡 = tanh 𝑊 ∙ 𝑟𝑡 ∗ ℎ𝑡−1, 𝑥𝑡 + 𝑏

Page 23: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

23

• Reset gates 𝑟𝑡 control which parts of the state get used to compute the next target state

• It introduces additional nonlinear effect in the relationship between past state and future state

Gated Recurrent Unit (GRU)

ℎ𝑡 = 1 − 𝑧𝑡 ∗ ℎ𝑡−1 + 𝑧𝑡 ∗ ෩ℎ𝑡

𝑧𝑡 = 𝜎(𝑊𝑧 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑧)

𝑟𝑡 = 𝜎(𝑊𝑟 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑟)

෩ℎ𝑡 = tanh 𝑊 ∙ 𝑟𝑡 ∗ ℎ𝑡−1, 𝑥𝑡 + 𝑏

Comparison LSTM and GRU

ℎ𝑡 = 1 − 𝑧𝑡 ∗ ℎ𝑡−1 + 𝑧𝑡 ∗ ෩ℎ𝑡

𝑧𝑡 = 𝜎(𝑊𝑧 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑧)

𝑟𝑡 = 𝜎(𝑊𝑟 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑟)

෩ℎ𝑡 = tanh 𝑊 ∙ 𝑟𝑡 ∗ ℎ𝑡−1, 𝑥𝑡 + 𝑏

𝑓𝑡 = 𝜎(𝑊𝑓 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑓)

ሚ𝐶𝑡 = tanh(𝑊𝐶 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝐶)

𝑖𝑡 = 𝜎(𝑊𝑖 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑖)

𝐶𝑡 = 𝑓𝑡 ∗ 𝐶𝑡−1 + 𝑖𝑡 ∗ ሚ𝐶𝑡

ℎ𝑡 = 𝑜𝑡 ∗ tanh 𝐶𝑡

𝑜𝑡 = 𝜎(𝑊𝑜 ∙ ℎ𝑡−1, 𝑥𝑡 + 𝑏𝑜)

LSTM GRU

ℎ𝑡−1

𝐶𝑡−1 𝐶𝑡

ℎ𝑡

ℎ𝑡

𝑥𝑡

Page 24: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

24

Comparison LSTM and GRU

• Greff, et al. (2015) compared LSTM, GRU and several variants on thousands of experiments and found that none of the variants can improve upon the standard LSTM architecture significantly, and demonstrate the forget gate and the output activation function to be its most critical components.

• Greff, et al. (2015): LSTM: A Search Space Odyssey

Outline

• RNN

• LSTM

• GRU

• Tasks with RNN• One-to-Many• Many-to-One• Many-to-Many• Encoder-Decoder Seq2Seq Model• Attention Mechanism• Bidirectional RNN

• Software Packages

Page 25: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

25

Tasks with RNN

• One of strengths of RNN is flexibility in modeling any task with any data type

• By composing the input and output as either sequence or non-sequence data, you can model many different tasks

• Here are some of the examples:

One-to-Many

• Input: non-sequence vector / Output: sequence of vectors

• After the first time step, hidden states are updated with only previous step’s hidden states

• Example: Sentence generation given image• Typically the input image is processed with CNN to generate

a real-valued vector representation

• During training, true target is a sentence (sequence of words) about the training image

Page 26: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

26

Many-to-One

• Input: sequence of vectors / Output: non-sequence vector

• Only the last time step’s hidden states is used as the output

• Example: Sequence classification, sentiment classification

Many-to-Many

• Input: sequence of vectors / Output: sequence of vectors

• Generate a sequence given another sequence

• Example: Machine translation• Especially parameterized by what is called “Encoder-Decoder”

model

Page 27: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

27

• Key idea:

• Encoder RNN generates a fixed-length context vector 𝐶 from input sequence 𝑿 = (𝑥 1 , … , 𝑥 𝑛𝑥 )

• Decoder RNN generates an output sequence 𝒀 = (𝑦 1 , … , 𝑦 𝑛𝑦 )conditioned on the context 𝐶

• The two RNNs are trained jointly to maximize the average of

log𝑃(𝑦 1 , … , 𝑦 𝑛𝑦 |𝑥 1 , … , 𝑥 𝑛𝑥 ) over all sequence in training set

Encoder-Decoder (Seq2Seq) Model

• Typically, the last hidden states of encoder RNN ℎ 𝑛𝑥 is used as context 𝐶

• But when the context 𝐶 has smaller dimension or lengths of sequences are longer, 𝐶 can be a bottleneck; it cannot properly summarize the input sequence

Encoder-Decoder (Seq2Seq) Model

𝑥 1 𝑥 2 𝑥 3 … 𝑥 𝑛𝑥

ℎ 1 ℎ 2 ℎ 3 … ℎ 𝑛𝑥 𝑔 1 𝑔 2 𝑔 3 … 𝑔 𝑛𝑦

ො𝑦 1 ො𝑦 2 ො𝑦 3 … ො𝑦 𝑛𝑦

𝐶

Decoder RNN

Encoder RNN

Input sequence

Target sequence

Page 28: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

28

• Attention mechanism learns to associate hidden states of input sequence to generation of each step of the target sequence

Attention Mechanism

𝑥 1 𝑥 2 𝑥 3 … 𝑥 𝑛𝑥

ℎ 1 ℎ 2 ℎ 3 … ℎ 𝑛𝑥 𝑔 1 𝑔 2 𝑔 3 … 𝑔 𝑛𝑦

ො𝑦 1 ො𝑦 2 ො𝑦 3 … ො𝑦 𝑛𝑦

𝐶

Decoder RNN

Encoder RNN

Input sequence

Target sequence

𝑓Attention

Mechanism 𝑐2

𝛼 1 𝛼 2 𝛼 3 𝛼 𝑛𝑥

• The association is modeled as additional feed-forward network 𝑓 gets input sequence’s hidden states and predicted target on previous time step

Attention Mechanism

𝑥 1 𝑥 2 𝑥 3 … 𝑥 𝑛𝑥

ℎ 1 ℎ 2 ℎ 3 … ℎ 𝑛𝑥 𝑔 1 𝑔 2 𝑔 3 … 𝑔 𝑛𝑦

ො𝑦 1 ො𝑦 2 ො𝑦 3 … ො𝑦 𝑛𝑦

𝐶

Decoder RNN

Encoder RNN

Input sequence

Target sequence

𝑓Attention

Mechanism 𝑐2

𝛼 1 𝛼 2 𝛼 3 𝛼 𝑛𝑥

Page 29: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

29

• In 𝑓, Softmax is used to generate the weights among the hidden states of the input sequences

Attention Mechanism

𝑥 1 𝑥 2 𝑥 3 … 𝑥 𝑛𝑥

ℎ 1 ℎ 2 ℎ 3 … ℎ 𝑛𝑥 𝑔 1 𝑔 2 𝑔 3 … 𝑔 𝑛𝑦

ො𝑦 1 ො𝑦 2 ො𝑦 3 … ො𝑦 𝑛𝑦

𝐶

Decoder RNN

Encoder RNN

Input sequence

Target sequence

𝑓Attention

Mechanism 𝑐2

𝛼 1 𝛼 2 𝛼 3 𝛼 𝑛𝑥

Outline

• RNN

• LSTM

• GRU

• Encoder-Decoder Seq2Seq Model

• Bidirectional RNN

• Software Packages

Page 30: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

30

• In some applications, such as speech recognition or machine translation, dependencies over time not only lie in forward in time but also lie in backward in time

• It assumes all-time step of a sequence is available

Bidirectional RNN

Image: https://distill.pub/2017/ctc/

• To model these, two RNNs are trained together forward RNN and backward RNN

• Each time step’s hidden states from both RNNs are concatenated to form a final output

Bidirectional RNN

forward RNN

backward RNN

Page 31: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

31

• In many cases, a sequence could have (latent) hierarchical structures.

• Example:

• Document ➝ Paragraphs ➝ Sentences ➝ Words ➝ Characters

• Video ➝ Shots ➝ Still frames

Hierarchical RNNVideo as multiple shots

Shot #1

Shot #2

Shot #k

Shot #k+1

A video

• The straightforward approach is to stack hidden states in several layers.

Hierarchical RNN

Page 32: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

32

• One of key research question is to detect where a segment finishes and starts

• E.g., • Boundaries of words (in a sequence of

character)

• Boundaries of scenes (in a sequence of image frames)

• Many works attempted to train models that detect these boundaries

Hierarchical RNN

?

• Video[HSA-RNN: Hierarchical Structure-Adaptive RNN for Video Summarization, Zhao 2018]

• Two Layer-Approach• First layer learns to segment a video into several

shots

• Second layer captures forward & backward dependencies among the boundary frames

Hierarchical RNN

Page 33: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

33

• Text [Hierarchical Multiscale Recurrent Neural Networks, Chung 2016]

• Hidden states at each level are updated based on (learned) structure of a sequence• Higher-level hidden states are only update when a

segment finishes

• Lower-level hidden states uses higher-level hidden states info when a new segment is started

Hierarchical RNN

Outline

• RNN

• LSTM

• GRU

• Tasks with RNN

• Software Packages

Page 34: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

34

• Many recent Deep Learning packages are supporting RNN/LSTM/GRU:

• PyTorch: https://pytorch.org/docs/stable/nn.html#recurrent-layers

• TensorFlow: https://www.tensorflow.org/tutorials/sequences/recurrent

• Caffe2: https://caffe2.ai/docs/RNNs-and-LSTM-networks.html

• Keras: https://keras.io/layers/recurrent/

• Especially I recommend this for beginner: “Sequence classification on PyTorch (character-level name -> Language)”https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html

Software Packages for RNN

• A Critical Review of Recurrent Neural Networks for Sequence Learning https://arxiv.org/pdf/1506.00019.pdf

• The Unreasonable Effectiveness of Recurrent Neural Networks http://karpathy.github.io/2015/05/21/rnn-effectiveness/

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

• LSTM: A Search Space Odyssey https://arxiv.org/pdf/1503.04069.pdf

• [WildML 2015] Recurrent Neural Networks Tutorial, Part 3 –Backpropagation Through Time and Vanishing Gradients http://www.wildml.com/2015/10/recurrent-neural-networks-tutorial-part-3-backpropagation-through-time-and-vanishing-gradients/

• [Green and Perek 2018] : http://www.master-taid.ro/Cursuri/MLAV_files/10_MLAV_En_Recurrent_2018.pdf

References

Page 35: Recurrent neural networks and Long-short term memory (LSTM)people.cs.pitt.edu/~milos/courses/cs3750-Spring... · Recurrent neural networks and Long-short term memory (LSTM) Jeong

3/3/2020

35

Thank you!

Any questions?