Gradient LabStart the lesson

Lessons › Chapter 10: Sequences and text

LSTM and GRU

An lstm adds a separate memory “belt” that runs along the whole sequence. Information can ride on it for a long time without being squeezed at every step.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Forgetting the start

In long messages, the early design forgets what the customer said in the first sentence.

Concept

A memory belt

An lstm adds a separate memory “belt” that runs along the whole sequence. Information can ride on it for a long time without being squeezed at every step.

Build

The forget gate

At each step, a forget gate decides how much of the belt to erase. Finished with a fact (say, the topic changed)? Wipe it.

Build

The input gate

An input gate decides what new information to write onto the belt, such as “the subject is plural”.

Key idea

The output gate, and long memory

An output gate decides what to reveal at this step. Because gates can keep a fact untouched for many steps, an LSTM can remember that “the cats” was plural when it reaches “were”, twenty words later. A GRU does the same job with two gates.

Real life

In real life: speech to text

LSTMs drove big jumps in speech recognition and translation in the mid-2010s, which is when voice typing got good. Transformers have since taken over most of that work, but LSTMs still run in small devices and in forecasting time series like energy demand.

Quick check: What does the forget gate decide?

It scales the memory belt between keep (1) and erase (0) at each step.

Pseudocode

memory belt = carried along every step
forget gate: how much to erase from the belt
input gate:  how much new information to write
output gate: how much to reveal right now

# GRU: a simpler cousin with two gates instead of three

Key terms

lstm
Long short-term memory: a recurrent network with gates that control what to remember and forget.
gate
A learned on/off valve (from 0 to 1) that decides how much information passes.