Md. Asif Uddin

    Chapter 9 I.9

    Recurrent Neural Networks

    A recurrent network carries a fixed-width state through a sequence. Learning what to remember requires differentiating every use of the shared weights.

    Shared state, backpropagation through time, gradient transport, and the cost of remembering a sequence.

    How this chapter is built

    M3Load-bearing

    The content is mathematics. Understanding is demonstrated by computation, not recall.

    basics11/11what the words mean
    concept2/2what to picture
    theory4/4why it works, and when it does not
    mathematics15/15derive it, then compute it
    practice9/9build it, break it, read the papers

    Five strands, not one. Mathematics is the spine; the other four are the body. A chapter cannot pay its way out of teaching with problems, nor out of problems with teaching.

    Before you start

    The problem

    A dense layer over a flattened sequence assigns different weights to different positions and requires a chosen input length. A recurrent cell reuses one transition at every position. That buys variable-length processing and a compact streaming state, but introduces a long dependency chain. We build that chain, differentiate it and test where the backward signal is lost.

    A recurrence unrolled over five stepsFive inputs entering five copies of the same cell, each passing a fixed-width hidden state to the next. Everything read so far has to fit through that one state, whose width does not grow with the sequence.the same cell, five timesx₁fh1y1x₂fh2y2x₃fh3y3x₄fh4y4x₅fy5The state has a fixed width. Step 5 reaches step 1 only through everything between it and step 1.
    Fig. 9 — A recurrence unrolled. Every step passes a fixed-width state to the next, so the path between two distant positions is as long as the distance between them.

    What this chapter covers

    • One transition, shared across time
    • Sequence outputs, final-state outputs and causal prediction
    • Unrolling the graph without copying the parameters
    • Backpropagation through time and summed parameter gradients
    • Products of state-dependent Jacobians
    • Vanishing gradients, saturation and transient amplification
    • Gradient norm clipping and truncated backpropagation
    • Padding masks, state resets and sequence boundaries
    • Parameter counts, training memory and streaming inference
    • Dilated convolutions and the path to attention

    Apparatus

    The mathematics this chapter leans on, held in Book 0 so it can be assumed here without being taught here. Not a gate — follow a link when a step stops making sense.

    Rank, eigenvalues and the singular value decomposition 0.LA.04 · The chain rule 0.MC.03 · Floating point 0.NU.01

    Notation

    • TSequence length in tokens
    • dModel width
    • WA weight matrix
    • JA Jacobian matrix
    • ∇Gradient operator
    • λA regularisation coefficient, or an eigenvalue where the context is linear algebra

    Definitions

    Definition 1Sequence

    An ordered list of inputs x_1 through x_T. Position affects meaning, and examples may have different lengths. The input width D is fixed even when T varies.

    Definition 2Hidden state

    A vector h_t in R^H carried from one time step to the next, computed from the preceding state and current input. Its width H does not grow with the number of inputs read.

    Definition 3Parameter sharing

    Reusing the same trainable matrices and biases at every time step. Unrolling creates multiple uses of each parameter, not independent copies. Their gradient contributions must be added.

    Definition 4Unrolling

    Drawing a finite recurrence as an acyclic computation graph with one cell application per time step. Ordinary reverse-mode differentiation on this graph is backpropagation through time.

    Definition 5Backpropagation through time

    The backward sweep through the unrolled graph. At each state, the gradient combines the local output loss with the signal returned from later states, then differentiates the cell and accumulates shared-parameter gradients.

    Definition 6Temporal Jacobian

    The derivative of h_t with respect to h_(t-1), holding input and parameters fixed. For a tanh cell it is diag(1 - h_t²) W, so shared W still gives different Jacobians along a trajectory.

    Definition 7Vanishing or exploding gradient

    Attenuation or amplification of a backward signal through many temporal Jacobians. This describes a derivative, not necessarily the magnitude of the hidden states. Bounded tanh states can coexist with large gradients.

    Definition 8Truncated backpropagation through time

    Carrying a state's value into the next chunk while stopping its derivative at the boundary. This reduces the retained graph and omits cross-boundary credit assignment. Detaching is different from resetting to zero.

    Definition 9Global norm clipping

    Rescaling the concatenated parameter-gradient vector when its Euclidean norm exceeds a positive threshold. Its direction is preserved. Clipping coordinates or tensors separately is a different operation.

    Definition 10Padding mask

    An indicator distinguishing real positions from padding. It can exclude padded losses and stop padded steps from changing the state. A zero input alone does neither.

    Definition 11Causal computation

    An output at time t that depends only on inputs available through t. For next-token prediction the target is x_(t+1). A bidirectional encoder reads later inputs and is not a causal next-token predictor.

    Formal results

    I.9.T1

    Shared weights receive the sum of their temporal contributions

    For a_t = Ux_t + Wh_(t-1) + b, h_t = tanh(a_t), z_t = Vh_t + c, and a sum of per-step losses, let e_t = d ell_t/d z_t. Then delta_t = (Vᵀ e_t + Wᵀ delta_(t+1)) elementwise-multiplied by (1 - h_t²), with delta_(T+1) = 0. The recurrent-weight gradient is the sum of delta_t h_(t-1)ᵀ over all t. I.9.B02 derives the rule.

    What this does not promise

    This assumes the full unrolled graph, fixed inputs, and no extra parameter dependence in the loss. Truncation drops paths, regularisation adds its own derivative, and a learned initial state receives a gradient too.

    I.9.T2

    Uniform contraction bounds distant sensitivity

    Writing A_t = d h_t/d h_(t-1), the derivative d h_T/d h_s is A_T A_(T-1) ... A_(s+1). Its spectral norm is bounded by the product of the factor norms. If each is at most r < 1, the bound is r^(T-s).

    What this does not promise

    A large upper bound does not prove growth. Factors may contract different directions, and the incoming gradient may miss an amplified direction. Eigenvalues of W alone do not determine finite-horizon behaviour; I.9.B03 and I.9.X05 give counterexamples.

    I.9.T3

    Finite-precision state has finite distinguishable histories

    If an H-coordinate state has at most 2^p representable values per coordinate, it has at most 2^(pH) states. There are 2^T binary histories of length T. If T > pH, at least two histories map to the same state. A future computation given only that state cannot distinguish them.

    What this does not promise

    This finite-state counting argument does not say every task needs the entire history. Parity needs only two states. With ideal infinite-precision reals the finite-state premise no longer holds.

    Assumptions

    Each is paired with the problem that shows what its removal costs. An assumption nobody tests is a disclaimer.

    • State is carried only between chunks of the same stream, with batch rows preserving stream identity. Independent examples receive independent initial states. I.9.X06
    • A local Jacobian bound holds along the actual trajectory. A large norm or an eigenvalue calculation alone does not establish exploding gradients. I.9.X05
    • A causal predictor cannot access inputs later than the prediction time. I.9.X09
    • A fixed-width storage argument assumes finitely many representable states, not arbitrary-precision real encoding. I.9.X08

    Propositions

    1. Prop. 1A recurrent network carries its past in a fixed-width state.One transition is reused at every position. The state changes as inputs arrive; the parameters do not change until the optimiser updates them.
    2. Prop. 2Backpropagation through time accumulates shared gradients through a product of temporal Jacobians.The recurrent matrix is shared, but each activation changes its local derivative. Long products can attenuate or amplify the backward signal; their norms and directions both matter.
    3. Prop. 3A convolution over a sequence pays for range with depth and dilation.A convolutional layer evaluates known input positions in parallel. Its receptive field is finite; increasing the dilation schedule extends that field without a recurrent state.

    Numbered equations

    1. a_t = Ux_t + Wh_(t-1) + b; h_t = tanh(a_t); z_t = Vh_t + c

      The same parameters define every time step.

      (I.9.1)
    2. L = sum_t ell_t(z_t, y_t); L_masked = sum_t m_t ell_t / sum_t m_t

      The reduction decides which positions contribute and with what scale.

      (I.9.2)
    3. delta_t = (Vᵀ e_t + Wᵀ delta_(t+1)) elementwise (1 - h_t²)

      A state's derivative combines present and future losses.

      (I.9.3)
    4. dL/dW = sum_t delta_t h_(t-1)ᵀ; dL/dU = sum_t delta_t x_tᵀ

      Shared parameters accumulate contributions from every use.

      (I.9.4)
    5. A_t = diag(1 - h_t²) W; d h_T/d h_s = A_T ... A_(s+1)

      Identical weights can produce different temporal Jacobians.

      (I.9.5)
    6. ||d h_T/d h_s||₂ ≤ product_(k=s+1)^T ||A_k||₂ ≤ r^(T-s)

      The last inequality needs the bound ||A_k||₂ ≤ r.

      (I.9.6)
    7. g_clip = g min(1, tau/||g||₂), with g_clip = 0 when g = 0

      Global clipping changes a finite gradient's length, not its direction.

      (I.9.7)
    8. R_L = 1 + (k - 1) sum_(l=1)^L d_l

      A stride-one convolution stack pays for span with width and dilation.

      (I.9.8)

    Worked problems

    5/5 problems5/4 variants10/10 exercisesquota met, and enforced

    1. I.9.B01 — Run a shared cell through three inputsnumeric▲△△
    2. I.9.B02 — Every use of a weight contributessymbolic▲▲▲
    3. I.9.B03 — Small eigenvalues can hide a large transientcounterexample▲▲▲
    4. I.9.B04 — Separate parameters, states and matrix productsshape▲▲△
    5. I.9.B05 — Same prediction, different backward graphgradient▲▲△

    The whole problem set, with the exercises →

    Practice

    • Assigning rather than accumulating a shared-weight gradient keeps only one time step. In I.9.B02 the recurrent contributions are 0, 1.5 and 7.5; keeping only the last reports 7.5 instead of 9.
    • Masking the loss but reading the state after padding changes a representation. For h_next = 0.5 h + x, state 2 becomes 1 after zero padding. Freeze the padded state, pack the sequence, or gather the last valid output.
    • Carrying state between shuffled, unrelated examples leaks one example into another. Detaching prevents a gradient crossing the boundary but leaves the leaked value unchanged.
    • Clipping a finite gradient limits its norm; it does not restore vanished components or undo NaNs already created. Check finiteness before relying on clipping.
    • One-step truncation in I.9.B05 keeps the full pass's prediction and loss while changing dL/dw from 9 to 7.5 and dL/du from 3.75 to zero. Forward equality is not a gradient test.