recurrent neural network rnn x - ohio...

76
Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017 Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017 18 Recurrent Neural Network x RNN

Upload: others

Post on 01-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201718

Recurrent Neural Network

x

RNN

Page 2: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201719

Recurrent Neural Network

x

RNN

yusually want to predict a vector at some time steps

Page 3: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201720

Recurrent Neural Network

x

RNN

yWe can process a sequence of vectors x by applying a recurrence formula at every time step:

new state old state input vector at some time step

some functionwith parameters W

Page 4: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201721

Recurrent Neural Network

x

RNN

yWe can process a sequence of vectors x by applying a recurrence formula at every time step:

Notice: the same function and the same set of parameters are used at every time step.

Page 5: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201722

(Vanilla) Recurrent Neural Network

x

RNN

y

The state consists of a single “hidden” vector h:

Page 6: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201711

Vanilla Neural Networks

“Vanilla” Neural Network

Page 7: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201712

Recurrent Neural Networks: Process Sequences

e.g. Image Captioningimage -> sequence of words

Page 8: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201713

Recurrent Neural Networks: Process Sequences

e.g. Sentiment Classificationsequence of words -> sentiment

Page 9: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201714

Recurrent Neural Networks: Process Sequences

e.g. Machine Translationseq of words -> seq of words

Page 10: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201715

Recurrent Neural Networks: Process Sequences

e.g. Video classification on frame level

Page 11: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201723

h0 fW h1

x1

RNN: Computational Graph

Page 12: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201724

h0 fW h1 fW h2

x2x1

RNN: Computational Graph

Page 13: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201725

h0 fW h1 fW h2 fW h3

x3

x2x1

RNN: Computational Graph

hT

Page 14: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201726

h0 fW h1 fW h2 fW h3

x3

x2x1W

RNN: Computational Graph

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

hT

Page 15: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201727

h0 fW h1 fW h2 fW h3

x3

yT

x2x1W

RNN: Computational Graph: Many to Many

hT

y3y2y1

Page 16: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201728

h0 fW h1 fW h2 fW h3

x3

yT

x2x1W

RNN: Computational Graph: Many to Many

hT

y3y2y1 L1L2 L3 LT

Page 17: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201729

h0 fW h1 fW h2 fW h3

x3

yT

x2x1W

RNN: Computational Graph: Many to Many

hT

y3y2y1 L1L2 L3 LT

L

Page 18: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201730

h0 fW h1 fW h2 fW h3

x3

y

x2x1W

RNN: Computational Graph: Many to One

hT

Page 19: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201731

h0 fW h1 fW h2 fW h3

yT

xW

RNN: Computational Graph: One to Many

hT

y3y3y3

Page 20: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201732

Sequence to Sequence: Many-to-one + one-to-many

h0

fWh1

fWh2

fWh3

x3

x2

x1

W1

hT

Many to one: Encode input sequence in a single vector

Page 21: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201733

Sequence to Sequence: Many-to-one + one-to-many

h0

fWh1

fWh2

fWh3

x3

x2

x1

W1

hT

y1

y2

Many to one: Encode input sequence in a single vector

One to many: Produce output sequence from single input vector

fWh1

fWh2

fW

W2

Page 22: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201734

Example: Character-levelLanguage Model

Vocabulary:[h,e,l,o]

Example trainingsequence:“hello”

Page 23: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201735

Example: Character-levelLanguage Model

Vocabulary:[h,e,l,o]

Example trainingsequence:“hello”

Page 24: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201736

Example: Character-levelLanguage Model

Vocabulary:[h,e,l,o]

Example trainingsequence:“hello”

Page 25: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201737

Example: Character-levelLanguage ModelSampling

Vocabulary:[h,e,l,o]

At test-time sample characters one at a time, feed back to model

.03

.13

.00

.84

.25

.20

.05

.50

.11

.17

.68

.03

.11

.02

.08

.79Softmax

“e” “l” “l” “o”Sample

Page 26: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201738

.03

.13

.00

.84

.25

.20

.05

.50

.11

.17

.68

.03

.11

.02

.08

.79Softmax

“e” “l” “l” “o”SampleExample:

Character-levelLanguage ModelSampling

Vocabulary:[h,e,l,o]

At test-time sample characters one at a time, feed back to model

Page 27: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201739

.03

.13

.00

.84

.25

.20

.05

.50

.11

.17

.68

.03

.11

.02

.08

.79Softmax

“e” “l” “l” “o”SampleExample:

Character-levelLanguage ModelSampling

Vocabulary:[h,e,l,o]

At test-time sample characters one at a time, feed back to model

Page 28: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201740

.03

.13

.00

.84

.25

.20

.05

.50

.11

.17

.68

.03

.11

.02

.08

.79Softmax

“e” “l” “l” “o”SampleExample:

Character-levelLanguage ModelSampling

Vocabulary:[h,e,l,o]

At test-time sample characters one at a time, feed back to model

Page 29: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201741

Backpropagation through timeLoss

Forward through entire sequence to compute loss, then backward through entire sequence to compute gradient

