DEV Community

Cover image for LSTM: The Gates That Taught Networks to Remember
Muhammad Saud
Muhammad Saud

Posted on Originally published at logicstack.org

LSTM: The Gates That Taught Networks to Remember

In Part 4, the RNN learned to remember, but its memory faded fast. In 1997, Hochreiter and Schmidhuber fixed that with a cell state and three gates. We show how the LSTM holds a memory for 1,000 steps.

1. A memory that does not fade

A plain RNN rewrites its hidden state at every step, so the signal shrinks from 1.00 to 0.03 in three steps. The LSTM adds a second line, the cell state, that runs straight through and only changes when a gate acts.

2. Three gates guard the memory

Each gate reads the new input and the last hidden state, then outputs a number from 0 to 1. The forget gate erases or keeps, the input gate writes or skips, and the output gate shares or hides. Together they decide what the cell remembers.

3. Remembering “keys” across the gap

Read “The keys to the cabinet are” and the verb must agree with “keys”, five words back. The input gate stores that “keys” is plural, the forget gate keeps it through “to the cabinet”, and the output gate releases it in time to predict “are”.

4. From a paper to the backbone of AI

The 1997 paper showed LSTM could bridge gaps of more than 1,000 steps. Gers added the forget gate in 2000, and by 2014 LSTM powered sequence-to-sequence translation. The Transformer took over in 2017, but LSTM still runs on phones and small devices.

Key takeaway

The LSTM gave the RNN a cell state that nothing rewrites by accident, plus three gates that learn what to keep, what to write, and what to share. That is why it could remember across 1,000 steps, and why it ran speech and translation for twenty years.

What is Next in This Series

Next is Part 6, Seq2Seq and Attention, Visualized, where the LSTM reads a whole sentence first and then writes the translation. To see the problem the LSTM solved, read Part 4, RNNs: The Network That Learned to Remember. For how these ideas grew into modern LLMs, see How LLMs Actually Work.


Originally published on LogicStack. Run the code examples right in your browser with our free interactive Python and SQL playgrounds — no setup, no sign-up.

Top comments (0)