Study Atlas › Computer Science › Artificial intelligence

Transformers and large language models

Neural networks built on self-attention that model sequences and underlie modern language AI.

Edited by Alex C, EduAlx · Sources cited below

Explanation

Vaswani et al. (2017) replaced recurrence with attention: each token weighs every other token through queries, keys and values. Pretraining on large text corpora, then fine-tuning and reinforcement learning from human feedback, gives general-purpose assistants (GPT, Claude, Gemini, Llama). Scaling laws (Kaplan et al., 2020) relate loss to model size, data and compute.

Example

Attention(Q,K,V) = softmax(QKᵀ/√d)V.

Code

import numpy as np
def attention(Q, K, V):
    s = Q @ K.T / np.sqrt(K.shape[-1])
    w = np.exp(s - s.max(-1, keepdims=True))
    w /= w.sum(-1, keepdims=True)
    return w @ V

How it works, step by step

  1. Split text into tokens and embed them
  2. Add positional information
  3. Self-attention mixes information across tokens
  4. Feed-forward layers transform each token
  5. Stack many layers; predict the next token

Open the interactive page with diagrams →

People behind it

References

  1. A. Vaswani et al., NeurIPS 2017, 5998–6008 (Attention Is All You Need).
  2. J. Kaplan et al., arXiv:2001.08361, 2020.

Textbooks

Related topics

Artificial intelligence: search and the Turing testNeural networks and backpropagationComputer vision and convolutional neural networksMachine learningNatural language processingReinforcement learningSoftware engineering and version controlQuantum computingCompilersBlockchain and distributed ledgers