CV Courseversity

Recurrent and Sequence Neural Networks

Introduces recurrent neural networks, the vanishing and exploding gradient problem in backpropagation through time, and how LSTM and GRU gating mechanisms let networks retain and forget information across long sequences.

“A voice assistant hears "I grew up in France, moved four times as a kid, studied engineering, and still speak fluent French" — to correctly predict the word "French," the network must recall a word spoken thirty tokens earlier while ignoring a stream of irrelevant detail in between. Feedforward networks have no way to carry information across positions in a sequence at all. What kind of architecture lets a network hold onto exactly the right earlier information across long gaps, without that signal decaying to noise or blowing up into instability?”

Modeling Sequences with Recurrent Networks · 15 min

Many of the most important prediction problems in artificial intelligence are not about isolated, fixed-size inputs but about sequences: a sentence is a sequence of words, a stock history is a sequence of prices, a sensor log is a sequence of readings. A feedforward network processes each input independently through a fixed set of weights and has no built-in notion of order or memory — feeding it a sentence requires either truncating or padding it to a fixed length and discarding the fact that word five relates to word two. Recurrent neural networks (RNNs) address this by introducing a hidden state vector that is updated at every time step and passed forward to the next one. At each step t, the network computes a new hidden state h_t as a function of the current input x_t and the previous hidden state h_{t-1}, typically h_t = tanh(W x_t + U h_{t-1} + b), and the same weight matrices W and U are reused at every position in the sequence. This weight sharing across time is what allows an RNN to process sequences of arbitrary length with a fixed number of parameters, and it gives the network, in principle, a mechanism for carrying information forward indefinitely through the recurrent connection.

Training a recurrent network requires an extension of standard backpropagation called backpropagation through time (BPTT). Conceptually, the recurrent network is "unrolled" into a long feedforward computation graph with one copy of the network per time step, each copy sharing the same weights, and ordinary backpropagation is then run on this unrolled graph. Because the same weight matrices appear at every time step, the total gradient with respect to those weights is a sum of contributions from every step, and the gradient flowing backward from a late time step to an early one must pass through a long chain of repeated multiplications by the same Jacobian matrix (and, along the way, repeated multiplications by the derivative of the activation function). When the relevant eigenvalues of that Jacobian are consistently below one in magnitude, the product shrinks geometrically with the number of time steps and the gradient vanishes; when they are consistently above one, the product grows geometrically and the gradient explodes. Both failure modes make it extremely difficult for a plain RNN trained with gradient descent to learn dependencies that span more than a modest number of time steps, even though the recurrent architecture is mathematically capable of representing such dependencies.

The practical consequence is that vanilla RNNs tend to do well on tasks where the relevant context is nearby — predicting the next character given the last few characters, for instance — but degrade sharply as the distance between a cue and the information that depends on it grows. Natural language is full of exactly this kind of long-range structure: subject–verb agreement can span an entire relative clause, and resolving what a pronoun refers to can require remembering a noun introduced many words earlier, as in the assistant example above. Time-series problems have analogous long memory requirements, such as a seasonal pattern that only repeats after many time steps. This mismatch between what plain recurrence can represent in theory and what it can actually learn in practice, given gradient-based training, is the problem that gated recurrent architectures were designed to solve, and it motivates the cell-state and gating mechanisms introduced in the next lesson.

Gated Memory: LSTM and GRU · 18 min

The Long Short-Term Memory (LSTM) architecture was introduced specifically to overcome the vanishing gradient problem in recurrent networks. Its central idea is to give the network a separate memory pathway, the cell state c_t, that is updated mostly by addition rather than by repeated multiplication through a nonlinearity. At each time step, an LSTM cell computes three gate vectors using sigmoid activations — a forget gate f_t, an input gate i_t, and an output gate o_t — along with a candidate update g_t computed with a tanh activation. The cell state is then updated as c_t = f_t ⊙ c_{t-1} + i_t ⊙ g_t, where ⊙ denotes elementwise multiplication: the forget gate decides what fraction of the old memory to keep, and the input gate decides how much of the new candidate content to write in. The hidden state exposed to the rest of the network is then a gated, tanh-squashed view of this cell state, h_t = o_t ⊙ tanh(c_t). Because the forget-gate pathway from c_{t-1} to c_t is a near-linear, elementwise operation rather than a matrix multiplication followed by a saturating nonlinearity, gradients can flow backward along the cell state across many time steps without shrinking geometrically, so long as the forget gate stays open — an architectural property sometimes described as a constant error carousel.

