How Neural Networks Understand Text, Part 2: LSTM and GRU

How LSTMs use a cell state and gates to control information flow, and how GRU simplifies recurrent networks.

In this article

Imagine reading a movie review that says, "The movie is beautiful, despite its slow beginning." To understand the praise, you need to connect "beautiful" with the qualification that follows it. A model reading the sentence one word at a time has to carry useful context forward as each new word arrives.

In Part 1, we saw how a recurrent neural network (RNN) processes text this way, moving through a sequence of timesteps. But standard RNNs can struggle to preserve context across long sequences. During training, vanishing gradients can make distant relationships hard to learn, while exploding gradients can make optimization unstable (Pascanu et al., 2013).

Long short-term memory networks (LSTMs) were designed to help recurrent networks learn long-range dependencies by providing a cell state that can carry information and error signals across many timesteps. Their gates regulate what is retained and updated. This helps with vanishing gradients, but does not make exploding gradients impossible. Hochreiter and Schmidhuber's original LSTM paper.

Rather than maintaining a single state, an LSTM carries two states through the sequence:

  • Cell state: carries longer-term information and is updated at each timestep.
  • Hidden state: represents the current output and is passed to the next timestep.

The gates in an LSTM regulate how information enters, remains in, and leaves the cell state.

LSTM Architecture

Anatomy of an LSTM Cell

An LSTM cell uses three gates:

  1. Forget gate
  2. Input gate
  3. Output gate

The equations below use xtx_t for the input at the current timestep, ht−1h_{t-1} for the previous hidden state, and Ct−1C_{t-1} for the previous cell state. The symbol ⊙\odot denotes element-wise multiplication.

Forget Gate

Diagram highlighting the forget gate in an LSTM cell

The forget gate decides which parts of the previous cell state to retain. It combines the current input with the previous hidden state, then applies a sigmoid function to produce values between 0 and 1. Values near 0 suppress information, while values near 1 preserve it.

LaTeX equation
ft=σ(Wf[ht−1,xt]+bf)f_t = \sigma(W_f [h_{t-1}, x_t] + b_f)
LaTeX equation
Ctretained=ft⊙Ct−1C_t^{\mathrm{retained}} = f_t \odot C_{t-1}

The gate does not remove information by itself. Instead, its output scales each component of the previous cell state. Components multiplied by values near zero contribute less to the updated state.

Input Gate

Diagram highlighting the input gate in an LSTM cell

The input gate controls which new information is added to the cell state. First, the cell creates a candidate state. Then the input gate determines how much of each candidate value to use:

LaTeX equation
C~t=tanh⁡(WC[ht−1,xt]+bC)\tilde{C}_t = \tanh(W_C [h_{t-1}, x_t] + b_C)
LaTeX equation
it=σ(Wi[ht−1,xt]+bi)i_t = \sigma(W_i [h_{t-1}, x_t] + b_i)
LaTeX equation
Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t

Here, C~t\tilde{C}_t is the candidate cell state and iti_t is the input gate output. The updated cell state combines retained information with the selected candidate information.

Output Gate

Diagram highlighting the output gate in an LSTM cell

The output gate determines which parts of the updated cell state become the hidden state for the current timestep:

LaTeX equation
ot=σ(Wo[ht−1,xt]+bo)o_t = \sigma(W_o [h_{t-1}, x_t] + b_o)
LaTeX equation
ht=ot⊙tanh⁡(Ct)h_t = o_t \odot \tanh(C_t)

The hidden state hth_t is the cell's output. It is passed forward to the next timestep and can also be used by later layers or a prediction head.

Gated Recurrent Units

LSTMs can model long-range relationships, but their multiple gates and two state vectors add parameters and computation. A gated recurrent unit (GRU) is a related architecture that uses a simpler design. In experiments on music and speech sequence modeling, GRUs performed comparably to LSTMs. Results can differ across datasets and model configurations (Chung et al., 2014).

Unlike an LSTM, a GRU maintains one hidden state. Its two gates are the reset gate and the update gate. The diagram below shows the basic GRU architecture.

Diagram showing the basic structure of a gated recurrent unit (GRU) cell

Reset Gate

The reset gate controls how much of the previous hidden state contributes when forming a candidate state:

LaTeX equation
rt=σ(Wr[ht−1,xt]+br)r_t = \sigma(W_r [h_{t-1}, x_t] + b_r)

Update Gate and Candidate State

The candidate hidden state uses the reset gate to filter the previous hidden state:

LaTeX equation
h~t=tanh⁡(Wh[rt⊙ht−1,xt]+bh)\tilde{h}_t = \tanh(W_h [r_t \odot h_{t-1}, x_t] + b_h)
LaTeX equation
zt=σ(Wz[ht−1,xt]+bz)z_t = \sigma(W_z [h_{t-1}, x_t] + b_z)
LaTeX equation
ht=(1−zt)⊙ht−1+zt⊙h~th_t = (1-z_t) \odot h_{t-1} + z_t \odot \tilde{h}_t

The update gate ztz_t balances the previous hidden state with the candidate state. This gives the GRU a way to retain useful context while incorporating new information.

References