A scalar RNN has ht=tanh⁡(w ht−1+u xt) with w=0.5, u=1 and h0=0. For inputs x1=1 and x2=−1, what are h1, h2 and the gradient ∂h2/∂h1?

Sequence models and transformers, Recurrent networks
Level L2 Elementary. Length Medium. Nature Mat Mathematical.

What's being asked

Whether you can unroll a recurrence by hand and apply the chain rule through one time step.

Key idea

∂ht∂ht−1=(1−ht2) w, because tanh⁡′(a)=1−tanh⁡2(a).

Steps

  1. Step 1: h1=tanh⁡(0.5⋅0+1⋅1)=tanh⁡(1)=0.7616.
  2. Step 2: pre-activation =0.5×0.7616−1=−0.6192, so h2=tanh⁡(−0.6192)=−0.5506.
  3. Local derivative: 1−h22=1−0.3032=0.6969.
  4. Chain rule: ∂h2/∂h1=(1−h22) w=0.6969×0.5=0.3484.
  5. This factor is less than 1 because both ∣w∣<1 and tanh⁡′≤1. Over many steps such factors multiply, which is the origin of the vanishing gradient.

Common mistakes

How long your answer should be

Half a page of arithmetic.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: A weight matrix W0∈R4096×4096 is adapted with LoRA of rank r=8. How many trainable parameters does LoRA add for this matrix, and what fraction of the full matrix is that? If LoRA is applied to the query and value matrices of all 32 layers, how many trainable parameters is that in total?
Next in Sequence models and transformers: Take the sinusoidal positional encoding with dmodel=4, ordered as (sin⁡,cos⁡) pairs, PE(pos,2i)=sin⁡ ⁣(pos/100002i/d) and PE(pos,2i+1)=cos⁡ ⁣(pos/100002i/d). What is the vector for position pos=2?