RNNs and LSTMs: Trace Hidden State Through a Three-Step Sequence

Follow one three-step input sequence through a vanilla RNN and an LSTM. The worked tables show every state update, gradient factor, gate branch, and common tracing trap.

KnowledgeGate Team

Exam prep & CS education

Updated 29 Nov 20266 min read
RNNs and LSTMs: Trace Hidden State Through a Three-Step Sequence

A recurrence is one line of algebra, but tracing it step by step is where you can lose the thread: which value is the current input, which is the carried state, and how much of an early signal survives at the last step. For x=[1,0,2], the vanilla RNN trace ends at h3=0.857277, and the LSTM trace ends at c3=0.680000 and h3=0.532367.

RNN hidden state is a running summary, not a stored copy

A sequence model processes ordered inputs one time step at a time. At step t, a vanilla RNN combines the current input x_t with the previous hidden state h_{t-1}:

a_t = W_x x_t + W_h h_{t-1} + b, then h_t = tanh(a_t).

The hidden state is a transformed running summary, not a verbatim store of every earlier input. When the network is unrolled for t=1,2,3, the three cells reuse the same W_x, W_h, and b. The initial state h0 starts the memory, and order matters because each state depends on the one before it. For neuron, activation, perceptron, and MLP foundations, Artificial Neural Networks: Complete Guide with Worked Examples for GATE and Interviews owns the feed-forward baseline; this trace begins with recurrent state, then follows LSTM gates and through-time gradients.

Vanilla RNN worked example: follow all three state updates

Use x=[1,0,2], h0=0, W_x=0.5, W_h=0.8, and b=0. Thus a_t=0.5x_t+0.8h_{t-1} and h_t=tanh(a_t). This scalar teaching reduction exposes the bookkeeping that vector and matrix RNNs perform component-wise.

  • At t=1, a1=0.5(1)+0.8(0)=0.500000, so h1=tanh(0.5)=0.462117.

  • At t=2, using full-precision h1, a2=0.5(0)+0.8(0.462117157)=0.369694, so h2=0.353724.

  • At t=3, using full-precision h2, a3=0.5(2)+0.8(0.353723789)=1.282979, so h3=0.857277.

t

x_t

h_{t-1}

a_t

h_t

1

1

0.000000

0.500000

0.462117

2

0

0.462117

0.369694

0.353724

3

2

0.353724

1.282979

0.857277

The middle row is revealing. Although x2=0, h2 is not zero because the recurrent term carries information from h1.

An unrolled RNN over three time steps, showing each hidden state feeding into the next as the same weights process the inputs 1, 0, and 2.

Vanishing gradients appear as repeated factors through time

Backpropagation through time sends the effect of an early state through a product of local derivatives. Here, dh_t/dh_{t-1}=W_h(1-h_t^2) because tanh'(a_t)=1-tanh^2(a_t).

From the full-precision trace, the local factors are 0.8(1-h1^2)=0.629158, 0.8(1-h2^2)=0.699904, and 0.8(1-h3^2)=0.212061. Therefore:

dh3/dh0 = 0.629158 × 0.699904 × 0.212061 = 0.093381

For only the two transitions from h1 to h3, the factor is 0.699904 × 0.212061 = 0.148423. Repeated factors below 1 shrink an early signal. Saturated tanh states make 1-h_t^2 especially small. Factors above 1 can instead cause explosion. This is a mechanism, not a rule that every RNN must vanish or that length alone decides the outcome.

LSTM gates create a controlled cell-state path

An LSTM uses a forget gate f_t=sigmoid(...), input gate i_t=sigmoid(...), candidate g_t=tanh(...), and output gate o_t=sigmoid(...). Its states follow:

c_t = f_t c_{t-1} + i_t g_t, and h_t = o_t tanh(c_t).

f_t c_{t-1} keeps a selected share of the old cell state, while i_t g_t writes selected new content. Addition combines the two, and o_t decides how much of the transformed cell is exposed as h_t. In a real LSTM, these gates depend on the current input and previous hidden state. Fixing the gate outputs here separates state bookkeeping from gate prediction.

The additive cell update gives a more direct gradient route when forget gates stay near 1, though it does not guarantee long-range learning. After this trace, browse Skills for practice across the wider model family.

LSTM worked example: trace cell state and hidden state together