The three gates are themselves small learned networks: each is a sigmoid applied to a linear combination of the current input and the previous hidden state, so the network learns, as part of training, when to remember, when to overwrite, and when to expose its memory to downstream computation. This gives the LSTM a qualitatively different failure mode than the plain RNN: instead of being architecturally forced to blend all past information together with a fixed decay rate, it can learn to hold a specific piece of information almost unchanged across many steps (forget gate near 1, input gate near 0) and then release or overwrite it precisely when needed. This is what allows an LSTM-based language model to carry the gender or number of a subject across an intervening clause, updating its cell state only when new, relevant information actually arrives rather than passively decaying every step. Because the gates are learned jointly with the rest of the network rather than hand-specified, the very same LSTM cell architecture can, depending on the task and the data it is trained on, settle into very different memory strategies — holding some features almost permanently stable while refreshing others at nearly every step — purely as a consequence of the gradients produced by whatever loss the network is trained to minimize.

The Gated Recurrent Unit (GRU) was proposed shortly afterward as part of an encoder–decoder architecture for statistical machine translation, and it simplifies the gating idea. A GRU merges the cell state and hidden state into a single vector and uses only two gates: a reset gate that controls how much of the previous hidden state contributes to computing a new candidate, and an update gate that interpolates between the previous hidden state and that candidate to produce the new hidden state. Where the LSTM maintains a dedicated memory channel, gated separately from the hidden state exposed to the rest of the network, the GRU collapses these into a single vector serving both roles, updated by one interpolation gate rather than by separate forget and input gates acting on separate quantities. With fewer parameters and no separate output gate, GRUs are cheaper to train than LSTMs while achieving comparable performance on many sequence tasks in practice, and the choice between the two architectures is often driven by empirical tuning on the task at hand — dataset size, typical sequence length, and available compute — rather than by any settled theoretical advantage of one over the other.

Sequence-to-Sequence Architectures and Their Limits · 15 min

Many sequence tasks benefit from seeing the entire input before making any prediction, rather than only the input seen so far. A bidirectional RNN addresses this by running two separate recurrent networks over the same sequence — one processing it left to right, the other right to left — and concatenating their hidden states at each position. The resulting representation at position t incorporates context from both before and after t, which is valuable for tasks like part-of-speech tagging or named-entity recognition, where the correct label for a word can depend on words that come later in the sentence — for instance, disambiguating a word that could be either a noun or a verb often requires seeing what follows it, not just what precedes it. Bidirectionality does not solve the vanishing gradient problem by itself; it is typically combined with gated cells (bidirectional LSTMs or GRUs) to get both full-context awareness and long-range memory in the same model, and because both directions can be computed independently before being combined, bidirectional processing adds relatively little extra design complexity on top of the underlying gated cell.

A second major architectural pattern built on recurrent cells is the sequence-to-sequence (seq2seq) encoder–decoder model, introduced for tasks like machine translation where the input and output are both sequences, possibly of different lengths, with no requirement that words align one to one between them. An encoder RNN reads the entire source sequence and compresses it into a single fixed-length vector, usually its final hidden state. A separate decoder RNN is then initialized with this vector and generates the output sequence one token at a time, feeding each generated token back in as input to produce the next one, continuing until it produces a designated end-of-sequence marker. This factorization let a single differentiable model be trained end to end to map an input sequence directly to an output sequence, replacing the hand-engineered, multi-stage pipelines that dominated machine translation before it, and it established the encoder–decoder pattern that later architectures, including attention-based and Transformer models, would build directly on top of.

The fixed-length bottleneck at the heart of the basic seq2seq design is also its main limitation. Whatever the length of the source sequence — five words or fifty — the encoder must squeeze everything relevant into one vector of fixed dimensionality, and empirically translation quality degrades noticeably as source sentences get longer, because early information gets overwritten or diluted by the time the encoder reaches the end of the sequence and produces the vector the decoder will rely on for the entire output. This is precisely the problem that motivated researchers to let the decoder look back at all of the encoder's intermediate hidden states rather than only the final one, weighting each one differently at each decoding step depending on what is currently being generated. That mechanism, attention, removes the fixed-length bottleneck entirely, restores the decoder's access to every part of the source sequence at every step, and is the subject explored in depth in the next module.

