Recurrent Neural Networks

2 min read

RNNs process sequences by maintaining a hidden state that is updated at each time step:

ht=tanh(Whht1+Wxxt+b),yt=Woutht+bout\mathbf{h}_t = \tanh(W_h\mathbf{h}_{t-1} + W_x\mathbf{x}_t + \mathbf{b}), \qquad \mathbf{y}_t = W_\text{out}\mathbf{h}_t + \mathbf{b}_\text{out}

The same weights (Wh,Wx)(W_h, W_x) are shared across all time steps (parameter sharing through time).

Vanilla Rnn

Forward pass: process sequence left to right, updating h\mathbf{h} at each step. Output can be taken at each step (sequence-to-sequence) or only at the end (sequence-to-one).

Backpropagation through time (BPTT): unroll the RNN into a deep network (one layer per time step) and apply Backpropagation. The gradient flows through all time steps.

Vanishing gradient problem: the gradient passes through WhW_h at every time step. With zt=Wxxt+Whht1+bz_t = W_x\mathbf{x}_t + W_h\mathbf{h}_{t-1} + \mathbf{b}, the step-to-step Jacobian is

htht1=diag(tanh(zt))Wh\frac{\partial \mathbf{h}_t}{\partial \mathbf{h}_{t-1}} = \operatorname{diag}(\tanh'(z_t))\, W_h

Since tanh(0,1]\tanh' \in (0, 1] the diagonal factor only ever shrinks, and repeated multiplication by WhW_h drives gradients toward 0 (if Wh<1\|W_h\| < 1) or \infty (if Wh>1\|W_h\| > 1). So the RNN forgets long-range dependencies; explosion is fixed by gradient clipping.

This is the fundamental limitation of vanilla RNNs — they struggle with sequences longer than ~20-50 steps.

Solutions:

  • LSTM and GRU — gating mechanisms to control information flow
  • Transformers — direct position-to-position connections, no sequential bottleneck

See also: LSTM and GRU, Self-Attention, Backpropagation

Linked from