intermediate · python
Language Modeling with Transformers
Learn how transformer architectures represent language, model sequences, and generate text. Progress from tokenization and attention fundamentals to training objectives, efficient inference, evaluation, and responsible model use.
What you could build
- Train a small character-level language model and compare its predictions with a token-level model.
- Implement a compact transformer decoder that generates text from a curated corpus.
- Create a tokenizer evaluation tool that compares vocabulary size, sequence length, and unknown-token behavior.
- Build an interactive text-generation interface with configurable temperature, top-k, and top-p sampling.
- Measure perplexity and generation quality across several language-model checkpoints.
You pick the project at the start — these are examples, not a fixed list.
What you'll be able to do
- Trace a tokenized sequence through a decoder-only transformer, including embeddings, positional information, masked self-attention, feed-forward layers, normalization, residual connections, and vocabulary probabilities.
- Calculate and interpret next-token cross-entropy loss and perplexity, then use training, validation, and evaluation evidence to diagnose model behavior.
- Compare greedy decoding, temperature scaling, top-k sampling, nucleus sampling, and beam search, including how key-value caching changes autoregressive inference.
- Explain and evaluate efficiency techniques such as quantization, pruning, distillation, parameter-efficient fine-tuning, low-rank adaptation, speculative decoding, and model routing.
- Analyze transformer outputs using token probabilities, calibration, evaluation slices, counterfactual inputs, uncertainty, reasoning decomposition, and error propagation.
Who this is for
This course is for programmers, ML practitioners, and technically curious learners who want to understand how transformer language models represent text, learn from sequences, generate responses, and behave under different evaluation and inference conditions. It fits learners who want a rigorous conceptual foundation without treating transformers as a black box.
What you should already know
- Basic programming experience, preferably with Python and familiarity with arrays or tensors.
- Working knowledge of vectors, matrices, dot products, and softmax.
- Familiarity with basic probability, logarithms, and the idea of optimizing a loss with gradients.
How a lesson actually goes
Written from this course's first lesson to show the format — not a recording of a real session.
Course content
marks each module's capstone lesson.
01Language Modeling Foundations10 lessons
Establish the foundations of language modeling by moving from tokenization and representation to sequence probabilities, causal prediction, training loss, and evaluation. The module culminates in synthesizing how these concepts support transformer-based text generation.
Course Introduction
Welcome and orientation: explore how transformers represent language, model sequences, generate text, and support efficient and responsible use. Review the learning path from foundational concepts through training, inference, evaluation, and model limitations.
Tokenization
Tokenization divides text into units that a language model can process, such as words, subwords, or characters. The choice of token units affects vocabulary size, sequence length, and how well uncommon text is represented.
Token Embeddings
Token embeddings represent discrete token IDs as learned vectors in a continuous space. These vectors give neural networks a differentiable representation on which subsequent transformations can operate.
Positional Information
Token embeddings alone do not indicate where tokens occur in a sequence. Positional information supplies order-related signals so a model can distinguish sequences with the same tokens arranged differently.
Autoregressive Factorization
Autoregressive language modeling represents the probability of a sequence as successive conditional probabilities. Each prediction is conditioned on the preceding context, making sequence modeling tractable one token at a time.
Causal Masking
Causal masking restricts each sequence position to itself and earlier positions during computation. This preserves the left-to-right prediction condition required for autoregressive language modeling.
Next-Token Prediction
Next-token prediction trains a model to estimate the token that follows a given prefix. For each context position, the model produces a probability distribution over the vocabulary.
Cross-Entropy Loss
Cross-entropy loss measures how much probability a model assigns to the observed target token. Lower loss indicates that the model assigns greater probability to the correct next token across its predictions.
Perplexity
Perplexity is an exponential transformation of average language-model loss. It provides an interpretable measure related to the effective number of plausible next-token choices under the model’s probability distribution.
Foundations Synthesis
Integrate the module’s foundations into a coherent account of language modeling. Reason through how an input sequence becomes token representations, how valid next-token probabilities are produced, and how those predictions are optimized and evaluated.
02Transformer Architecture10 lessons
Examine the components and data flow of transformer architectures, from query-key-value attention through multi-head processing, feed-forward transformations, normalization, residual pathways, and decoder-only language model stacks.
Self-Attention
Self-attention allows every position to gather information from other positions through content-dependent interactions. The resulting representation incorporates context rather than relying only on the token at that position.
Queries, Keys, and Values
Queries represent what each position is seeking, keys represent what each position offers for matching, and values carry the information that is aggregated. Learned projections transform token representations into these three roles.
Attention Scaling
The magnitude of query-key dot products tends to grow with representation dimension. Dividing by the square root of the key dimension keeps softmax inputs in a useful range and supports stable gradients.
Multi-Head Attention
Multi-head attention runs several attention operations in parallel using different learned projections. Their outputs are combined so the model can represent multiple relationship patterns at the same layer.
Position-Wise Feed-Forward Networks
A position-wise feed-forward network applies the same nonlinear transformation to every sequence position separately. It expands and then projects the representation to increase the model's capacity for feature transformation.
Residual Connections
A residual connection adds a sublayer's output to its input. This shortcut gives later layers direct access to earlier representations and makes optimization of deep stacks more reliable.
Layer Normalization
Layer normalization standardizes the features within each token representation and then applies learned scale and shift parameters. This controls activation statistics without depending on the batch dimension.
Decoder-Only Architecture
A decoder-only architecture stacks transformer blocks that use causal self-attention, preventing each position from accessing future tokens. The repeated blocks progressively refine contextual representations for next-token prediction.
Transformer Language Model
A transformer language model projects final hidden states into vocabulary-sized logits and converts them into probabilities with softmax. During causal language modeling, the distribution at each position is trained against the next token.
Architecture Synthesis
This synthesis assesses how projected queries, keys, and values produce scaled multi-head attention, how feed-forward transformations and stabilization mechanisms compose into repeated decoder blocks, and how the final representations produce vocabulary predictions.
03Transformer Training10 lessons
Trace how transformer language models are trained from tokenized sequences through batched next-token prediction, gradient computation, parameter updates, learning-rate control, and validation. The module culminates in synthesizing a complete training loop and reasoning about its behavior.
Training Sequences
Training sequences divide tokenized text into examples in which each position provides context for predicting the following token. Sequence length determines the amount of context processed in one example and affects computational cost.
Batching
A batch combines multiple training sequences so a model can process them together and estimate a gradient from several examples. Batching improves hardware utilization while introducing variation in each gradient estimate.
Teacher Forcing
With teacher forcing, the model receives the known token sequence as input and predicts the next token at every position in parallel. This differs from generation, where each predicted token becomes part of the subsequent context.
Backpropagation
Backpropagation applies the chain rule to propagate the training loss from vocabulary predictions through transformer layers to every learnable parameter. The resulting gradients indicate how each parameter should change to reduce the loss.
Adam Optimizer
Adam maintains moving estimates of gradient values and squared gradient values, then uses them to adapt the scale of parameter updates. Its behavior depends on the learning rate as well as the accumulated gradient statistics.
Learning Rate Schedules
A learning-rate schedule varies the step size used by an optimizer as training progresses. Warmup and decay patterns can control early instability and later convergence without changing the model architecture.
Gradient Clipping
Gradient clipping rescales gradients when their norm exceeds a chosen threshold. This limits the effect of unstable gradient estimates while preserving the direction of the update.
Validation Loss
Training loss measures performance on examples used for updates, whereas validation loss measures performance on held-out examples. Their trajectories reveal whether improvements are generalizing or whether overfitting may be emerging.
Checkpoints
A checkpoint records model parameters and may also preserve optimizer statistics, scheduler state, and training progress. Comparing checkpoints allows training to resume and enables selection based on validation behavior.
Training Loop Synthesis
A transformer training loop repeatedly forms batches, computes teacher-forced next-token predictions, backpropagates loss, and updates parameters under an optimization schedule. Training and validation behavior, gradient control, and checkpoints together provide evidence about learning progress, stability, and generalization.
04Text Generation10 lessons
Learn how a trained autoregressive transformer produces text one token at a time and how decoding choices shape output quality, diversity, repetition, length, and inference efficiency. The module progresses from the generation loop through deterministic and stochastic decoding methods, search strategies, stopping behavior, and key-value caching before synthesizing a complete generation process.
Generation Loop
The generation loop feeds the current sequence into the model, obtains a next-token distribution, selects a token, and appends it to the sequence. Each iteration extends the context used for the following prediction.
Greedy Decoding
Greedy decoding chooses the highest-probability token at every generation step. This method is simple and deterministic, but local choices can produce repetitive or globally suboptimal sequences.
Temperature Scaling
Temperature rescales model logits before softmax. Lower values concentrate probability on likely tokens, while higher values flatten the distribution and increase sampling diversity.
Top-k Sampling
Top-k sampling keeps only the k highest-probability candidate tokens and renormalizes their probabilities. It prevents very unlikely tokens from being selected while preserving randomness among plausible alternatives.
Nucleus Sampling
Nucleus, or top-p, sampling dynamically retains the smallest set of tokens whose cumulative probability is at least p. The number of eligible tokens changes with the uncertainty of the model's distribution.
Beam Search
Beam search retains a fixed number of high-scoring partial sequences rather than committing to one token path immediately. At each step, it expands the beams and keeps the strongest candidates according to their accumulated scores.
Repetition Penalty
A repetition penalty modifies the scores of tokens that have already appeared in the generated context. By lowering their relative likelihood, it can reduce looping behavior while potentially affecting stylistic consistency.
Stopping Criteria
Stopping criteria define when the generation loop terminates. Common conditions include producing an end-of-sequence token, reaching a maximum number of new tokens, or satisfying a prescribed termination condition.
Key-Value Caching
During generation, the keys and values for previously processed tokens do not change. Caching them allows each new step to compute attention for the new token without recomputing those projections for the entire sequence.
Generation Synthesis
This synthesis integrates the full path from a prompt to a completed generated sequence. It requires reasoning about how decoding choices transform model probabilities, how generation terminates, and how caching changes computation without changing the intended autoregressive behavior.
05Evaluation and Diagnostics10 lessons
Evaluate transformer language models with reliable test sets, complementary metrics, calibration analysis, evaluation slices, error analysis, robustness checks, and human judgments. The module culminates in diagnosing model behavior across controlled and changing evaluation conditions.
Evaluation Sets
Evaluation sets provide held-out evidence about how a language model performs beyond the data used for fitting and tuning. Their design affects whether reported results are representative and comparable.
Data Contamination
Data contamination occurs when evaluation content or close variants appear in training data or tuning resources. Detecting contamination is necessary for interpreting whether a score reflects generalization or memorization.
Loss Diagnostics
Aggregate loss can hide meaningful differences across tokens, positions, examples, or sequence lengths. Examining loss at finer resolutions helps reveal concentration of errors and unusual evaluation cases.
Calibration
Calibration concerns the relationship between a model's confidence and its actual correctness. A model can have strong average performance while still being systematically overconfident or underconfident.
Evaluation Slices
Evaluation slices partition examples by relevant properties such as length, language pattern, or task condition. Slice-level results show whether an overall score conceals concentrated strengths or weaknesses.
Error Analysis
Error analysis turns individual failures into patterns by examining what the model predicted, what was expected, and what context was available. Useful categories make weaknesses measurable rather than anecdotal.
Distribution Shift
Distribution shift occurs when evaluation inputs differ from the data patterns a model learned during training. Comparing in-distribution and shifted evaluations helps separate general capability from sensitivity to changing conditions.
Reference-Based Metrics
Reference-based metrics compare generated text with one or more target references using measures such as token or n-gram overlap. Their scores provide useful signals but can penalize valid variation or miss factual and contextual problems.
Human Evaluation
Human evaluation captures qualities that automated metrics may miss, including usefulness, coherence, and factual acceptability. Its conclusions depend on clear criteria, representative samples, and consistency among evaluators.
Evaluation Synthesis
A reliable evaluation diagnosis combines complementary evidence rather than relying on a single score. This synthesis requires tracing a reported result back to the evaluation data, identifying where and why errors occur, and judging how strongly the evidence supports conclusions about model quality.
06Efficient Model Use11 lessons
Analyze the main sources of computational cost in transformer use and the techniques that reduce latency, memory requirements, training overhead, and serving expense. The module progresses from efficiency measures through compression, adaptation, accelerated decoding, and model selection before synthesizing trade-offs among these approaches.
Inference Latency
Inference latency measures how long a model takes to process an input or generate output. Learners examine how sequence length, model size, and decoding steps affect response time.
Inference Throughput
Inference throughput describes how much input or output a system processes within a given period. Learners distinguish throughput from latency and interpret how concurrent requests influence each measure.
Memory Footprint
A model's memory footprint includes stored parameters and temporary inference state. Learners identify how parameter count, numerical precision, and sequence processing contribute to memory use.
Quantization
Quantization maps weights or activations from higher-precision representations to lower-precision formats. Learners reason about reductions in memory and computation alongside possible changes in model accuracy.
Pruning
Pruning removes weights or structures judged to contribute little to model behavior. Learners distinguish unstructured and structured pruning and reason about when removed parameters produce practical efficiency gains.
Knowledge Distillation
Knowledge distillation trains a student model to reproduce information conveyed by a teacher model, often including softened output probabilities. Learners analyze why this can transfer useful behavior to a smaller model.
Parameter-Efficient Fine-Tuning
Parameter-efficient fine-tuning keeps most pretrained parameters fixed and trains a compact set of additional or selected parameters. Learners compare its storage and compute requirements with updating the entire model.
Low-Rank Adaptation
Low-rank adaptation inserts a compact parameterization of the update to selected weight matrices while leaving the original weights unchanged. Learners reason about how the rank controls the number of trainable parameters.
Speculative Decoding
Speculative decoding lets a fast draft model propose several continuation tokens and uses the target model to verify them. Learners determine when accepted batches of tokens can reduce the target model's sequential generation work.
Model Routing
Model routing assigns requests to models with different capabilities, sizes, or costs. Learners analyze how request difficulty and service constraints can guide routing decisions.
Efficiency Strategy Synthesis
This synthesis connects efficiency measurements with quantization, pruning, distillation, parameter-efficient adaptation, low-rank updates, speculative decoding, and model routing. Learners evaluate interactions and trade-offs rather than treating any single technique as universally optimal.
07Model Reasoning9 lessons
Integrate architectural, training, generation, evaluation, and efficiency concepts to reason about how transformer language models produce behavior, express uncertainty, learn from context, and fail. The module progresses from tracing model behavior to analyzing evidence, errors, interventions, and complete reasoning chains.
Model Behavior Tracing
Model behavior tracing follows an input through the major stages of a transformer language model. It connects earlier architectural and operational concepts into a single causal account of an observed output.
Context Sensitivity
Context sensitivity is the dependence of a prediction on the tokens that precede and surround it. The same token or prompt can receive different probabilities when contextual relationships change.
In-Context Learning
In-context learning occurs when a model infers a temporary pattern, task, or response format from information in the prompt. The behavior change comes from altered conditioning context rather than a parameter update.
Token Probability Analysis
Token probability analysis examines the alternatives available at a single generation step. It connects probability mass, ranking, temperature, and sampling behavior to the continuation that is ultimately produced.
Uncertainty Estimation
Uncertainty estimation uses probability distributions and confidence patterns to characterize how decisive or ambiguous a model's predictions are. High confidence does not by itself establish that a generated claim is correct.
Reasoning Decomposition
Reasoning decomposition separates a complex inference into smaller sequential decisions. Each intermediate step can constrain later predictions, expose uncertainty, and create opportunities for errors to be detected.
Error Propagation
Error propagation describes how an early mistake becomes part of the later context and can steer subsequent predictions. Autoregressive generation makes these dependencies especially important because generated tokens are reused as inputs.
Counterfactual Analysis
Counterfactual analysis tests how model behavior changes when one relevant condition is altered while others are held as constant as possible. These comparisons help distinguish plausible causes from coincidental associations.
Synthesis and Model Reasoning
This synthesis assesses whether learners can construct a coherent explanation of why a transformer produced a particular behavior and how that behavior might change under controlled conditions. It requires connecting model internals and operating choices to observable predictions, reasoning patterns, and failure modes.
Questions
Do I need to have trained a neural network before?
No prior transformer training experience is required. You should be comfortable with basic programming, vectors and matrices, probability, and the general idea of gradient-based optimization.
Will this course explain the transformer architecture mathematically?
Yes. You will work through tokenization, embeddings, positional information, queries, keys, values, scaled attention, masking, multi-head attention, feed-forward networks, normalization, and residual connections at a level suitable for tracing the computations and interpreting their roles.
Does the course cover text generation beyond choosing the most likely token?
Yes. It compares greedy decoding, temperature, top-k and nucleus sampling, beam search, repetition penalties, stopping criteria, and key-value caching, and connects those choices to output behavior and inference cost.
Will I learn how to tell whether a language model is performing well?
You will learn to use perplexity and loss alongside calibration, evaluation slices, contamination checks, distribution-shift analysis, reference-based metrics, human evaluation, and structured error analysis. The course emphasizes why no single metric fully captures generation quality.
Are efficient deployment and fine-tuning included?
Yes. The course covers latency, throughput, memory footprint, quantization, pruning, knowledge distillation, parameter-efficient fine-tuning, low-rank adaptation, speculative decoding, and model routing.
The first lesson is ten minutes away.
Free while codeset is early. You choose what you're building before the first lesson starts, and the course is taught around it.