Reuse x=[1,0,2] with c0=0 and h0=0. Fixed gate outputs keep the calculation focused on state bookkeeping.

  • At t=1, retain 0.50(0)=0 and write 0.50(0.80)=0.40. Thus c1=0.400000 and h1=0.75tanh(0.40)=0.284962.

  • At t=2, retain 0.75(0.40)=0.30 and write 0.25(0.40)=0.10. Thus c2=0.400000 and h2=0.284962.

  • At t=3, retain 0.50(0.40)=0.20 and write 0.80(0.60)=0.48. Thus c3=0.680000 and h3=0.90tanh(0.68)=0.532367.

t

x_t

f_t

i_t

g_t

o_t

f_t c_{t-1}

i_t g_t

c_t

h_t

1

1

0.50

0.50

0.80

0.75

0.000000

0.400000

0.400000

0.284962

2

0

0.75

0.25

0.40

0.75

0.300000

0.100000

0.400000

0.284962

3

2

0.50

0.80

0.60

0.90

0.200000

0.480000

0.680000

0.532367

Under this fixed-gate local view, the direct cell-state factor is dc3/dc1=f2f3=0.75(0.50)=0.375000. Compare that with dh3/dh1=0.148423 along the RNN recurrent path. This illustrates only the direct cell path; full LSTM gradients contain more than forget-gate products.

A three-step LSTM diagram tracing the cell-state highway, with forget and input gates setting what each step retains and writes.

RNN and LSTM tracing mistakes reveal what each state means

These tracing slips are worth naming because each one reveals what the state actually represents. Once you see why a slip happens, its repair becomes easier to remember.

  • If you reset state at each step, sequence memory disappears. Carry h_t, and c_t for an LSTM, forward.

  • Putting x_t in place of h_{t-1} lets the current input overwrite the carried context, so label both terms before substituting.

  • Fresh weights for every unrolled cell create three separate networks instead of a recurrence with shared parameters. Reuse one parameter set throughout.

  • Swapping the forget and input branches makes old and new content trade roles, producing an incorrect cell update. Retain with f_t and write with i_t.

  • Applying sigmoid to g_t changes the candidate range, so the update no longer matches its definition. Use the defined tanh candidate.

  • Confusing c_t with h_t removes their separate stored and exposed roles. Compute c_t first, then h_t=o_t tanh(c_t).

  • Adding a gate to a state stops the gate from scaling content, so the branch no longer performs gating. Multiply the branch values instead.

  • Rounding h1 before computing h2 carries the error forward and makes later answers drift. Retain full precision until the final display.

An LSTM can still forget. A small forget gate deliberately attenuates the cell path. Its advantage is selective, learnable retention, not perfect permanent memory.

How RNN and LSTM questions test state bookkeeping

Number-based questions hand you recurrence values and ask for a missing h_t or a fixed-step unrolling. They are really checking whether you preserve the carried state, reuse shared weights, and spot a gate or index error. Use Artificial Neural Networks MCQs for adjacent neural-network retrieval practice.

Conceptual questions ask what a shared weight or gate means. They check whether you can distinguish c_t from h_t and interpret f_t=1 with i_t=0, not merely repeat the formulas. Introduction to Natural Language Processing MCQs provides related retrieval practice in a sequence-model application area.

Gradient-reasoning questions ask you to compare how an early signal reaches a later state. Trace each local derivative in order before multiplying the factors.

  1. Why is h2=0.353724 when x2=0? The recurrent contribution from h1 remains.

  2. What if f_t=1 and i_t=0? Then c_t=c_{t-1}, regardless of g_t.

  3. What if c_t=0.4 and o_t=0? Then h_t=0, although the cell still stores 0.4.

RNNs and LSTMs: the short version and next step

  • Carry the previous state forward.

  • Reuse the same weights at every step.

  • Calculate pre-activation before tanh.

  • Compute LSTM retain and write branches separately.

  • Update c_t before h_t.

  • Keep full precision until display.

The RNN states are h=[0.462117,0.353724,0.857277]; the LSTM states are c=[0.400000,0.400000,0.680000] and h=[0.284962,0.284962,0.532367]. For a retrieval check, change only x2 to 1 in the RNN: h2 becomes 0.701218 and h3 becomes 0.915578. Then change only f2 to 0.25 in the LSTM: c2 becomes 0.200000, h2 becomes 0.148031, c3 becomes 0.580000, and h3 becomes 0.470399. For a structured route through related models, continue with Artificial Intelligence (AI).