This Machine Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.
Machine Learning
LSTM and GRU Gates
Read LSTM and GRU equations as learned memory routes: carry old state, write a candidate, reset irrelevant context, and expose the result when the sequence asks for it.
Concept Structure
LSTM and GRU Gates
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learner Contract
What this page should let you do.
2 prerequisites listed; refresh them before leaning on the math or code.
Explain the mechanism, trace the main notation, and test one prediction in the live demo.
Read the intuition before the notation; the math should name a mechanism you already felt.
Follow this edge after making one prediction here; the next page should reuse the result, not restart the route.
Claim/source review status
Substantive review recorded
1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
A plain RNN tries to remember by repeatedly replacing one hidden state with the next. That is why the previous page cared so much about repeated Jacobian products: long-range credit assignment has to survive many recurrent transitions.
LSTM and GRU units change the question. Instead of asking "what should the next hidden state be?" they ask smaller routing questions at every time step:
- Should the old state keep flowing?
- Should this new candidate be written?
- Should old context be reset before making the candidate?
- Should the memory be visible to downstream layers right now?
The gates are not little symbolic if-statements. They are learned sigmoid vectors with values between and . A coordinate near opens a route. A coordinate near mostly closes it. Different coordinates can make different decisions at the same time.
The important mental picture is a memory highway with controlled side doors. In an LSTM, the cell state has an additive update:
One route carries old memory; another writes new candidate memory. The output gate then decides how much of the internal cell becomes the exposed hidden state.
In a GRU, the hidden state is also the memory, and the update gate interpolates between old state and candidate:
When is near , the old state mostly persists. When is near , the candidate mostly replaces it. The reset gate shapes the candidate by deciding how much old state the candidate computation can see.
So gates are not magic memory. They are learned routes that make useful memory easier to represent and train. A gate can keep the wrong thing, overwrite the right thing, or expose memory at the wrong time. The architecture gives the optimizer better handles; the task and training still decide what the handles learn.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
Let be the input at time . Let be the sigmoid function and let denote elementwise multiplication.
For an LSTM, the hidden state is and the internal cell state is . The gates are
The candidate cell write is
The cell and hidden updates are
Read those equations as three route decisions:
- is the carry route for old cell memory.
- is the write route for the candidate.
- is the exposure route from internal memory to hidden state.
If you temporarily hold the gates and candidate fixed while looking at the direct cell-state path, then one coordinate obeys
Across many steps, that direct path contributes a product of forget gates. If the relevant values stay near , the cell gives information and gradients a much better corridor than repeated full hidden-state replacement. If the forget gates close or the input gates keep writing distractors, the memory can still be lost.
For a GRU, there is no separate cell state. The hidden state is the memory seen by the next recurrent step. Using the convention from D2L, the reset and update gates are
The candidate is
and the final hidden update is
Here decides how much old state the candidate can consult. The update gate decides how much old state carries directly into . Some papers and libraries use the opposite interpolation convention, so always check whether "update" means "keep old" or "write new" in the source you are reading.
The mathematical bridge from RNN gradient pathologies is now visible:
That bridge is why LSTMs and GRUs were such important pre-transformer sequence models.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
tokens = ["KEY", "noise", "pause", "DISTRACTOR", "QUERY"]
candidates = np.array([0.9, 0.05, -0.05, -0.9, 0.0])
presets = {
"keep": (
[0.00, 0.96, 0.96, 0.97, 0.98], # forget/carry
[0.95, 0.04, 0.05, 0.03, 0.02], # input/write
[0.08, 0.08, 0.10, 0.12, 0.28], # output/expose
),
"overwrite": (
[0.00, 0.92, 0.90, 0.16, 0.90],
[0.95, 0.08, 0.08, 0.88, 0.12],
[0.08, 0.08, 0.10, 0.18, 0.35],
),
"expose": (
[0.00, 0.96, 0.97, 0.97, 0.98],
[0.95, 0.04, 0.03, 0.03, 0.02],
[0.08, 0.08, 0.10, 0.18, 0.92],
),
}
def run_lstm_route(name):
c = 0.0
rows = []
f_gate, i_gate, o_gate = presets[name]
for token, cand, f, i, o in zip(tokens, candidates, f_gate, i_gate, o_gate):
c = f * c + i * cand
h = o * np.tanh(c)
rows.append((token, round(f, 2), round(i, 2), round(o, 2), round(c, 3), round(h, 3)))
return rows
for name in presets:
print("\n", name)
for row in run_lstm_route(name):
print(row)
The numbers are hand-set to isolate the mechanism. A trained LSTM learns the gates from data, and each gate is a vector, not a scalar. Still, the code makes the central object impossible to miss: the final cell is a sum of a carried old state and gated candidate writes.
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the Gate Routing Lab to turn the equations into a hidden-state routing prediction.
Before reveal, choose an LSTM or GRU, inspect the token sequence, and commit to one outcome:
- Kept: the target memory survives the distractor.
- Overwritten: the distractor replaces the target memory.
- Exposed: the target memory becomes visible to the readout at the query step.
After reveal, inspect the gate values, state trace, route summary, and caveat strip. The lab is a finite deterministic witness, not a proof that LSTMs or GRUs always solve long-range dependency tasks. Real gates are learned, vector-valued, input-dependent, and trained through the same loss surface as the rest of the network.
Live Concept Demo
Explore LSTM and GRU Gates
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what LSTM and GRU Gates should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
Read LSTM and GRU equations as learned memory routes: carry old state, write a candidate, reset irrelevant context, and expose the result when the sequence asks for it.
Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change LSTM and GRU Gates should make visible.
Visual Inquiry
Make the image answer a mathematical question
Read LSTM and GRU equations as learned memory routes: carry old state, write a candidate, reset irrelevant context, and expose the result when the sequence asks for it.
Which visible object should carry the first intuition?
Pick the cue that should make LSTM and GRU Gates easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Defines LSTM input, forget, output gates, candidate cell, cell-state update, hidden-state exposure, and the constant-memory special case.
Open sourceDefines GRU reset/update gates, candidate hidden state, and update-gate interpolation between old state and candidate.
Open sourceCourse source for GRU/LSTM equations and intuition about persistent memory, new memory, reset, update, forget, input, and exposure gates.
Open sourcePrimary source for LSTM memory cells, constant error flow, and multiplicative input/output gates controlling access to cell contents.
Open sourcePrimary source for the GRU-style reset and update gates, hidden-state interpolation, and adaptive memory timescales.
Open sourceClaim Review
Read LSTM and GRU equations as learned memory routes: carry old state, write a candidate, reset irrelevant context, and expose the result when the sequence asks for it.
Claims without a substantive review badge still need exact source-support review.
d2l-2026-lstm, d2l-2026-gru, cs224n-2019-rnn-gru-lstm-notes, hochreiter-schmidhuber-1997-lstm, cho-2014-rnn-encoder-decoder
Use equations, runnable code, and demos to check whether the source support is operational.
D2L supports the LSTM gate/cell equations, the GRU reset/update equations, and the keep/write/expose interpretations; CS224N supports the same course-level equations and gate intuitions; Hochreiter-Schmidhuber ground the LSTM memory-cell and constant-error-flow motivation; Cho et al. ground the GRU reset/update gates and adaptive remember/forget behavior.
Sources: Dive into Deep Learning: Long Short-Term Memory, Dive into Deep Learning: Gated Recurrent Units, CS224N: Language Models, RNN, GRU and LSTM, Long Short-Term Memory, Learning Phrase Representations using RNN Encoder-Decoder for Statistical Machine TranslationFinite deterministic gate-routing witness only; real gates are vector-valued, learned from data, coupled through nonlinear candidates, and affected by training details.A bounded review summary is present; still check caveats and exact reference scope.Checked D2L LSTM/GRU chapters, Stanford CS224N RNN notes, Hochreiter-Schmidhuber 1997, and Cho et al. 2014 for gate equations, controlled memory routing, constant/direct carry paths, update/reset behavior, and long-range-memory caveats. The demo is a finite scalar/vector routing witness, not a trained architecture benchmark. GPT Pro/Oracle publication critique remains pending because 127.0.0.1:51672 refused connection.
Reviewer: codex-local-primary-source-audit; reviewed 2026-07-02Source support candidates
book 2026Dive into Deep Learning: Long Short-Term MemoryDefines LSTM input, forget, output gates, candidate cell, cell-state update, hidden-state exposure, and the constant-memory special case.
book 2026Dive into Deep Learning: Gated Recurrent UnitsDefines GRU reset/update gates, candidate hidden state, and update-gate interpolation between old state and candidate.
course-notes 2019CS224N: Language Models, RNN, GRU and LSTMCourse source for GRU/LSTM equations and intuition about persistent memory, new memory, reset, update, forget, input, and exposure gates.
paper 1997Long Short-Term MemoryPrimary source for LSTM memory cells, constant error flow, and multiplicative input/output gates controlling access to cell contents.
Practice Loop
Try the idea before it explains itself
Read LSTM and GRU equations as learned memory routes: carry old state, write a candidate, reset irrelevant context, and expose the result when the sequence asks for it.
Before touching the demo, predict one visible change that should happen in LSTM and GRU Gates.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to a claim, equation, code, or demo
Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.Open the draft below to save one note and next action in this browser.
LSTM and GRU Gates
What is the smallest example that makes LSTM and GRU Gates click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays in this browser, attached to the selected learning item.
- References to inspect: attached references on this page.
- Definition, prerequisite, and contrast concept links
- The equation or runnable code that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - LSTM and GRU Gates Selected item key: recorded for copy. Context: Machine Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes LSTM and GRU Gates click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/machine-learning/lstm-gru-gates
concept:machine-learning/lstm-gru-gates