The Birth of Transformers: From RNNs to Revolution
The Birth of Transformers: From RNNs to Revolution
The year 2017 marked a watershed moment in the history of artificial intelligence and natural language processing. In June of that year, a team of researchers at Google Brain published a paper with a deceptively simple title: "Attention Is All You Need." This paper introduced the Transformer architecture, a neural network design that would fundamentally reshape the landscape of machine learning, natural language processing, and eventually the entire field of artificial intelligence. To understand the revolutionary nature of this contribution, we must first journey through the decade of research that preceded it—the era of recurrent neural networks, the struggles with sequential processing, and the gradual emergence of attention mechanisms as a solution to problems that had long seemed intractable.
This chapter establishes the foundational context for understanding how we arrived at the Transformer era. We begin by examining the limitations of the architectures that dominated NLP before 2017, particularly Recurrent Neural Networks and their more sophisticated variants like Long Short-Term Memory networks and Gated Recurrent Units. We then trace the parallel development of attention mechanisms, starting from their origins in neural machine translation and culminating in the multi-head attention formulation introduced by Vaswani and colleagues. Throughout this exploration, we maintain a focus on the practical engineering challenges that drove these innovations, understanding not just what these architectures do, but why they were necessary and what fundamental problems they solved.
The Sequential Paradigm: RNNs and Their Architecture
Before the Transformer revolution, the dominant paradigm for processing sequential data—text, speech, time series—was the Recurrent Neural Network. The fundamental insight behind RNNs was elegant: to process a sequence, you process it one element at a time, maintaining a hidden state that captures information about all elements seen so far. This hidden state serves as a form of memory, updated at each timestep as new information arrives.
The mathematical formulation of a basic RNN is straightforward. At each timestep $t$, the network takes two inputs: the current input element $x_t$ and the previous hidden state $h_{t-1}$. It produces a new hidden state $h_t$ and optionally an output $y_t$:
$$h_t = \tanh(W_{xh} \cdot x_t + W_{hh} \cdot h_{t-1} + b_h)$$
$$y_t = W_{hy} \cdot h_t + b_y$$
The weight matrices $W_{xh}$, $W_{hh}$, and $W_{hy}$ are shared across all timesteps, which means the network learns to recognize patterns regardless of where they appear in the sequence. This parameter sharing is both a strength and a source of limitation—it makes the model efficient and allows it to handle variable-length sequences, but it also forces the network to compress all information about the past into a single fixed-size hidden state.
Consider what this means in practice. When processing the sentence "The author of this book grew up in a small town where she learned to love reading," the word "she" refers back to "author." To correctly resolve this pronoun reference, the model must maintain information about "author" from the beginning of the sentence until it reaches "she" many tokens later. The hidden state must somehow encode not just "author" but the entire grammatical structure and meaning of everything that preceded it.
The Mathematics of Memory Compression
The compression of arbitrarily long sequences into fixed-size vectors creates a fundamental bottleneck that researchers call the "information bottleneck." Let the hidden state have dimension $d_h$ and let the vocabulary size be $V$. The amount of information that can be stored in $h_t$ is bounded by $O(d_h)$ parameters, while the information content of a sequence of length $n$ grows as $O(n)$. For short sequences, this is not problematic, but as sequences grow longer, the compression becomes increasingly lossy.
This mathematical reality manifests as a practical problem: RNNs struggle to remember information from early in long sequences. The hidden state must simultaneously encode syntactic structure, semantic content, coreference relationships, and long-distance dependencies—each competing for limited representational capacity.
The vanishing gradient problem compounds this issue significantly. To understand why gradients vanish, consider what happens during backpropagation through time (BPTT). When we compute the gradient of the loss with respect to the parameters, we must propagate gradients backward through all timesteps. Each timestep multiplies the gradient by the derivative of the activation function and the recurrent weight matrix.
For a deep RNN with $T$ timesteps, the gradient with respect to the initial hidden state involves $T$ matrix multiplications:
$$\frac{\partial L}{\partial h_0} = \frac{\partial L}{\partial h_T} \cdot \prod_{t=1}^{T} \frac{\partial h_t}{\partial h_{t-1}}$$
The product of Jacobians determines whether gradients grow or shrink exponentially. If the largest eigenvalue of the recurrent weight matrix is less than 1, gradients decay exponentially with sequence length. If it's greater than 1, gradients explode. This exponential behavior makes it nearly impossible to learn long-range dependencies through gradient descent.
Long Short-Term Memory Networks: Gates to Memory
The seminal paper by Hochreiter and Schmidhuber in 1997 introduced the Long Short-Term Memory (LSTM) architecture as a solution to the vanishing gradient problem. The key innovation was the introduction of a gating mechanism that allows the network to learn what to remember and what to forget.
LSTMs introduce a cell state $c_t$, a vector that acts as a conveyor belt running through the entire sequence. Information can be added to or removed from the cell state through carefully controlled gates. This architecture allows gradients to flow more directly through time, mitigating the vanishing gradient problem.
The LSTM equations are:
$$f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f) \quad \text{(forget gate)}$$
$$i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i) \quad \text{(input gate)}$$
$$\tilde{c}_t = \tanh(W_c \cdot [h_{t-1}, x_t] + b_c) \quad \text{(candidate cell state)}$$
$$c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}_t \quad \text{(cell state update)}$$
$$o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) \quad \text{(output gate)}$$
$$h_t = o_t \odot \tanh(c_t) \quad \text{(hidden state)}$$
The forget gate $f_t$ controls what information from the previous cell state should be discarded. The input gate $i_t$ controls what new information should be stored. This multiplicative gating allows the network to maintain information across very long sequences when appropriate, effectively giving it a form of explicit memory management.
The Encoder-Decoder Architecture
LSTMs became the foundation of the encoder-decoder architecture that dominated neural machine translation from approximately 2014 to 2017. The encoder reads the source sentence token by token, compressing all information into a final hidden state (or the final cell state for LSTMs). This final state serves as a "thought vector" or "context vector" that supposedly captures the meaning of the entire source sentence. The decoder then generates the target sentence token by token, using the context vector as its initial hidden state.
This architecture, while groundbreaking for its time, inherited all the limitations of RNNs. The most severe problem was information compression. Consider translating "The conference that took place last year in Vienna which was attended by over 500 researchers from 42 different countries was considered a great success" into German or Chinese. The encoder must compress this entire sentence into a single fixed-size vector, then the decoder must generate an accurate translation from this single representation. Inform