Why a recurrent network?
Text is a variable-length sequence, and each word depends on what came before it. An ordinary feedforward network takes a fixed-length input. A recurrent neural network (RNN) processes the sequence step by step and carries a hidden state \mathbf{h}_t that summarizes what it has seen so far, with weights shared across all steps (the Elman network, 1990):
In language modeling, \hat{\mathbf{y}}_t is the distribution of the next word, and the loss is the sum of the cross-entropy over all steps.
Backpropagation through time
We unroll the network through time, so it becomes a deep network whose depth is the sequence length, and apply backpropagation (BPTT). The gradient of the loss at step t with respect to an earlier state \mathbf{h}_k contains a product of Jacobians:
Vanishing and exploding gradients
Bengio et al. (1994), and later Pascanu et al. (2013), analyzed this repeated multiplication:
- If the largest singular value of W_{hh} (multiplied by the tanh derivative term) is less than 1, the gradient vanishes exponentially, so the network cannot learn long-range dependencies: in “الطالبات اللواتي حضرن المحاضرة الطويلة أمس نجحن» (the female students who attended the long lecture yesterday passed), the verb must remember the feminine plural from the start of the sentence.
- If it is greater than 1, the gradient may explode, so the weights jump and the loss becomes NaN.
Solutions: gradient clipping for explosion; gates (LSTM/GRU) for vanishing, together with good initialization.
LSTM: long short-term memory
Hochreiter and Schmidhuber (1997) proposed a cell with a cell state \mathbf{c}_t that is updated by addition, not multiplication, and three gates that learn what to forget, what to add and what to output (the formula below is the one common today, with the forget gate that Gers et al. added later):
When \mathbf{f}_t \approx 1 and \mathbf{i}_t \approx 0, the cell state passes almost unchanged, so the gradient flows through time without vanishing. This resembles the idea of residual connections that you will see in transformers.
Three gates (forget, input and output) control the memory cell c; the cell's horizontal path passes the gradient through time with only element-wise multiplication and addition.
GRU: a simpler version
Cho et al. (2014) proposed the GRU, with two gates and no separate cell state:
Fewer parameters and performance close to the LSTM on many tasks; neither is an outright winner.
Common architectures
- Bidirectional (BiLSTM): two networks, forward and backward, whose representations are concatenated: \mathbf{h}_t = [\overrightarrow{\mathbf{h}}_t; \overleftarrow{\mathbf{h}}_t]. Essential for tagging (entities, parts of speech, diacritization), where what follows the word matters. Not suitable for generation because the future is unknown.
- Stacked: several layers on top of each other.
- With a CRF layer: for sequence labeling (BiLSTM-CRF).
- Character level: for diacritization, spelling correction and Arabizi.
Why did they decline in favor of transformers?
- Sequentiality: \mathbf{h}_t cannot be computed before \mathbf{h}_{t-1}, so there is no parallelism across time, and training is slow on huge corpora.
- The bottleneck: the whole history is compressed into a fixed-size vector.
- Distance: information from 50 words ago passes through 50 steps.
But the ideas partly came back: modern state space models (such as Mamba) and RWKV try to combine the efficiency of recurrent inference (constant memory per token) with parallel trainability.
