Skip to content
AI360XpertBeta
Core ML

Visual explainer

Vanilla RNN

Trace one shared recurrent cell through signed state updates, backward sensitivity, sequence resets and a visible saturation failure.

Counting tokens erases their order.
Counting tokens erases their order.

A recurrent neural network carries a hidden state from one input to the next, allowing order to affect the result. Counting words alone maps both pictured sentences to counts (1, 1, 1), so any model given only those counts must return the same prediction for both. The prediction need not be 0.5; identical inputs are the reason it cannot distinguish their roles.

Carry One State

Each new input changes the carried hidden state.
Each new input changes the carried hidden state.

The dashed line is zero; bars above it are positive and the last bar below it is negative. Inputs enter from below. This is a synthetic one-coordinate RNN, with no claim that its numbers represent word meanings. Starting at hidden state h0=0h_0=0, use inputs x=(1,0,−1)x=(1,0,-1) and shared weights:

ht=tanh⁡(0.5xt+0.8ht−1).h_t=\tanh(0.5x_t+0.8h_{t-1}).

The first state is tanh⁡(0.5)=0.462117\tanh(0.5)=0.462117. Next, tanh⁡(0+0.8×0.462117)=0.353724\tanh(0+0.8\times0.462117)=0.353724. Finally, tanh⁡(−0.5+0.8×0.353724)=−0.213677\tanh(-0.5+0.8\times0.353724)=-0.213677. Heights use 160 drawing units per state unit. Real RNNs use vectors and matrices; the scalar fixture makes every operation checkable. PyTorch's RNN specification gives the general recurrence.

Reuse The Cell

One recurrent cell reuses its weights at every step.
One recurrent cell reuses its weights at every step.

The loop carries the previous time step's state, not an instantaneous circular computation. Input weight 0.5 and recurrent weight 0.8 are reused each time; bias is fixed at zero in this example. A trainable scalar cell would have an input weight, a recurrent weight and a bias. Parameter sharing alone does not guarantee that shifting a sequence preserves outputs, because its starting state and boundaries matter.

Unroll More Steps

A longer sequence adds steps but no new weights.
A longer sequence adds steps but no new weights.

Each small square is another evaluation of that same cell. Six evaluations perform twice as many recurrent updates as three. For fixed input and hidden dimensions, parameter count is independent of sequence length T, while recurrent work grows with T. Streaming inference can keep only the current state; ordinary backpropagation through the whole sequence stores intermediate activations, so its memory generally grows with T. Fixed parameters do not imply constant training memory.

Bound Earlier Influence

Sensitivity is bounded by repeated recurrent multipliers.
Sensitivity is bounded by repeated recurrent multipliers.

The four horizontal bars share a scale of 360 units per unit of sensitivity. They show an upper bound, not measured memory retention. One local derivative is Jt=0.8(1−ht2)J_t=0.8(1-h_t^2); its absolute value is at most 0.8. Across k transitions, the sensitivity magnitude is therefore at most 0.8k0.8^k: 1, 0.64, 0.4096 and 0.0144115 for 0, 2, 4 and 19 hops. Saturated tanh activations shrink it further. Other recurrent weights can yield different behavior, including exploding gradients.

Add A Memory Route

A gated cell adds a separate route for memory.
A gated cell adds a separate route for memory.

The upper hidden-state path crosses tanh every step. The lower LSTM cell path multiplies old memory by a forget gate and adds a gated write; its hidden readout is a separate branch. The diagram isolates that direct memory route, with the readout's gates expanded on the LSTM page. This adds parameters and operations, but can make long dependencies easier to learn. There is no universal ten-step or twenty-step boundary for choosing an architecture.

Follow Sensitivity Backward

Backward sensitivity multiplies the local derivatives.
Backward sensitivity multiplies the local derivatives.

Now the arrows point backward. Seed ∂h3/∂h3=1\partial h_3/\partial h_3=1. For the actual three-input fixture, J3=0.763474J_3=0.763474 and J2=0.699904J_2=0.699904, giving ∂h3/∂h1=J3J2=0.534358\partial h_3/\partial h_1=J_3J_2=0.534358. Bars use 120 drawing units per derivative unit. This is a hidden-state derivative, not a loss or a probability. A loss gradient would also include the output head and loss derivative; shared weights collect contributions from every time step. A small early contribution is not proof that all learning stops.

Reset Independent Sequences

Resetting the state separates independent sequences.
Resetting the state separates independent sequences.

Both cases receive a new input of zero. Carrying the first fixture state, 0.462117, produces 0.353724; resetting the preceding state to zero produces exactly zero, shown by a dot. Both gauges use 300 units per state unit. Preserve state when streams belong together; reset or manage it explicitly when examples are independent. Otherwise one example can affect another's prediction.

Read Out A Prediction

A separate output head turns state into probabilities.
A separate output head turns state into probabilities.

The final hidden state can be negative; a probability cannot. Define a separate two-class head with logits (h3,−h3)(h_3,-h_3). Softmax gives p(A)=eh3/(eh3+e−h3)=0.394758p(A)=e^{h_3}/(e^{h_3}+e^{-h_3})=0.394758 and p(B)=0.605242p(B)=0.605242. The chart has a shared zero-to-one scale, 200 units high. These class names are illustrative; no model was trained. Reversing the inputs to (−1, 0, 1) gives final state +0.213677 and swaps the two probabilities, a held-out order check.

Where It Breaks

A saturated tanh can erase differences between large inputs.
A saturated tanh can erase differences between large inputs.

Change only the input: from a zero previous state, compare 10 with 12. The left bars use 12 units per input unit. The right bars share 144 units per state unit: tanh⁡(5)=0.9999092\tanh(5)=0.9999092 and tanh⁡(6)=0.9999877\tanh(6)=0.9999877. Their tops differ by only 0.0113 drawing units, so the distinction is visibly lost at this scale. The dashed line marks state 1; neither output reaches it exactly. Amber and the approximation sign identify the failure.

The state difference is about 0.0000785 despite a two-unit input difference. Local input derivatives are 0.5(1−h2)0.5(1-h^2), about 0.0000908 and 0.0000123. Saturation can suppress both information differences and useful gradients. Input scaling and initialization matter, and recurrence alone guarantees neither faithful memory nor easy optimization.

The Quick Version

  • Counts erase order; recurrence can preserve order-dependent differences.
  • New input and previous state jointly determine the next state.
  • Shared parameters stay fixed as sequence length grows.
  • Training work and stored activations can grow with length.
  • Sensitivity bounds differ from observed memory retention.
  • Gated memory adds a direct cell route.
  • Backward derivatives multiply across time.
  • Reset state between independent examples.
  • An output head converts hidden state into predictions.
  • Saturation can collapse distinct inputs into nearly identical states.