Video summary
Stanford CME295 Transformers & LLMs | Autumn 2026 | Lecture 1 - Transformers
Main summary
Key takeaways
Course Purpose and Logistics
Instructors Afshine and Shervine Amidi introduce the course as a practical and conceptual overview of how large language models (LLMs) work, with a focus on the Transformer architecture.
The course is intended for a broad audience, including aspiring researchers, people building personal projects or coding agents, and anyone seeking greater AI literacy. Suggested background includes basic linear algebra and machine-learning concepts such as loss functions, neural networks, and embeddings.
Because the field changes quickly, this year’s material includes developments since the previous course, such as AI agents, coding agents, distillation, and newer model approaches.
The course has nine lectures and meets Fridays from 3:30 to 5:20 p.m. There is no homework. Assessment consists of a midterm covering roughly the first four lectures and a final covering the remaining lectures. Lectures are recorded, and slides, course materials, and a study sheet are available online. Students can ask questions after class or through Ed.
How the Field Reached Transformers
In the 2010s, natural language processing (NLP) commonly relied on separate models for separate tasks, such as sentiment analysis, translation, and named-entity recognition. Recurrent neural networks (RNNs) were a major model family.
The 2017 paper “Attention Is All You Need” introduced the Transformer. Its architecture scaled effectively with more data and computation, helping lead to modern LLMs.
The instructors describe the launch of ChatGPT in 2022 as a major public-facing turning point. They frame the current era as a shift from conversational assistants toward systems with agent-like behavior.
Tokenization: Turning Text into Model Inputs
Models operate on numbers, not raw text. Tokenization divides text into units called tokens, which can then be represented numerically. A tokenizer’s vocabulary—the set of available tokens—affects the length of an input sequence.
Word-Level Tokenization
Word-level tokenization treats each word as a token. It is intuitive and easy to interpret, but it can require a large vocabulary to account for inflections and variants. It may also fail on unfamiliar words, creating an out-of-vocabulary problem. Related forms, such as bear and bears, are not automatically treated as similar.
Character-Level Tokenization
Character-level tokenization uses individual characters as tokens. It keeps the vocabulary small and can handle misspellings or unfamiliar words more robustly. However, it produces much longer sequences, increasing computational cost, and character representations can be less directly meaningful than word representations.
Subword Tokenization
Subword tokenization breaks text into units between characters and whole words, often retaining recurring word components. This offers a compromise: the tokenizer can reuse parts of related words without needing a vocabulary containing every possible word.
Byte-pair encoding (BPE) is a widely used example. In outline, it works as follows:
- Start with a basic set of tokens, such as characters.
- Find the most frequent adjacent token pairs in the training text.
- Merge a frequent pair into a new token.
- Repeat until the desired vocabulary size is reached.
A tokenizer trained on multilingual data can serve multiple languages, though its vocabulary allocation may affect sequence lengths for individual languages.
Special Tokens
The lecture also introduces special tokens, including:
- Unknown-token markers for text outside the tokenizer’s vocabulary.
- Beginning-of-sequence (BOS) and end-of-sequence (EOS) markers to indicate when generation starts and stops.
- Padding tokens to make sequences the same length for efficient batching.
- Other markers used to structure conversational roles or messages. Exact conventions vary across systems.
Embeddings and Word2vec
A basic way to represent tokens is one-hot encoding, in which each token gets a vector with one active entry. This distinguishes tokens but does not express similarity between them.
Models therefore learn embeddings: vector representations intended to capture useful relationships between tokens. The lecture explains word2vec as learning representations through a proxy task—a training objective that is not the final application but helps produce useful representations. Examples include:
- Predicting a word from its surrounding context (CBOW).
- Predicting surrounding words from a given word (Skip-gram).
A neural network is trained to predict the target word, using a loss function to penalize incorrect predictions. The learned hidden representations can encode associations and similarities, such as relationships between concepts or country–capital pairs.
Word2vec has two key limitations:
- A word gets the same representation regardless of context, even when it has different meanings.
- Word order is not represented by a simple bag-of-words approach.
RNNs and Their Limitations
RNNs process a sequence one token at a time, carrying a hidden state forward to summarize what has been seen so far. This allows them to represent word order and supports tasks such as classification, tagging, and text generation.
However, the hidden state must carry information across the sequence, making it difficult to preserve details over long distances. This is the long-term dependency problem, which is associated with issues such as vanishing gradients.
LSTMs were designed to help retain information for longer. However, recurrent models still process tokens sequentially, which limits parallelism and can make training slow.
Attention and Self-Attention
Attention gives a model direct connections to earlier tokens, rather than requiring it to rely only on a single running hidden state. Self-attention extends this idea: to compute a representation for a token, the model can consider other tokens in the same sequence and learn which ones matter.
The lecture introduces three components:
- Query (Q): what a token is looking for.
- Key (K): information used to judge how relevant another token is.
- Value (V): the information combined to form the output.
The general procedure is:
- Project token representations into query, key, and value spaces using learned matrices.
- Compare each query with keys, commonly using dot products, to obtain relevance scores.
- Scale the scores and apply softmax to turn them into weights.
- Use those weights to combine the values, producing a context-aware representation.
Scaled dot-product attention is commonly written as:
softmax(QKᵀ / √dₖ)V
Because attention can relate tokens directly, it can capture relationships across a sequence without passing information through a recurrent chain.
Transformer Architecture
The original Transformer was introduced for machine translation and has two main components.
Encoder
The encoder receives the source-language text, converts tokens into learned token embeddings, and adds positional information. It then uses self-attention and feed-forward layers to produce contextual representations for the source tokens.
Decoder
The decoder generates the target-language output one token at a time. It uses masked self-attention so that a token can attend only to itself and earlier generated tokens—not future tokens. It also uses cross-attention to consult the encoder’s representations of the input sentence.
The decoder produces probabilities for the next token. That token is then fed back into the decoder, and generation continues until an end-of-sequence token is produced.
Positional Information
Positional information is necessary because attention alone does not inherently encode token order. The lecture describes adding position embeddings to token embeddings. Positions can be learned directly or represented using fixed sine and cosine functions at different frequencies.
Practical Techniques Used in Transformer Training
The lecture introduces several techniques that help Transformers train or generalize:
- Residual connections add a layer’s input to its output, helping information and gradients flow through deep networks.
- Layer normalization normalizes activations and can support more stable training.
- Multi-head attention uses several learned attention projections in parallel so the model can represent different kinds of relationships.
- Dropout randomly disables units during training to reduce over-reliance on particular features and improve generalization.
- Label smoothing avoids treating the target token as the only possible outcome with absolute certainty, which can improve generalization and translation metrics.
- Causal masking prevents the decoder from attending to tokens that have not yet been generated. Masked positions can be assigned negative-infinite scores before softmax, making their attention weights zero.
End-to-End Translation Example
The instructors walk through translating a sentence such as “Cute plush bear is reading”:
- Tokenize the input and add any required sequence markers.
- Convert tokens into embeddings and add positional information.
- Pass the source through the encoder to create contextual representations.
- Start the decoder with a beginning-of-sequence token.
- Use masked self-attention and cross-attention to calculate next-token probabilities.
- Select or sample a next token, feed it back into the decoder, and repeat until the end-of-sequence token appears.
The instructors emphasize that attention and feed-forward neural networks are central to the Transformer, while many other elements support training and performance. The lecture establishes the basic architecture; later lectures will cover how these models are trained and used in agentic systems.
Speakers and Sources Featured
- Afshine Amidi — instructor.
- Shervine Amidi — co-instructor; subtitles indicate that he contributes a later worked example.
- Students in the lecture — ask questions about course logistics and the technical material.
- “Attention Is All You Need” (2017) — the Transformer paper discussed in the lecture.
- Word2vec and byte-pair encoding (BPE) — methods discussed in the explanations of embeddings and tokenization.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.