Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed
Deep Learning for Computer Vision · Lecture 10 of 16 · 1:13:09
Lecture 10: Recurrent Neural Networks
Study guide
What this lecture covers
After a recap of CNN architectures, this lecture answers a different question: what happens when your input or output is not a fixed-size vector but a variable-length sequence? It introduces recurrent neural networks (RNNs) as a general way to handle sequences, then builds up to language modeling, image captioning with attention, and finally the vanishing and exploding gradient problems that motivate the LSTM architecture.
You'll come away able to describe an RNN's recurrence relation, explain how backpropagation through time works (including its truncated approximation for long sequences), and understand why plain RNNs struggle with long sequences while LSTMs handle them better. The lecture also shows several concrete applications: character-level text generation, image captioning, visual question answering, and models that combine CNNs with RNNs.
Key ideas
- Sequence flexibility: RNNs support one-to-many (image to caption), many-to-one (sequence to classification), and many-to-many setups (sequence to sequence, as in translation), unlike fixed-input feed-forward networks.
- Recurrence relation: at each step an RNN combines the previous hidden state and current input through the same function and weights,
h_t = f_w(h_{t-1}, x_t), optionally producing an outputy_tat each step. - Vanilla RNN: computes the next hidden state as
tanh(W_hh . h_{t-1} + W_xh . x_t), using the same weight matrix at every time step. - Backpropagation through time: gradients for the shared weight matrix are summed across all time steps; for very long sequences this is approximated with truncated backpropagation through time, which processes fixed-length chunks and carries the hidden state forward between chunks.
- Character-level language modeling: an RNN trained to predict the next character in a sequence can, after training, be sampled one character at a time to generate new text resembling its training data.
- Image captioning: a CNN encodes the image into a vector that initializes an RNN's hidden state, which then generates the caption one word at a time until it samples an end token.
- Soft attention: instead of a single image summary vector, the model attends to different spatial locations of the image at each generated word, learned entirely through training rather than being manually specified.
- Vanishing and exploding gradients: repeatedly multiplying by the same weight matrix during backpropagation through a vanilla RNN causes gradients to explode or vanish depending on the matrix's largest singular value; gradient clipping addresses exploding gradients, but vanishing gradients require a different architecture.
- LSTM: maintains a separate cell state alongside the hidden state and uses input, forget, output and "gate" gates with element-wise (not matrix) interactions, giving gradients a more direct path backward through time, similar in spirit to ResNet's shortcut connections.
Walkthrough
Recap: CNN architectures and gradient flow (0:07)
The lecture opens with administrative notes, then reviews the previous lecture on AlexNet, VGG, GoogleNet and ResNet, adding the observation that VGG and GoogleNet needed training hacks because they predate batch normalization, while ResNet's shortcut connections give gradients a more direct backward path (a "gradient super highway") that also foreshadows the LSTM discussion later in this lecture.
Why recurrent networks, and the recurrence relation (8:09)
The lecture motivates RNNs by listing the kinds of variable-length problems they handle: one-to-many (image captioning), many-to-one (sentiment analysis, video classification), and many-to-many (machine translation, per-frame video labeling). It then defines the core recurrence: an RNN cell takes the previous hidden state and current input, applies the same function and weights at every time step, and produces a new hidden state, optionally with an output. The vanilla RNN implements this as tanh(W_hh h_{t-1} + W_xh x_t). Unrolling the computation across time steps clarifies that the same weight matrix is reused everywhere, and during backpropagation each time step contributes a separate gradient for that shared weight matrix, which are summed to form the final gradient.
Character-level language modeling and sampling (20:17)
Using a toy vocabulary of four letters spelling "hello," the lecture shows how a character-level RNN is trained: each character is one-hot encoded, fed in as x_t, and the model predicts a distribution over the next character, incurring softmax loss against the true next character. At test time, the trained model can be seeded with a starting character and made to generate new text by sampling from its predicted distribution at each step and feeding the sampled character back in as the next input. Truncated backpropagation through time is introduced here as the practical way to train on very long sequences (such as all of Wikipedia) without forward- and back-propagating through the entire sequence at once.
Generating text and interpreting hidden units (31:23)
The lecture shows a minimal (roughly 112-line) implementation, then demonstrates trained models sampling Shakespeare-like text, LaTeX resembling an algebraic topology textbook, and C-like Linux kernel source code, all learned purely from predicting the next character. A follow-up analysis looks inside a trained model's hidden state vector and finds individual units that appear to track meaningful structure, such as whether the model is inside a quoted string, how many characters since a line break, or whether it's inside an if statement condition, despite never being told to learn these concepts explicitly.
Image captioning and attention (38:29)
A CNN encodes an image into a summary vector that initializes the hidden state of an RNN language model, which then generates a caption word by word until it samples a special end token; the whole system is trained jointly and end to end on datasets like Microsoft COCO. Example outputs show both plausible captions and clear failure cases where the model mismatches unfamiliar objects to familiar training patterns. The lecture then introduces soft attention, where the CNN produces a grid of vectors (one per spatial location) instead of a single summary vector, and the RNN learns to shift attention across image regions as it generates each word, without being explicitly told where to look. The same encoder-decoder pattern is shown extended to visual question answering, where a question and an image are separately encoded and combined to predict an answer.
Multi-layer RNNs and the vanishing/exploding gradient problem (50:42)
Stacking RNN layers (typically two to four, rarely deeper) is introduced briefly, followed by a detailed derivation of why vanilla RNNs are hard to train over long sequences: backpropagating through each cell multiplies the gradient by (a portion of) the same weight matrix, so across many time steps the gradient is repeatedly multiplied by that matrix. If its largest singular value exceeds one, gradients explode; if it's below one, they vanish. Gradient clipping is presented as a practical, if inelegant, fix for exploding gradients, while vanishing gradients require an architectural change.
LSTM: a gradient super highway (55:43)
The LSTM, dating to 1997, maintains both a hidden state and a separate cell state, and computes four gates (input, forget, output, and a "gate gate," commonly abbreviated ifog) from the stacked previous hidden state and current input. The forget gate controls how much of the previous cell state to keep, the input and gate gates control how much new information to write, and the output gate controls how much of the cell state to expose as the hidden state. During backpropagation, the cell state's gradient path involves only element-wise multiplication by the (per-time-step, sigmoid-bounded) forget gate rather than repeated full matrix multiplication, giving gradients a much more direct path backward, analogous to ResNet's identity shortcuts. The lecture closes by noting related variants (GRU, highway networks) and research showing that many LSTM-like variants perform similarly, suggesting the key ingredient is managing gradient flow through additive and multiplicative gates rather than any single "magic" equation.
Before you watch
- Review the CNN architecture lecture, especially ResNet's residual connections, since the lecture draws a direct comparison to LSTM gradient flow.
- Be comfortable with backpropagation through computational graphs, including how gradients behave at addition and matrix multiplication nodes.
- Familiarity with softmax loss and one-hot encoding is assumed for the language modeling examples.
Check your understanding
- What is the recurrence relation of a vanilla RNN, and what stays the same across every time step?
- Why is truncated backpropagation through time needed for very long sequences, and what is carried forward between truncated batches?
- In the image captioning model, what initializes the RNN's hidden state, and what signals the model to stop generating words?
- Why does repeatedly multiplying by the same weight matrix during backpropagation in a vanilla RNN lead to exploding or vanishing gradients?
- How does the LSTM's forget gate change the gradient path compared to a vanilla RNN, and why does this help with vanishing gradients?
Vocabulary
- recurrent neural network (RNN) (noun)
- A neural network designed to process sequences by reusing the same weights at every step.
RNNs can handle inputs and outputs of varying length. - sequence (noun)
- A series of items or values in a particular order.
A sentence is a sequence of words. - recurrence relation (noun)
- A rule defining each step of a process based on the previous step.
The RNN's recurrence relation updates the hidden state at each step. - hidden state (noun)
- A vector inside a recurrent network that carries information from previous steps.
The hidden state is updated at every time step. - unroll (verb)
- To expand a repeated process into a sequence of separate steps for analysis.
Unrolling the RNN shows the same weights reused at each step. - backpropagation through time (noun)
- A method for training RNNs by unfolding the sequence and applying backpropagation across all time steps.
Backpropagation through time sums gradients across every step. - truncated (adjective)
- Cut short or limited to a smaller portion.
Truncated backpropagation processes the sequence in fixed-length chunks. - one-hot encoding (noun)
- A way to represent a category as a vector with a single 1 and the rest 0s.
Each character is one-hot encoded before being fed into the RNN. - sample (generate) (verb)
- To draw a random output according to a model's predicted probabilities.
The trained model can sample new text one character at a time. - seed (verb)
- To start a process with an initial input or value.
The model is seeded with a starting character before generating text. - encode (verb)
- To convert information into a compact numerical representation.
A CNN encodes the image into a summary vector. - end token (noun)
- A special symbol marking the end of a generated sequence.
The RNN stops generating words when it samples the end token. - soft attention (noun)
- A mechanism that lets a model focus on different parts of the input at each step.
Soft attention lets the model look at different image regions per word. - vanishing gradient (noun)
- A problem where gradients become extremely small as they pass backward through many layers.
Vanishing gradients make it hard for RNNs to learn long-range dependencies. - exploding gradient (noun)
- A problem where gradients grow extremely large during backpropagation.
Exploding gradients can be controlled with gradient clipping. - singular value (noun)
- A number describing how much a matrix scales vectors in a particular direction.
Gradients explode if the matrix's largest singular value exceeds one. - gradient clipping (noun)
- A technique that caps the size of gradients to prevent them from growing too large.
Gradient clipping is a simple fix for exploding gradients. - LSTM (noun)
- A type of recurrent network with gates designed to preserve information over long sequences.
LSTMs handle long sequences better than vanilla RNNs. - cell state (noun)
- An internal memory vector in an LSTM that carries information across time steps.
The cell state gives gradients a more direct backward path. - forget gate (noun)
- A part of an LSTM that controls how much old information to discard.
The forget gate decides how much of the previous cell state to keep. - element-wise (adjective)
- Applied separately to each individual element of a vector or matrix.
LSTM gates use element-wise multiplication instead of full matrix multiplication.
Chapters
- 0:00 Intro
- 0:30 Administrative
- 1:25 Extra Credit: Train Game
- 7:16 Last Time: CNN Architectures
- 8:57 "Vanilla" Neural Network
- 9:19 Recurrent Neural Networks: Process Sequences
- 11:34 Sequential Processing of Non-Sequence Data
- 14:37 (Vanilla) Recurrent Neural Network The state consists of a single "hidden" vector h
- 19:37 Sequence to Sequence: Many-to-one + one-to-many
- 28:35 Truncated Backpropagation through time
- 36:35 Searching for interpretable cells
- 43:17 Image Captioning: Failure Cases
- 50:26 Multilayer RNNS
- 51:29 Vanilla RNN Gradient Flow
From the YouTube description
In Lecture 10 we discuss the use of recurrent neural networks for modeling sequence data. We show how recurrent neural networks can be used for language modeling and image captioning, and how soft spatial attention can be incorporated into image captioning models. We discuss different architectures for recurrent neural networks, including Long Short Term Memory (LSTM) and Gated Recurrent Units (GRU).
Keywords: Recurrent neural networks, RNN, language modeling, image captioning, soft attention, LSTM, GRU
Slides: http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture10.pdf
--------------------------------------------------------------------------------------
Convolutional Neural Networks for Visual Recognition
Instructors:
Fei-Fei Li: http://vision.stanford.edu/feifeili/
Justin Johnson: http://cs.stanford.edu/people/jcjohns/
Serena Yeung: http://ai.stanford.edu/~syyeung/
Computer Vision has become ubiquitous in our society, with applications in search, image understanding, apps, mapping, medicine, drones, and self-driving cars. Core to many of these applications are visual recognition tasks such as image classification, localization and detection. Recent developments in neural network (aka “deep learning”) approaches have greatly advanced the performance of these state-of-the-art visual recognition systems. This lecture collection is a deep dive into details of the deep learning architectures with a focus on learning end-to-end models for these tasks, particularly image classification. From this lecture collection, students will learn to implement, train and debug their own neural networks and gain a detailed understanding of cutting-edge research in computer vision.
Website:
http://cs231n.stanford.edu/
For additional learning opportunities please visit:
http://online.stanford.edu/
← Lecture 9: CNN Architectures · Lecture 11: Detection and Segmentation →
