Natural Language Processing · Advanced level

Advanced level · Module 3: Long Short-Term Memory Networks for Text · Lesson 1 of 3

How LSTMs Remember

Learners examine how the forget, input and output gates control a memory cell, and how this lets gradients flow across longer texts than a plain RNN can handle.

In this lesson you will

  • Explain the LSTM gates and cell state
  • Compare LSTM and GRU

Key concepts

cell stateforget gateinput gateoutput gateGRU

The lesson

An LSTM keeps a cell state alongside the hidden state. The forget gate decides what to erase, the input gate what to write and the output gate what to reveal. Because the cell state is updated by addition rather than repeated multiplication, gradients survive across many more steps.

A GRU is a simpler variant with two gates that often performs similarly with fewer parameters. Both are available in PyTorch as nn.LSTM and nn.GRU with the same interface as nn.RNN.

Repeat the bracket-closing test from the previous module with an LSTM to see the difference in practice.

Step by step

  1. Draw an LSTM cell and label each gate.
  2. Explain in words what each gate does for the sentence 'The fees, which were due in March, are still not paid'.
  3. Swap nn.RNN for nn.LSTM in the language model and retrain.
  4. Repeat the long-range test and compare.

Practice activity

Explain the LSTM cell with a diagram and show its improvement over the RNN on the long-range test.

Expected result: A correct diagram and an experiment showing better long-range memory.

Check your understanding

  1. What does the forget gate do?

    Show answer

    It decides which information to remove from the cell state.

  2. Why do LSTMs suffer less from vanishing gradients?

    Show answer

    The cell state is updated additively, which lets gradients flow over more steps.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.