Practice

LSTM Gate Arithmetic

c(t-1) = 2.0 previous cell state g(t) = 1.0 candidate value f(t) = 0.8 forget gate i(t) = 0.5 input gate 0.8 × 2.0 = 1.6 0.5 × 1.0 = 0.5 sum 1.6+0.5 c(t) = 1.6 + 0.5 = 2.1

With a previous cell state of 2.0, a forget gate of 0.8, an input gate of 0.5, and a candidate value of 1.0, the LSTM cell-state update c(t) = f(t)·c(t-1) + i(t)·g(t) gives 0.8×2.0 + 0.5×1.0 = 1.6 + 0.5 = 2.1, showing how the forget and input gates jointly, and additively, control how much old versus new information the cell carries forward.

  • Weight sharing across time steps is what lets an RNN handle sequences of any length with a fixed parameter count, but it also forces the same gradient chain to be multiplied over and over during backpropagation through time.
  • LSTM's cell state uses elementwise, mostly-additive updates rather than repeated matrix multiplications through a saturating nonlinearity, which is why gradients can survive over much longer spans than in a plain RNN.
  • GRU is not a strictly better or worse architecture than LSTM — it is a simpler, cheaper design (fewer gates, merged state) that is often competitive in practice and is typically chosen through empirical comparison on the task at hand.

Recall Practice

Vanishing gradientsClick to reveal
Why do gradients vanish or explode in a vanilla RNN trained on long sequences?
Backpropagation through time multiplies the same Jacobian matrix (and activation derivatives) once per time step. If the dominant eigenvalues are below 1 the product shrinks geometrically as the sequence gets longer (vanishing); if above 1 it grows geometrically (exploding). Both make it hard to learn dependencies spanning many time steps.
Cell stateClick to reveal
What equation governs the LSTM cell state update, and what does each gate control?
c(t) = f(t)⊙c(t-1) + i(t)⊙g(t). The forget gate f(t) controls how much of the previous cell state is retained; the input gate i(t) controls how much of the new candidate g(t) is written in. Both are elementwise multiplications, so the update is largely additive rather than passing through a repeated nonlinear transformation.
GRU vs LSTMClick to reveal
What two gates does a GRU use, and what state does it eliminate compared to an LSTM?
A GRU uses a reset gate (controls how much past hidden state feeds into the new candidate) and an update gate (interpolates between the previous hidden state and the new candidate). It eliminates the separate cell state, merging it with the hidden state, and has no distinct output gate.
Seq2seq bottleneckClick to reveal
Why does translation quality in a basic encoder–decoder RNN degrade as source sentences get longer?
The encoder must compress the entire source sequence into one fixed-length vector regardless of its length. As sequences get longer, more information has to be squeezed into that same fixed capacity, so earlier content tends to get overwritten or diluted by the time encoding finishes — the fixed-length bottleneck that attention mechanisms were designed to remove.

Glossary

Recurrent Neural Network (RNN)
A neural network with a hidden state that is updated at every time step using the same shared weights, allowing it to process sequences of arbitrary length.
Backpropagation Through Time (BPTT)
The algorithm for training RNNs by unrolling the recurrent computation into a feedforward graph across time steps and applying standard backpropagation to it.
Vanishing/Exploding Gradient
The phenomenon where gradients backpropagated across many time steps shrink toward zero or grow without bound, due to repeated multiplication by the same Jacobian matrix.
Cell State
The LSTM's internal memory vector, updated mostly through elementwise addition and multiplication rather than repeated nonlinear transformation, which preserves gradient flow across long spans.
Gate (LSTM/GRU)
A sigmoid-activated vector, learned from the current input and previous hidden state, that controls how much of some quantity (old memory, new candidate content, output) passes through.
Sequence-to-Sequence (seq2seq)
An encoder–decoder architecture in which one RNN encodes an input sequence into a representation and a second RNN decodes that representation into an output sequence.
Practical Activity

Trace an LSTM Cell by Hand

A fully simulated, pencil-and-paper style exercise (no real code execution or live data): given toy numeric values for a previous cell state, a forget gate activation, an input gate activation, and a candidate value at three consecutive timesteps, you compute the resulting cell state and hidden state at each step using the LSTM update equations, then explain in your own words which gate was responsible for each change in memory content.

Ready to test yourself?

5 questions on this module.

Start Quiz