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

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, soh1=tanh(0.5)=0.462117.At
t=2, using full-precisionh1,a2=0.5(0)+0.8(0.462117157)=0.369694, soh2=0.353724.At
t=3, using full-precisionh2,a3=0.5(2)+0.8(0.353723789)=1.282979, soh3=0.857277.
|
|
|
|
|
|---|---|---|---|---|
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.

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, retain0.50(0)=0and write0.50(0.80)=0.40. Thusc1=0.400000andh1=0.75tanh(0.40)=0.284962.At
t=2, retain0.75(0.40)=0.30and write0.25(0.40)=0.10. Thusc2=0.400000andh2=0.284962.At
t=3, retain0.50(0.40)=0.20and write0.80(0.60)=0.48. Thusc3=0.680000andh3=0.90tanh(0.68)=0.532367.
|
|
|
|
|
|
|
|
|
|
|---|---|---|---|---|---|---|---|---|---|
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.

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, andc_tfor an LSTM, forward.Putting
x_tin place ofh_{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_tand write withi_t.Applying
sigmoidtog_tchanges the candidate range, so the update no longer matches its definition. Use the definedtanhcandidate.Confusing
c_twithh_tremoves their separate stored and exposed roles. Computec_tfirst, thenh_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
h1before computingh2carries 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.
Why is
h2=0.353724whenx2=0? The recurrent contribution fromh1remains.What if
f_t=1andi_t=0? Thenc_t=c_{t-1}, regardless ofg_t.What if
c_t=0.4ando_t=0? Thenh_t=0, although the cell still stores0.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_tbeforeh_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).
Keep learning
Backpropagation in a Two-Layer Neural Network: Trace Every Gradient Step by Step
Follow every derivative in a 2 -> 2 -> 1 sigmoid network, then update all nine parameters together. Two sanity checks expose sign and stale-value errors.
Modelling and UML Fundamentals: Class, Sequence and State Diagrams with a Worked Library Example
Learn what each UML view says by carrying one library checkout through classes, objects, messages, guards and state changes without contradicting values.
Logical Sequence of Words: Step-by-Step Method, Shortcuts and Solved Examples
Learn to identify the relation and direction in word-sequencing questions, join forced pairs, and verify every link through four worked patterns.
Cache Coherence and Write Policies: Write-Through, Write-Back and MESI Worked Step by Step
Separate hit policy from miss policy, trace seven accesses under two cache designs, then follow one block across two private caches with MESI.