Page 30: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201742

Truncated Backpropagation through timeLoss

Run forward and backward through chunks of the sequence instead of whole sequence

Page 31: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201743

Truncated Backpropagation through timeLoss

Carry hidden states forward in time forever, but only backpropagate for some smaller number of steps

Page 32: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201744

Truncated Backpropagation through timeLoss

Page 33: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201745

min-char-rnn.py gist: 112 lines of Python

(https://gist.github.com/karpathy/d4dee566867f8291f086)

Page 34: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201746

x

RNN

y

Page 35: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201747

train more

train more

train more

at first:

Page 36: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201748

Page 37: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201763

Explain Images with Multimodal Recurrent Neural Networks, Mao et al.Deep Visual-Semantic Alignments for Generating Image Descriptions, Karpathy and Fei-FeiShow 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

Image Captioning

Figure from Karpathy et a, “Deep Visual-Semantic Alignments for Generating Image Descriptions”, CVPR 2015; figure copyright IEEE, 2015.Reproduced for educational purposes.

Page 38: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201764

Convolutional Neural Network

Recurrent Neural Network

Page 40: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

test image

Page 41: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

test image

X

Page 42: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

test image

x0<START>

<START>

Page 43: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

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

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

v

Wih

Page 44: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

straw

sample!

Page 45: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

straw

h1

y1

Page 46: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

straw

h1

y1

hat

sample!

Page 47: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

straw

h1

y1

hat

h2

y2

Page 48: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

h0

x0<START>

y0

<START>

test image

straw

h1

y1

hat

h2

y2

sample<END> token=> finish.

Page 49: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201775

A cat sitting on a suitcase on the floor

A cat is sitting on a tree branch

A dog is running in the grass with a frisbee

A white teddy bear sitting in the grass

Two people walking on the beach with surfboards

Two giraffes standing in a grassy field

A man riding a dirt bike on a dirt track

Image Captioning: Example Results

A tennis player in action on the court

Captions generated using neuraltalk2All images are CC0 Public domain: cat suitcase, cat tree, dog, bear, surfers, tennis, giraffe, motorcycle

Page 50: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201776

Image Captioning: Failure Cases

A woman is holding a cat in her hand

A woman standing on a beach holding a surfboard

A person holding a computer mouse on a desk

A bird is perched on a tree branch

A man in a baseball uniform throwing a ball

Captions generated using neuraltalk2All images are CC0 Public domain: fur coat, handstand, spider web, baseball

Page 51: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201777

Image Captioning with Attention

Xu et al, “Show, Attend, and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015Figure copyright Kelvin Xu, Jimmy Lei Ba, Jamie Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhutdinov, Richard S. Zemel, and Yoshua Benchio, 2015. Reproduced with permission.

RNN focuses its attention at a different spatial location when generating each word

Page 52: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201778

Image Captioning with Attention

CNN

Image: H x W x 3

Features: L x D

h0

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Page 53: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201779

CNN

Image: H x W x 3

Features: L x D

h0

a1

Distribution over L locations

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 54: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201780

CNN

Image: H x W x 3

Features: L x D

h0

a1

Weighted combination of features

Distribution over L locations

z1Weighted

features: D

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 55: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201781

CNN

Image: H x W x 3

Features: L x D

h0

a1

z1

Weighted combination of features

h1

Distribution over L locations

Weighted features: D y1

First wordXu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 56: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201782

CNN

Image: H x W x 3

Features: L x D

h0

a1

z1

Weighted combination of features

y1

h1

First word

Distribution over L locations

a2 d1

Weighted features: D

Distribution over vocab

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 57: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201783

CNN

Image: H x W x 3

Features: L x D

h0

a1

z1

Weighted combination of features

y1

h1

First word

Distribution over L locations

a2 d1

h2

z2 y2Weighted

features: D

Distribution over vocab

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 58: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201784

CNN

Image: H x W x 3

Features: L x D

h0

a1

z1

Weighted combination of features

y1

h1

First word

Distribution over L locations

a2 d1

h2

a3 d2

z2 y2Weighted

features: D

Distribution over vocab

Xu et al, “Show, Attend and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015

Image Captioning with Attention

Page 59: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201785

Soft attention

Hard attention

Image Captioning with Attention

Xu et al, “Show, Attend, and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015Figure copyright Kelvin Xu, Jimmy Lei Ba, Jamie Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhutdinov, Richard S. Zemel, and Yoshua Benchio, 2015. Reproduced with permission.

Page 60: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201786

Image Captioning with Attention

Xu et al, “Show, Attend, and Tell: Neural Image Caption Generation with Visual Attention”, ICML 2015Figure copyright Kelvin Xu, Jimmy Lei Ba, Jamie Kiros, Kyunghyun Cho, Aaron Courville, Ruslan Salakhutdinov, Richard S. Zemel, and Yoshua Benchio, 2015. Reproduced with permission.

Page 61: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201789

time

depth

Multilayer RNNs

LSTM:

Page 62: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201790

ht-1

xt

W

stack

tanh

ht

Vanilla RNN Gradient Flow Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 63: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201791

ht-1

xt

W

stack

tanh

ht

Vanilla RNN Gradient FlowBackpropagation from ht to ht-1 multiplies by W (actually Whh

T)

Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 64: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201792

Vanilla RNN Gradient Flow

h0 h1 h2 h3 h4

x1 x2 x3 x4

Computing gradient of h0 involves many factors of W(and repeated tanh)

Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 65: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201793

Vanilla RNN Gradient Flow

h0 h1 h2 h3 h4

x1 x2 x3 x4

Largest singular value > 1: Exploding gradients

Largest singular value < 1:Vanishing gradients

Computing gradient of h0 involves many factors of W(and repeated tanh)

Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 66: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201794

Vanilla RNN Gradient Flow

h0 h1 h2 h3 h4

x1 x2 x3 x4

Largest singular value > 1: Exploding gradients

Largest singular value < 1:Vanishing gradients

Gradient clipping: Scale gradient if its norm is too bigComputing gradient

of h0 involves many factors of W(and repeated tanh)

Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 67: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201795

Vanilla RNN Gradient Flow

h0 h1 h2 h3 h4

x1 x2 x3 x4

Computing gradient of h0 involves many factors of W(and repeated tanh)

Largest singular value > 1: Exploding gradients

Largest singular value < 1:Vanishing gradients Change RNN architecture

Bengio et al, “Learning long-term dependencies with gradient descent is difficult”, IEEE Transactions on Neural Networks, 1994Pascanu et al, “On the difficulty of training recurrent neural networks”, ICML 2013

Page 68: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201796

Long Short Term Memory (LSTM)

Hochreiter and Schmidhuber, “Long Short Term Memory”, Neural Computation 1997

Vanilla RNN LSTM

Page 69: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 201797

Long Short Term Memory (LSTM)[Hochreiter et al., 1997]

x

h

vector from before (h)

W

i

f

o

g

vector from below (x)

sigmoid

sigmoid

tanh

sigmoid

4h x 2h 4h 4*h

f: Forget gate, Whether to erase celli: Input gate, whether to write to cellg: Gate gate (?), How much to write to cello: Output gate, How much to reveal cell

Page 70: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017

98

ct-1

ht-1

xt

fig

o

W ☉

+ ct

tanh

☉ ht

Long Short Term Memory (LSTM)[Hochreiter et al., 1997]

stack

Page 71: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017

99

ct-1

ht-1

xt

fig

o

W ☉

+ ct

tanh

☉ ht

Long Short Term Memory (LSTM): Gradient Flow[Hochreiter et al., 1997]

stack

Backpropagation from ct to ct-1 only elementwise multiplication by f, no matrix multiply by W

Page 72: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017100

Long Short Term Memory (LSTM): Gradient Flow[Hochreiter et al., 1997]

c0 c1 c2 c3

Uninterrupted gradient flow!

Page 73: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017101

Long Short Term Memory (LSTM): Gradient Flow[Hochreiter et al., 1997]

c0 c1 c2 c3

Uninterrupted gradient flow!

Input

Softm

ax

3x3 conv, 64

7x7 conv, 64 / 2

FC 1000

Pool

3x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 1283x3 conv, 128 / 2

3x3 conv, 1283x3 conv, 128

3x3 conv, 1283x3 conv, 128

...

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

Pool

Similar to ResNet!

Page 74: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017102

Long Short Term Memory (LSTM): Gradient Flow[Hochreiter et al., 1997]

c0 c1 c2 c3

Uninterrupted gradient flow!

Input

Softm

ax

3x3 conv, 64

7x7 conv, 64 / 2

FC 1000

Pool

3x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 1283x3 conv, 128 / 2

3x3 conv, 1283x3 conv, 128

3x3 conv, 1283x3 conv, 128

...

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

3x3 conv, 643x3 conv, 64

Pool

Similar to ResNet!

In between:Highway Networks

Srivastava et al, “Highway Networks”, ICML DL Workshop 2015

Page 75: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017103

Other RNN Variants

[LSTM: A Search Space Odyssey, Greff et al., 2015]

[An Empirical Exploration of Recurrent Network Architectures, Jozefowicz et al., 2015]

GRU [Learning phrase representations using rnn encoder-decoder for statistical machine translation, Cho et al. 2014]

Page 76: Recurrent Neural Network RNN x - Ohio Universityace.cs.ohio.edu/~razvan/courses/mlds18/08-26-rnn.pdfA white teddy bear sitting in the grass Two people walking on the beach with surfboards

Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017Fei-Fei Li & Justin Johnson & Serena Yeung Lecture 10 - May 4, 2017104

Summary- RNNs allow a lot of flexibility in architecture design- Vanilla RNNs are simple but don’t work very well- Common to use LSTM or GRU: their additive interactions

improve gradient flow- Backward flow of gradients in RNN can explode or vanish.

Exploding is controlled with gradient clipping. Vanishing is controlled with additive interactions (LSTM)

- Better/simpler architectures are a hot topic of current research- Better understanding (both theoretical and empirical) is needed.