3.1 Model of a Typical Recurrent Neural Network (RNN)
ht) as it reads word-by-word: at every word, it reads the new word (x(t)), glances at its previous sticky-note (h(t−1)), updates the note, and uses the exact same reading rules (shared weights U, W, V) across every sentence length!
1. Why Standard Feedforward MLPs Fail on Sequences
- Traditional MLPs require fixed-size input vectors and assume every input example is independent.
- Sequential data (sentences, audio, stock prices, ECG, video) has variable length, order sensitivity ("Dog bites man" ≠ "Man bites dog"), and contextual dependence ("I grew up in India, so I speak ______").
2. Mathematical Equations of a Typical RNN
Given input sequence (x(1), x(2), …, x(τ)) and initial hidden state h(0) (typically zeros):
2. Hidden State Update: h(t) = tanh(a(t)) = tanh(b + W h(t−1) + U x(t))
3. Output Logits: o(t) = c + V h(t)
4. Predicted Probability: ŷ(t) = softmax(o(t))
5. Sequence Loss (Negative Log-Likelihood): L = ∑t=1..τ L(t) = −∑t=1..τ log ŷ(t)y(t)
- Shared Parameters Across Time:
U(input → hidden),W(hidden → hidden),V(hidden → output), and biasesb, care identical at every time stept, giving huge generalization across variable lengths.
Given: Initial state h(0) = 0, weights U = 0.8, W = 0.5, V = 1.2, b = 0, c = 0, activation tanh, inputs x(1) = 1.0, x(2) = 0.5.
- Step t = 1:
a(1) = 0.5(0) + 0.8(1.0) = 0.8⇒h(1) = tanh(0.8) =0.664,o(1) = 1.2(0.664) =0.797. - Step t = 2:
a(2) = 0.5(0.664) + 0.8(0.5) = 0.332 + 0.40 = 0.732⇒h(2) = tanh(0.732) =0.624,o(2) = 1.2(0.624) =0.749.