Stanford CS229 Machine Learning | Spring 2026 | Lecture 13: LLMs, Next-Word Prediction Loss

The lecture covers representation learning through embeddings, highlighting supervised and contrastive learning methods to create vector representations that enable semantic search and similarity-based tasks across various data types. It also discusses the application of embeddings in retrieval-augmented generation for large language models, where relevant documents are retrieved at query time to provide context, enhancing model responses without additional training.

The lecture begins by introducing the concept of representation learning, particularly focusing on embeddings, which are vector representations of raw inputs such as images, text, audio, or video. The goal of embeddings is to map similar inputs to nearby points in a vector space, enabling tasks like semantic search. For example, images of cats should have embeddings close to each other, while images of airplanes should be far apart. This concept extends beyond images to text and other modalities, with embeddings serving as a foundational tool for similarity-based applications.

Next, the lecture discusses methods to train these embeddings, starting with supervised pretraining. In this approach, a neural network is trained on labeled data (e.g., ImageNet with thousands of classes) to classify images, and the penultimate layer’s output is used as the embedding. While straightforward, this method requires large labeled datasets, which can be expensive and limited in diversity. Consequently, the field has largely shifted towards unsupervised or self-supervised methods, particularly contrastive learning, which does not rely on labels but instead uses data augmentations to create positive pairs and encourages embeddings of different images to be distinct.

Contrastive learning involves generating two augmented versions of the same image and training the model to produce similar embeddings for these augmentations while pushing embeddings of different images apart. The lecture highlights challenges such as the occasional similarity between different images (e.g., two different cats) that the model might mistakenly push apart. Despite this, the approach works well in practice because most randomly sampled pairs are dissimilar. The loss function used, often based on a softmax over similarities, encourages the model to distinguish positive pairs from many negative pairs, with techniques like hard negative mining improving performance by focusing on challenging examples.

The lecture then explains how embeddings enable semantic search by precomputing embeddings for a large collection of documents or images and then finding the nearest neighbors to a query embedding using inner product similarity. While brute-force search is possible, vector databases and approximate nearest neighbor algorithms are commonly used to scale this process efficiently. Semantic search is widely applicable, from image retrieval to document search, and forms a critical component in many modern AI systems.

Finally, the lecture covers an important application of embeddings in retrieval-augmented generation (RAG) for large language models (LLMs). Since LLMs are typically pretrained on generic data and may not have access to proprietary or personal information, retrieval is used to fetch relevant documents from a private corpus at query time. These documents are then provided as additional context to the LLM, enabling it to generate informed responses without retraining or fine-tuning the model. This modular approach facilitates data governance, easier updates, and cost-effective deployment. The lecture also touches on alternative retrieval methods like regular expression search used in structured domains such as code, emphasizing that retrieval strategies depend on the application context.