3.5.5 · D2Sequence Models

Visual walkthrough — Gated Recurrent Units (GRU)

1,903 words9 min readBack to topic

Step 1 — The problem: a number that must survive

WHAT. Imagine memory as a single number (think: "how positive is this sentence so far", a value like ). At every word we get a chance to change it. We call the value before the current word and the value after . The little subscript just counts words: is the first word, the second, and so on.

WHY. A plain recurrent network (3.5.01-Basic-RNN-Architecture) rewrites completely at every word. If is multiplied by a small number 30 times in a row, it shrinks toward zero — the famous vanishing gradient problem. Our whole goal is to give a way to pass through unchanged when nothing important happens.

PICTURE. The top track shows a plain RNN: every word squashes the memory a little, so "Alice" fades to nothing by the end. The bottom track is the goal — a lane where the memory can coast untouched.

Figure — Gated Recurrent Units (GRU)

Step 2 — The dial we need: the sigmoid

WHAT. To control how much of anything flows, we need a knob that only ever gives a value between fully-closed () and fully-open (). That knob is the sigmoid, written :

Read it term by term: is any real number the network computes (it can be huge negative or huge positive). is the exponential — a positive number that is tiny when is large and enormous when is very negative. Dividing by " plus that" squeezes everything into the open interval between and .

WHY this tool and not another? We want a soft gate, not a hard on/off switch. A hard switch (a step function) has zero slope everywhere, so learning by gradients is impossible — the network could never tell which direction to nudge. The sigmoid is the smooth S-shaped stand-in: it can be nearly or nearly , yet it always has a nonzero slope to learn from.

PICTURE. The S-curve: far left it hugs (gate shut), far right it hugs (gate open), and it passes through at the middle.

Figure — Gated Recurrent Units (GRU)

Step 3 — Feeding the gate: concatenate, then weight

WHAT. What number goes into the sigmoid? We stack the old memory and the new word into one long list and take a weighted sum:

  • ::: concatenation — literally glue the two lists end to end into one longer list.
  • ::: a matrix of weights the network learns; it mixes every input number to produce the gate's raw score.
  • ::: a bias, a learned constant that shifts the score up or down.
  • ::: squashes that score into a tap opening. The subscript marks this as the reset gate.

WHY. The gate must react to both what we already remember () and what just arrived (). Gluing them together and running one weighted sum lets the network base its decision on the full situation.

PICTURE. The two boxes slide together into one bar, the weight matrix rakes across it, the bias nudges, and the sigmoid tap comes out.

Figure — Gated Recurrent Units (GRU)

Step 4 — The reset gate: should the past even count?

WHAT. The reset gate (built in Step 3) decides how much of the old memory to consult when drafting a new idea. We apply it with the element-wise product :

means: multiply matching entries. If and , then . Each memory slot has its own tap.

WHY. Sometimes the past is misleading. In "The movie started well but…", the word "but" flips the meaning. Here the network learns , so the positive history is dialled way down before we compute anything new — we "reset" so the stale positivity does not poison the fresh guess.

PICTURE. Two cases side by side. When the past passes through untouched; when the past is squeezed to nearly nothing.

Figure — Gated Recurrent Units (GRU)

Step 5 — The candidate: a fresh proposal via

WHAT. Using the reset-muted past, we draft a proposed new memory, the candidate (the tilde just means "candidate / not final yet"):

  • ::: the muted past from Step 4.
  • glued to , raked by weights , shifted by bias ::: same recipe as Step 3.
  • ::: squashes the result into the range to .

WHY and not here? A gate must be (a tap opening can't be negative). But a memory value should be able to be positive or negative — "very positive sentiment" vs. "very negative sentiment." is the sibling of sigmoid that is centred on zero, running from to , so it can express both directions.

PICTURE. The sigmoid and tanh curves overlaid: same S-shape, but tanh straddles zero so the candidate can swing negative.

Figure — Gated Recurrent Units (GRU)

Step 6 — The update gate mixes old and new

WHAT. We compute a second tap, the update gate (same recipe as Step 3, its own weights ):

Then we blend the old memory and the fresh candidate with it:

  • ::: the fraction of the old memory we keep.
  • ::: the leftover fraction goes to the new candidate.
  • The two fractions always add to , so this is a weighted average — nothing is created or destroyed, only shared.

WHY. One knob, two coupled decisions. In an LSTM a forget gate and a separate input gate do this in two moves. GRU notices they are two sides of one coin: keep more old ⇒ accept less new. So it uses a single and its complement . That is the "merged gate" that makes GRU lighter.

PICTURE. A slider: at one end (pure keep), at the other (pure replace), everywhere in between a proportional mix.

Figure — Gated Recurrent Units (GRU)

Step 7 — The edge cases (all four corners)

WHAT. Two gates, each near or , give four corners. We must be sure every corner behaves.

Result Meaning
anything coast — memory passes through untouched
using full past rewrite, but informed by history
hard reset — start fresh from the word alone
candidate is ignored anyway, so doesn't matter

WHY it matters. Row 1 is the gradient highway: when the slope , so the gradient neither shrinks nor explodes across many words — that is precisely how GRU beats the vanishing-gradient wall. Row 3 shows a clean topic change. Row 4 shows why is harmless when we're keeping the old value: its work is discarded, no conflict.

PICTURE. The unit square of with each corner labelled by what the memory does there.

Figure — Gated Recurrent Units (GRU)
Recall Check the corners

If , what is ? ::: Exactly — the memory coasts, gradient . If and , what drives ? ::: Only the current word ; the past is fully muted — a fresh start. Why is irrelevant when ? ::: The candidate is weighted by , so whatever did is thrown away.


The one-picture summary

Everything at once: the word and old memory come in, feed two sigmoid taps (, ), the reset tap mutes the past for the tanh candidate , and the update tap slides between old and new to produce . Follow the plum "highway" line to see the gradient's coasting path.

Figure — Gated Recurrent Units (GRU)
Recall Feynman retelling (say it out loud)

Memory is a set of numbers I want to protect on a long trip. At each word I ask two soft yes/no questions using the sigmoid tap. First question (reset): "should I trust what I already remember while I brainstorm?" — if not, I mute the old memory before drafting. I draft a proposal with tanh so it can be positive or negative. Second question (update): "how much of the old should I keep versus this proposal?" — I take a weighted average, where the two weights and always sum to one. When the update tap is fully open on "keep," the memory sails through unchanged and the gradient rides along without shrinking — that's the trick that lets a name like "Alice" survive to the end of a paragraph. LSTM does this with three taps and a spare tank; GRU realises two of the taps were really one, and does the whole thing with two.

Recall Where this leads

The same "let information coast, decide what to attend to" idea reappears, generalised, in attention and is stacked in both directions in bidirectional RNNs. Prefer to read this in Hindi/English mix? See 3.5.05 Gated Recurrent Units (GRU) (Hinglish).