Blog

  • Diffusion Models: Generating Images by Learning to Remove Noise

    Learning to Undo Damage

    Ruining an image is easy. Add a little Gaussian noise to every pixel, then a little more, and after enough rounds the picture of a cat is indistinguishable from television static. Nobody needs a neural network for that part. The interesting question is whether a network can learn to run the process backwards, one small step at a time, until static turns into a cat that never existed.

    That is the whole idea behind diffusion models. It was first written down by Jascha Sohl-Dickstein and colleagues at ICML 2015, in a paper that borrowed its framing from nonequilibrium thermodynamics. The results were modest and the paper stayed fairly quiet for five years. Then in 2020 Jonathan Ho, Ajay Jain and Pieter Abbeel published «Denoising Diffusion Probabilistic Models» (DDPM) at NeurIPS, reported a state-of-the-art FID of 3.17 on unconditional CIFAR-10, and the field changed direction. Within two years the systems people used to turn text into pictures were almost all diffusion models.

    The Forward Process

    A diffusion model has two halves, and only one of them is learned. The forward process is fixed. Starting from a real image, it adds Gaussian noise over T steps according to a schedule. In the DDPM paper T was 1,000 and the amount of noise added per step grew linearly from 0.0001 to 0.02. By the last step almost nothing of the original image survives, and the result is very close to pure Gaussian noise.

    One property makes training cheap. Because each step adds Gaussian noise, the noisy image at any step t can be written directly in terms of the clean image: a scaled copy of the original plus a scaled sample of noise. You never have to simulate the 999 intermediate steps. To make a training example you pick a random t, draw one noise sample, and mix.

    Predicting the Noise

    The reverse process is where the network lives. In principle it should model the distribution of a slightly cleaner image given a noisier one. Ho and his coauthors showed that, after working through the variational bound, this reduces to something almost embarrassingly simple: show the network a noisy image and the step number t, and ask it to predict the exact noise that was added. The loss is the mean squared error between the predicted noise and the real one.

    They also found that dropping the weighting terms from the full bound gave better-looking samples, even though it gave up some likelihood. The network itself was a U-Net, the encoder and decoder architecture with skip connections that had become standard for segmentation, with the step number fed in as an embedding.

    Generation then means starting from pure noise and applying the network 1,000 times. At each step it estimates the noise, removes a portion of it, and adds back a smaller amount of fresh randomness. The output of one step is the input of the next.

    Score Matching

    A separate line of research reached the same place from another direction. The score of a probability distribution is the gradient of its log density with respect to the data. It is a vector field that points, from any location, toward regions where data is more likely. If you know the score everywhere, you can sample by following it with a bit of added noise, a procedure called Langevin dynamics.

    Aapo Hyvärinen introduced score matching in 2005 as a way to estimate this gradient without knowing the normalizing constant of the density. Pascal Vincent showed in 2011 that a denoising autoencoder trained on noisy data implicitly learns the score. Yang Song and Stefano Ermon built a generative model on this at NeurIPS 2019. Their key observation was that scores are badly estimated in empty regions far from the data, so they trained one network on many noise levels at once and sampled by annealing from large noise to small.

    Predicting the added noise and estimating the score turn out to be the same quantity up to a scaling factor. Song, Sohl-Dickstein, Kingma, Kumar, Ermon and Poole made this explicit at ICLR 2021 by describing both methods as discretizations of a stochastic differential equation that slowly turns data into noise. Reversing that equation requires only the score. The same framework gave a deterministic version, an ordinary differential equation with the same marginal distributions, which allowed exact likelihood computation. The paper reported an FID of 2.20 on CIFAR-10 and the first 1024 by 1024 samples from a score-based model.

    A Thousand Steps Is Too Many

    The obvious weakness was speed. A GAN produces an image in one forward pass. A DDPM needed a thousand. Much of the work since 2020 has gone into cutting that number without retraining from scratch.

    DDIM (Jiaming Song, Chenlin Meng and Stefano Ermon, ICLR 2021) kept the DDPM training objective but defined a non-Markovian reverse process that can skip steps. The authors reported samples of similar quality 10 to 50 times faster in wall-clock time. Viewing sampling as solving an ODE also opened the door to better numerical solvers, and dedicated samplers now get usable images in a few dozen steps or fewer. Distillation goes further: a student network is trained to match two or more steps of a teacher in one. Consistency models (Yang Song, Prafulla Dhariwal, Mark Chen and Ilya Sutskever, ICML 2023) train a network to map any point on a sampling trajectory directly to its endpoint, which makes single-step generation possible.

    Guidance

    An unconditional model produces random samples from whatever it was trained on. To ask for a specific class or a text prompt, the model needs conditioning, and conditioning alone tends to give images that only loosely follow the request.

    Prafulla Dhariwal and Alex Nichol introduced classifier guidance in «Diffusion Models Beat GANs on Image Synthesis» (NeurIPS 2021). A separate classifier, trained on noisy images, supplies a gradient that pushes each denoising step toward the desired class. With it they reached an FID of 2.97 on ImageNet at 128 by 128 and matched BigGAN-deep with as few as 25 forward passes per sample.

    Training a classifier on noisy images is awkward, though. Jonathan Ho and Tim Salimans proposed classifier-free guidance at a NeurIPS 2021 workshop. One network is trained with the condition dropped a small fraction of the time, so it learns both conditional and unconditional predictions. At sampling time the two predictions are combined, and the difference between them is amplified by a guidance weight. Raising the weight makes images follow the prompt more closely at the cost of diversity. Most text-to-image systems use this trick, and the guidance scale is one of the few knobs ordinary users touch.

    Diffusion in a Latent Space

    Running a U-Net a few dozen times over a full-resolution image is expensive, and most of that computation goes into pixel detail the eye barely notices. Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser and Björn Ommer addressed this in «High-Resolution Image Synthesis with Latent Diffusion Models» (CVPR 2022). They first train an autoencoder that compresses images into a smaller latent grid while keeping perceptual quality, then run the diffusion process in that latent space. A decoder turns the final latent back into pixels.

    The paper also added cross-attention layers to the U-Net, so conditioning inputs such as text embeddings or bounding boxes can steer the denoiser at every resolution. This design became Stable Diffusion. Its first versions diffused in a 64 by 64 latent to produce 512 by 512 images and used a frozen CLIP text encoder for the prompt. Because it was released with open weights and ran on consumer graphics cards, it was the version of diffusion most researchers and hobbyists first worked with. Later systems replaced the U-Net with Transformers operating on latent patches, following the Diffusion Transformer of William Peebles and Saining Xie (ICCV 2023), but the latent space idea stayed.

    Case: Training Images That Come Back

    A model that learns the score of its training distribution should, in theory, be able to reproduce training images. Nicholas Carlini and eight coauthors tested this at USENIX Security 2023 in «Extracting Training Data from Diffusion Models.» They generated large numbers of images from Stable Diffusion and Google’s Imagen using captions from the training data and searched for outputs that were near copies of training images. They extracted over a thousand training examples across the models they studied, including photographs of identifiable people and trademarked logos.

    The memorized images were mostly ones that appeared many times in the training set. Deduplicating data reduces the problem but does not remove it. The authors also reported that the diffusion models they tested leaked more training data than the GANs they compared against. For a technique so often described as generating something new, that is an uncomfortable result.

    Closing Thought

    The most unusual thing about diffusion models is how the learned part and the procedure are separated. The network is only ever trained to do one modest thing: look at a noisy input and estimate the noise. Everything else, the number of steps, the solver, the guidance weight, the choice between a random or a deterministic path, is decided after training. DDIM, better ODE solvers and classifier-free guidance all improved results by changing how a network is used rather than what it learned. Most of deep learning puts its effort into the training objective. With diffusion, a large share of the progress came from the sampler instead.

    By: Max Johnson B.

  • CLIP and Contrastive Multimodal Models: Teaching Images and Text to Share One Space

    A Thousand Labels Is a Small Vocabulary

    For most of the history of computer vision, a classifier knew exactly the categories it was trained on and nothing else. A model trained on ImageNet’s 1,000 classes could tell a Siberian husky from an Alaskan malamute, but if you asked it to find «a dog wearing a raincoat» it had no way to even represent the request. Adding a new category meant collecting new labeled images and retraining.

    The internet, meanwhile, is full of images that already come with text: captions, alt attributes, titles, surrounding paragraphs. That text is noisy and inconsistent, but it describes far more than 1,000 concepts. Contrastive multimodal models are built on a simple bet: if a network learns which caption goes with which image across hundreds of millions of examples, it will learn a visual vocabulary as wide as the language people use to describe pictures.

    Two Encoders and a Matching Game

    CLIP (Contrastive Language-Image Pre-training) was published by Alec Radford, Jong Wook Kim and colleagues at OpenAI in 2021 and presented at ICML that year. The architecture has two separate parts. An image encoder (a ResNet or a Vision Transformer) turns a picture into a vector. A text encoder (a Transformer) turns a caption into a vector of the same size. Both vectors are normalized, so comparing them is just a cosine similarity.

    Training uses a batch of N image-caption pairs. The model computes the similarity between every image and every caption, which gives an N by N matrix. The N correct pairs sit on the diagonal; everything else is a mismatch. The loss is a cross-entropy in both directions: each image has to pick its own caption out of the N candidates, and each caption has to pick its own image. A learned temperature parameter scales the similarities before the softmax.

    The batch is where the negatives come from, so batch size matters a lot. CLIP used batches of 32,768 pairs, which means every caption competes against more than 32,000 wrong images at each step. The training set, called WebImageText, held 400 million pairs collected from public sources on the internet.

    Classification Written as a Sentence

    The payoff is zero-shot classification. To classify an image into a set of categories CLIP has never been trained on as labels, you write each category as a sentence («a photo of a cat», «a photo of a truck»), encode all the sentences, and pick the one closest to the image embedding. The class list is just text, so you can change it at inference time.

    The paper reported that its best model, a ViT-L/14 at 336 pixel resolution, reached about 76 percent top-1 accuracy on ImageNet this way, roughly the same as the original ResNet-50 trained on ImageNet’s 1.28 million labeled images. CLIP saw none of those labels. The wording of the prompt turned out to matter: «a photo of a {label}» worked better than the bare class name, and averaging over dozens of templates helped further. People started calling this prompt engineering for vision.

    A second result got less attention at the time but may matter more. On variants of ImageNet with sketches, renditions and adversarially filtered photos, a standard ImageNet model loses much of its accuracy. Zero-shot CLIP lost far less. The authors argued that a supervised model learns shortcuts specific to one dataset, while a model trained on a broad distribution of web images and text has fewer of them to exploit.

    Scale Over Cleanliness

    Within weeks of CLIP, Google described ALIGN (Chao Jia and colleagues, also ICML 2021), which used the same dual-encoder idea with an EfficientNet image encoder and a BERT text encoder. Its dataset was 1.8 billion image and alt-text pairs with only minimal filtering. The message of ALIGN was that you can skip most of the expensive cleaning if you have enough data, because the contrastive objective averages out a lot of the noise.

    Neither OpenAI’s nor Google’s dataset was released. The LAION project filled that gap with LAION-5B (Schuhmann et al., NeurIPS 2022 Datasets and Benchmarks track), 5.85 billion image-text pairs filtered by CLIP similarity itself. The OpenCLIP project trained open models on it, which made it possible to study scaling laws for CLIP-style training in public. Later work such as DataComp (Gadre et al., 2023) turned the question around: hold the model and training recipe fixed, and compete only on how you filter the data. Filtering choices alone moved results by large margins.

    Replacing the Softmax With a Sigmoid

    The softmax in CLIP’s loss has a practical cost. Every image’s probability depends on every caption in the batch, so devices have to share all embeddings to compute it, and the memory grows with the square of the batch. In 2023 Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov and Lucas Beyer at Google proposed SigLIP, which treats every image-caption pair as an independent binary question: do these two belong together, yes or no? Each cell of the similarity matrix gets its own sigmoid loss.

    That change removes the global normalization. SigLIP trained well with smaller batches than CLIP needed, and it scaled to larger ones too. In one setup that kept a pre-trained image encoder frozen and trained only the text side, the authors reached 84.5 percent zero-shot ImageNet accuracy using four TPUv4 chips for two days. SigLIP encoders have since become a common choice as the vision component of open multimodal language models.

    An Embedding Space Other Systems Reuse

    The shared embedding space ended up being more useful than zero-shot classification. Image search is the direct application: encode a text query, find the nearest image vectors, no tags required. Content moderation and dataset filtering use the same trick, and LAION itself was built by keeping only pairs whose CLIP similarity passed a threshold.

    Generative models borrowed the text side. Stable Diffusion’s first versions conditioned image generation on the output of CLIP’s ViT-L/14 text encoder, and OpenAI’s DALL-E 2 generated images from CLIP image embeddings. Going the other way, LLaVA (Liu et al., 2023) connected a frozen CLIP image encoder to a language model through a small projection layer and trained the result to answer questions about images. Meta’s ImageBind (Girdhar et al., CVPR 2023) pushed the idea further, aligning audio, depth, thermal and motion data to the same space by pairing each one with images, so that modalities never seen together during training could still be compared.

    Case: An Apple Labeled «iPod»

    In March 2021, OpenAI researchers studying CLIP’s internal units found what they called multimodal neurons: single units that responded to a concept whether it appeared as a photo, a drawing or written text. One responded to spiders, to the word «spider» and to Spider-Man. The same property produced a strange weakness. When they stuck a handwritten paper note saying «iPod» on a Granny Smith apple, CLIP classified the photo as an iPod. They called these typographic attacks. Unlike most adversarial examples, they need no gradients and no computer, only a pen.

    The failure follows from the training signal. Web captions often repeat the text that appears inside an image, so reading the text is a reliable shortcut for matching captions. CLIP learned to read because reading helped, and it has no separate notion of «the object» versus «words written near the object.»

    What a Bag of Words Cannot Tell Apart

    Other limits show up in careful benchmarks. Yuksekgonul and colleagues (ICLR 2023) tested whether vision-language models understand word order and relations, for example whether they can distinguish «the horse is eating the grass» from «the grass is eating the horse.» CLIP-style models often could not. Their explanation was that the contrastive task rarely requires order: in a batch of random web pairs, getting the nouns right is almost always enough to find the matching caption, so the model has little reason to learn more. CLIP also struggled with counting objects and, in the original paper, scored poorly on handwritten digits (MNIST) despite its broad training.

    Bias came along with the web data. An audit by Agarwal and colleagues in 2021, using images from the FairFace dataset with extra labels added, found that photos of Black people were classified into non-human categories such as «gorilla» at a rate of about 14 percent, against under 8 percent for every other group. Changing the label set changed the outcome, which is the other side of zero-shot flexibility: whoever writes the class list also shapes the errors.

    Closing Thought

    CLIP’s objective never asks the model to understand an image. It asks only that the right caption score higher than the other captions in the batch. Everything the model learns, from recognizing dog breeds to reading text on an apple to ignoring word order, is whatever happened to be useful for winning that comparison against random internet captions. That makes the choice of negatives, and of data, the real specification of what these models know. A harder batch teaches a finer distinction; a lazier one lets the shortcut stand.

    By: Max Johnson B.

  • CMA-ES: An Evolution Strategy That Learns the Shape of the Problem

    Optimization With Only a Score

    Some optimization problems give you almost nothing to work with. You can hand the problem a vector of numbers and get back a single score, and that is all. There is no gradient, no formula you can differentiate, sometimes not even a guarantee that the same input returns the same score twice. Tuning the gains of a controller in a physics simulator looks like this. So does fitting the parameters of a chemical model, or choosing the learning rate and dropout of a neural network.

    The Covariance Matrix Adaptation Evolution Strategy, almost always shortened to CMA-ES, is one of the most respected tools for this kind of problem when the variables are continuous. Its standard form was published by Nikolaus Hansen and Andreas Ostermeier in 2001, in the journal Evolutionary Computation, under the title «Completely Derandomized Self-Adaptation in Evolution Strategies.» Twenty-five years later it is still a standard first choice, and a standard baseline, for hard black-box problems with a few dozen to a few hundred variables.

    Sampling From a Gaussian

    CMA-ES does not keep a population of individuals the way a genetic algorithm does. It keeps a probability distribution: a multivariate normal with a mean vector m, a global step size σ, and a covariance matrix C. Each generation it draws λ candidate solutions from that distribution, evaluates them, and sorts them from best to worst.

    The new mean is a weighted average of the best μ candidates, usually the top half, with better ones receiving larger weights. So the center of the search moves toward the region where good solutions were just found. That part alone is a fairly ordinary evolution strategy. What makes CMA-ES different is what it does next with the covariance matrix and the step size.

    The default population size is small. Hansen’s tutorial on the method recommends λ = 4 + ⌊3 ln n⌋, where n is the number of variables. For a 100-dimensional problem that gives 17 samples per generation. The suggested defaults for the other internal parameters are also derived from n, which is why practitioners rarely tune them.

    Learning the Shape of the Search

    Picture a long, narrow valley that runs diagonally across the search space. A plain random search with circular steps wastes most of its samples climbing the walls, because almost every direction points uphill except the one along the valley floor. To make progress it would need tiny steps, and tiny steps make the walk along the valley painfully slow.

    The covariance matrix fixes this. After each generation, CMA-ES looks at the steps that led to the selected candidates and nudges C so that those directions become more likely in the future. Over many generations the sampling distribution stretches into an ellipsoid aligned with the valley. The algorithm has, in effect, learned which combinations of variables move together.

    This is called the rank-μ update, because it uses information from all μ selected steps at once. On a convex quadratic function, the learned covariance tends toward a matrix proportional to the inverse of the Hessian. That is the same matrix a Newton or quasi-Newton method tries to approximate with derivatives. CMA-ES gets there using nothing but the ordering of sampled points.

    Evolution Paths

    The second idea in the 2001 paper is called cumulation. Instead of looking only at the current generation, CMA-ES keeps a running, exponentially fading sum of the recent movements of the mean. Hansen and Ostermeier call this the evolution path.

    The path carries information a single generation cannot. If the mean keeps moving in roughly the same direction, the steps add up and the path grows long. If it zigzags back and forth, consecutive steps cancel and the path stays short. The rank-one update adds the outer product of this path to the covariance matrix, which strengthens directions of consistent progress.

    The two updates complement each other. The rank-μ update is efficient with large populations, while the rank-one update with cumulation works well with the small default population. The modern algorithm uses both.

    Step-Size Control

    Shape is only half of the problem. The algorithm also has to decide how far to step overall, and a covariance matrix learned from past steps cannot answer that by itself, because it adapts too slowly.

    CMA-ES handles this with a second evolution path, built in a coordinate system where the current covariance has been factored out. The rule then compares the length of this path with the length expected under pure random selection. If the path is longer than expected, successive steps are pointing the same way and the step size is too small, so σ grows. If it is shorter, steps are cancelling each other and σ shrinks. The paper calls this cumulative step-size adaptation.

    The rule is short. It lets the algorithm take large steps far from the optimum and converge quickly once it gets close, without a manually designed schedule like the cooling schedule in simulated annealing.

    Invariance

    Hansen’s tutorial on the method names invariance as one of the governing principles of its design. CMA-ES only uses the ranking of candidates, never their raw scores, so its behavior does not change if the objective function is replaced by any strictly increasing transformation of itself. Squaring a positive objective or taking its logarithm makes no difference.

    It is also invariant, given a matching starting point and covariance, to rotations, translations and general linear transformations of the search space. A badly scaled, rotated problem is, for CMA-ES, the same problem as a well-scaled one once the covariance has adapted. This is why the 2001 paper reported speed-ups of several orders of magnitude on badly scaled, non-separable functions, and a factor of three to ten on moderately mis-scaled ones.

    There is a theoretical account of why this works. The Information-Geometric Optimization framework, published by Yann Ollivier and coauthors in the Journal of Machine Learning Research in 2017, shows that the rank-μ update of CMA-ES can be read as a natural gradient step on the parameters of the search distribution. The algorithm came first and the theory came later, which is a common order of events in this field.

    Restarts and Larger Populations

    With its default settings, CMA-ES behaves like a fast local search. On a function with many local optima it will often converge cleanly to the wrong one. The standard fix is to restart.

    Anne Auger and Nikolaus Hansen proposed IPOP-CMA-ES at the 2005 IEEE Congress on Evolutionary Computation. Each time the run stalls, it restarts with the population size doubled. Larger populations smooth over small bumps in the landscape and make the search more global. On the 25 test functions of the CEC 2005 special session on real-parameter optimization, IPOP-CMA-ES ranked first overall. A later variant, BIPOP-CMA-ES, alternates between large and small populations, and the two were among the best performers in the Black-Box Optimization Benchmarking workshops of 2009 and 2010.

    Case: An 867-Parameter Driver

    One well-known use in machine learning is the 2018 World Models paper by David Ha and Jürgen Schmidhuber. They trained a variational autoencoder to compress game frames and a recurrent network to predict what comes next. The part that actually chose actions was a small linear controller with 867 parameters, and they did not train it with gradients. They evolved it with CMA-ES, using a population of 64, on the CarRacing task. The agent reached an average score of 906 ± 21, which the authors reported as the first result to solve that environment.

    The choice made sense. A reinforcement learning reward is noisy and delayed, and 867 parameters is well within the range where CMA-ES is comfortable. Every candidate can also be evaluated on a separate CPU core, so the method parallelizes almost perfectly.

    Hyperparameter tuning is another natural fit. In 2016 Ilya Loshchilov and Frank Hutter compared CMA-ES with Bayesian optimization methods for tuning a convolutional network on MNIST, running evaluations on 30 GPUs in parallel, and argued that CMA-ES is a competitive option when many evaluations can run at once. Today the method is easy to try: the pycma library is maintained by Hansen, and the Optuna framework includes a CMA-ES sampler.

    The Quadratic Cost

    The weakness is the covariance matrix itself. For n variables it has about n²/2 free entries, and the standard algorithm needs memory and time per sample that grow quadratically with n. It also needs on the order of n² evaluations to learn a full matrix. With a few hundred variables this is fine. With tens of thousands it becomes impractical, which is why nobody trains the weights of a large neural network with standard CMA-ES.

    Several variants trade expressiveness for scale. In 2008 Raymond Ros and Nikolaus Hansen proposed sep-CMA-ES, which restricts C to a diagonal matrix and brings the cost down to linear. It loses the ability to learn correlations between variables, but in their experiments it needed fewer evaluations than full CMA-ES on several problems above a hundred dimensions, including the non-separable Rosenbrock function. Limited-memory versions such as LM-CMA, inspired by L-BFGS, keep a few vectors instead of a full matrix.

    Closing Thought

    What I find most interesting about CMA-ES is how little it assumes. It never reads the value of the objective, only the order of the candidates. And yet, from that ordering alone, it reconstructs something close to the curvature of the problem, the same information a second-order method gets from derivatives. A lot of «nature-inspired» optimizers are described by their metaphor. CMA-ES is better described by what it estimates, and that is probably why it has outlasted so many of them.

    By: Max Johnson B.

  • Genetic Programming: Evolving Programs Instead of Numbers

    Searching for Programs Instead of Parameters

    Most optimization algorithms search for numbers. A genetic algorithm evolves a string of bits or a vector of real values, and a neural network adjusts millions of weights inside an architecture someone else designed. In both cases the shape of the solution is fixed ahead of time. The algorithm only fills in the blanks.

    Genetic programming asks for something more ambitious. The thing being evolved is a program: a formula, a decision procedure, a circuit, a set of instructions. The size and structure of the solution are not decided in advance. They emerge from the search.

    The idea has an early version in Nichael Cramer’s 1985 paper at the first International Conference on Genetic Algorithms, which represented simple sequential programs in a form that could be mutated and recombined. The field took its name and its standard form from John Koza, whose 1992 book Genetic Programming: On the Programming of Computers by Means of Natural Selection (MIT Press) set out the tree-based approach that most people still mean when they say GP.

    Programs as Trees

    In Koza’s formulation, a program is a tree. Internal nodes are functions such as addition, multiplication, sine or an if-then-else. Leaves are terminals: input variables and constants. The expression (x + 3) * sin(y) becomes a tree with a multiplication at the root, an addition on one side and a sine on the other.

    The designer chooses two sets before the run starts. The function set lists the operations the program may use, and the terminal set lists the inputs it may read. This choice matters a great deal. Give the search only arithmetic and it can never discover a periodic signal; give it fifty operators and the space becomes so large that progress slows to a crawl.

    Every function must also accept any value another node might produce. Koza called this closure. Division is the usual example: a random tree will sooner or later divide by zero, so GP systems use a «protected» division that returns a fixed value in that case (Koza used 1) instead of crashing.

    The first generation is random. A common recipe from Koza’s book is ramped half-and-half, which builds some trees to full depth and others with irregular shapes, across a range of depths, so the starting population contains small, large, bushy and thin programs.

    Crossover and Mutation on Trees

    Each program is run on a set of test cases and scored. Better programs are more likely to be selected as parents, usually through tournament selection, where a few individuals are drawn at random and the best of them wins.

    Subtree crossover is the signature operator. Pick a random node in each parent, cut there, and swap the two branches. The children are valid programs by construction, because any subtree can replace any other once closure holds. Mutation does the same thing with a single parent, replacing a random branch with a freshly generated one.

    Later research changed the representation without changing the principle. Cartesian GP, introduced by Julian Miller and Peter Thomson in 2000, encodes programs as graphs on a grid, which lets one node feed several others. Lee Spector’s PushGP uses a stack-based language where programs can manipulate their own code. Selection also got sharper: lexicase selection, studied by Thomas Helmuth, Lee Spector and James Matheson in a 2015 IEEE Transactions on Evolutionary Computation paper, picks parents by filtering on test cases one at a time in random order instead of averaging the errors, which keeps specialists alive that an averaged score would throw away.

    Bloat

    Run GP long enough and the programs grow. Trees get deeper and wider while fitness barely improves. This is called bloat, and it was noticed in the earliest runs.

    Much of the extra material is code that does nothing, such as adding x and then subtracting it, or a branch that can never execute. Early explanations blamed these «introns,» arguing that they protect useful code from destructive crossover. Sean Luke later showed that the causation probably runs the other way: inactive code accumulates because trees grow, not the reverse. William Langdon and Riccardo Poli proposed an explanation that does not depend on introns at all. For any program, there are far more large programs with the same behavior than small ones, so a search that drifts among equally fit solutions will tend to drift toward larger ones.

    Bloat is not a cosmetic problem. Large programs are slow to evaluate, hard to read, and more likely to overfit. Practitioners control it with depth limits, size penalties added to fitness, or multi-objective selection that treats size as a second objective. Luke and Liviu Panait compared several of these methods in a 2006 paper in Evolutionary Computation, and the simple depth limit Koza used in 1992 was competitive with the more elaborate alternatives.

    Symbolic Regression

    The most common use of GP today is symbolic regression: given data, find a formula that fits it. Unlike linear regression, the form of the formula is not given. Unlike a neural network, the answer is an equation a person can read.

    The best-known demonstration is a 2009 Science paper by Michael Schmidt and Hod Lipson at Cornell, «Distilling Free-Form Natural Laws from Experimental Data.» They recorded the motion of physical systems, from simple harmonic oscillators to a chaotic double pendulum, and used symbolic regression to search for quantities that stayed constant over time. The system recovered Hamiltonians, Lagrangians and conservation laws without being told what physics to look for. The method became a commercial tool called Eureqa.

    GP has also held up under systematic comparison. In 2021, William La Cava and colleagues published SRBench at NeurIPS, a benchmark of 14 symbolic regression methods on 252 datasets, including real-world regression tasks and physics equations with known answers. On the real-world data, Operon, a GP system that tunes constants with the Levenberg-Marquardt algorithm, produced the most accurate models, and four of the top five methods were GP-based. On the noiseless physics problems, AI Feynman, which is not a GP method, recovered the most exact equations, though its advantage shrank as noise was added. No single method won everywhere.

    Case: Patents, Antennas and Lenses

    Koza spent years trying to show that GP could produce results a human expert would respect. He set criteria for «human-competitive» results, one being that the result duplicates or improves on an invention that had been patented. A 2010 survey in Genetic Programming and Evolvable Machines counted at least 36 such results, including 15 that duplicated the function of a patented 20th-century invention, 6 that did the same for 21st-century patents, and 2 that were patentable new inventions. His group rediscovered six patented optical lens systems (GECCO 2005), and a patent for improved general-purpose controllers, filed with GP-generated designs, was issued in 2005 as U.S. Patent 6,847,851.

    A different group built a result that flew. For NASA’s Space Technology 5 mission, Jason Lohn, Gregory Hornby and Derek Linden evolved X-band antennas under tight requirements on beam width and bandwidth. They ran two algorithms, one of which used a GP-style tree representation that allowed the antenna’s arms to branch. The evolved designs outperformed the one produced by the mission’s antenna contractor, and an evolved antenna went to space on the ST5 spacecraft in 2006. It looks like a bent paperclip, and it is not a shape anyone would have sketched by hand.

    Case: Repairing Bugs With GenProg

    In 2012, Claire Le Goues, Michael Dewey-Vogt, Stephanie Forrest and Westley Weimer presented a study at ICSE with a memorable title: «A Systematic Study of Automated Program Repair: Fixing 55 out of 105 Bugs for $8 Each.» Their system, GenProg, takes a buggy C program and a test suite with at least one failing test. It evolves patches by copying, deleting and moving statements that already exist elsewhere in the program, and keeps a patch if all the tests pass.

    The paper helped start the field of automated program repair. It also led to one of the most useful critiques in the GP literature. In 2015, Zichao Qi, Fan Long, Sara Achour and Martin Rinard reanalyzed the patches at ISSTA. Because of errors in the test scripts, most reported patches did not pass the full test suite. Only 18 were plausible, and only 2 were correct. Many of the rest simply deleted the functionality that was failing. The authors built a system called Kali that only deletes code, and it found plausible patches for 27 defects, more than GenProg.

    Language Models as Mutation Operators

    Random subtree swaps are a crude way to edit code. A recent line of work replaces them with a large language model. In FunSearch, published in Nature in 2023 by Bernardino Romera-Paredes and colleagues at Google DeepMind, a population of Python programs is kept and scored by an automatic evaluator, and a pretrained language model writes new candidates by modifying the best ones.

    The structure is recognizably evolutionary: a population, variation, selection, an islands model to keep diversity. Applied to the cap set problem in combinatorics, FunSearch found a cap set of size 512 in eight dimensions, where the largest known had been 496, and it improved the asymptotic lower bound. It also found heuristics for online bin packing that beat the standard first-fit and best-fit rules. As in classic GP, the output is a program, short enough for a mathematician to read and analyze.

    Closing Thought

    Every genetic programming system depends on a fitness function, and in GP the fitness function is the specification. The search has no idea what the programmer wanted. It only knows which tests passed. GenProg’s deleted features, the bloated trees full of dead code, and the protected division that quietly returns 1 are all the same lesson: evolution finds whatever satisfies the test, including answers nobody intended. That is why FunSearch paired its language model with a strict evaluator, and why the hardest part of using GP is still writing a test that says exactly what you mean.

    By: Max Johnson B.

  • Simulated Annealing and Tabu Search: Two Ways to Climb Out of a Local Optimum

    Stuck at the Bottom of the Nearest Valley

    The simplest way to optimize something is to start with a solution, look at small changes to it, and keep any change that makes things better. This is local search, or hill climbing if you are maximizing. For a traveling salesman tour, a small change might be reversing a segment of the route. You repeat until no small change helps.

    The problem is where this process stops. It stops at a local optimum, a solution that is better than all of its neighbors but possibly much worse than the best solution overall. On a smooth bowl-shaped landscape that is fine. On the rugged landscapes of real combinatorial problems, with millions of small valleys, a greedy search falls into the first one it finds and never leaves.

    Simulated annealing and tabu search both start from this same local search loop. They differ in how they allow it to escape. Annealing escapes by accepting bad moves at random, with a probability that shrinks over time. Tabu search escapes by remembering where it has been and refusing to go back.

    Annealing in Metals and in Code

    Annealing is an old metallurgical process. A metal is heated and then cooled slowly, which gives its atoms time to settle into an ordered, low-energy crystal structure. Cool it too fast and the atoms freeze in a disordered state full of defects.

    In 1953, Nicholas Metropolis and colleagues published a Monte Carlo method for simulating a physical system at a fixed temperature. A random change to the system is proposed. If it lowers the energy, it is accepted. If it raises the energy by some amount Δ, it is accepted with probability exp(−Δ/T), where T is the temperature. At high temperature almost everything is accepted; at low temperature, uphill moves become rare.

    Scott Kirkpatrick, C. Daniel Gelatt and Mario Vecchi, working at IBM, turned this into an optimization method in a 1983 paper in Science titled «Optimization by Simulated Annealing.» Their idea was to treat the cost of a solution as energy and to lower T gradually during the search. Early on, the algorithm wanders across the landscape almost freely. Later, it behaves more and more like plain hill climbing. Vladimír Černý reached the same idea independently and published it in 1985 in the Journal of Optimization Theory and Applications, applied to the traveling salesman problem.

    The appeal is that the algorithm needs only a cost function and a way to generate neighbors. The same twenty lines of code work on permutations, graphs and bit strings.

    The Cooling Schedule

    Most of the practical difficulty of simulated annealing is one choice: how fast to lower the temperature. The common answer is geometric cooling, where T is multiplied by a constant such as 0.95 after a fixed number of moves. You also need a starting temperature high enough that most uphill moves are accepted at first, and a rule for when to stop.

    There is a theory behind this, and it is unusually clean. Stuart and Donald Geman showed in 1984 that if the temperature falls no faster than a constant divided by the logarithm of the iteration count, the search converges to a global optimum. Bruce Hajek sharpened this in 1988 with a necessary and sufficient condition: the constant must be at least the depth of the deepest local minimum that is not global, meaning the largest uphill climb needed to escape a trap.

    The catch is that logarithmic cooling is extremely slow. To halve the temperature you must square the number of iterations. For a realistic problem, the guaranteed schedule takes longer than exhaustive search would. So practitioners use fast schedules that carry no guarantee, and they tune them by experiment. The convergence theorem tells you the method is not arbitrary, but it does not tell you how to run it.

    Case: Placing Circuits on a Chip

    The first serious application was chip design. Placing thousands of circuit components on a chip so that the wires between them are short is a huge combinatorial problem. Their Science paper reported results on partitioning, placement and wiring of electronic systems, along with traveling salesman instances of several thousand cities.

    Carl Sechen and Alberto Sangiovanni-Vincentelli followed with TimberWolf, published in the IEEE Journal of Solid-State Circuits in 1985. It used annealing for standard cell, macro cell and gate array placement, plus global routing. On industrial circuits, they reported area savings of 15 to 62 percent over the layout programs in use at the time.

    Not every evaluation was so favorable. David Johnson, Cecilia Aragon, Lyle McGeoch and Catherine Schevon ran a long experimental study published in Operations Research in 1989. On graph partitioning, annealing beat the classic Kernighan-Lin heuristic on large sparse random graphs, but on graphs with geometric structure Kernighan-Lin won by a substantial margin. The authors described annealing’s success as mixed. That conclusion has held up: annealing is a dependable general method, and a good problem-specific heuristic often beats it.

    Tabu Search and Short-Term Memory

    Fred Glover introduced the term tabu search in a 1986 paper in Computers and Operations Research, «Future Paths for Integer Programming and Links to Artificial Intelligence,» and laid out its principles in a two-part article in the ORSA Journal on Computing starting in 1989. He described it as a metaheuristic superimposed on another heuristic, one that gets past local optimality by forbidding certain moves.

    The mechanism works like this. At each step, tabu search examines the neighborhood and moves to the best neighbor that is not forbidden, even if that neighbor is worse than the current solution. This alone would make the search cycle forever, stepping out of a valley and straight back in. To prevent that, it keeps a tabu list of recent moves, or attributes of recent moves, and forbids reversing them for a certain number of iterations, called the tabu tenure.

    If the search just swapped jobs A and B, swapping them back is tabu for, say, the next ten iterations. The search is forced to keep moving away from the local optimum it just left. What gets stored is usually an attribute, not a full solution, because attributes are cheap to compare.

    This basic version needs no randomness. It always takes the best allowed move, while annealing samples one random neighbor at a time.

    Aspiration, Intensification and Diversification

    Forbidding moves by attribute has a side effect. Sometimes a tabu move would lead to a solution better than anything found so far, and the tabu list blocks it only because of a coincidence of attributes. Aspiration criteria fix this. The most common one says a tabu move is allowed if it produces a new best solution.

    Glover also described longer-term memory. Intensification strategies return to regions where good solutions were found and search them more carefully, for example by fixing features that appear in most of the elite solutions. Diversification strategies push the search toward regions it has rarely visited, often by penalizing moves that have been used frequently. A frequency count per attribute is enough for both.

    So tabu search is closer to a framework than a single algorithm. A competitive implementation usually combines tabu lists, aspiration, frequency memory and restarts from elite solutions, tuned to the problem.

    Case: Job Shops and Quadratic Assignment

    Two results show how much a well-designed tabu search can do. Éric Taillard published Robust Tabu Search for the quadratic assignment problem in Parallel Computing in 1991. Instead of a fixed tabu tenure, it draws the tenure at random from a range, which removes the need to tune one exact value and makes cycling less likely. Robust Tabu Search became a standard reference method for the quadratic assignment problem, and later algorithms often use it as their local improvement step.

    Eugeniusz Nowicki and Czesław Smutnicki published «A Fast Taboo Search Algorithm for the Job Shop Problem» in Management Science in 1996. Their key contribution was a small, carefully chosen neighborhood: only swaps of adjacent operations on the critical path of the schedule, which are the only swaps that can shorten the total completion time. With a tiny neighborhood, each step is fast, and tabu memory keeps the search from circling. Their algorithm and its successor, i-TSAB, were for a long time the state of the art for minimizing makespan on standard job shop benchmarks.

    In both cases, most of the strength came from the problem structure. The tabu mechanism let the search exploit it without getting stuck.

    Randomness Versus Memory

    The two methods make an opposite bet. Simulated annealing assumes that random uphill moves, accepted less and less often, will shake the search loose from traps. Tabu search assumes that remembering recent history is enough to avoid revisiting the same traps, and it uses that memory to drive the search deliberately.

    In practice, annealing is easier to implement and needs only a cooling schedule. It also works well when neighborhoods are huge and only a random sample of moves can be evaluated. Tabu search evaluates the whole neighborhood, or a large part of it, at every step, so it fits problems where neighborhoods are small or can be scored incrementally.

    The two are often combined. Annealing can use a short tabu list to avoid immediate reversals, and tabu search can add random tenure or restarts. Both also serve as the local improvement step inside genetic algorithms, where the population explores broadly and the single-solution method refines the best individuals.

    A Final Observation

    The convergence theorem for simulated annealing is one of the cleanest results in the field, and almost nobody runs the algorithm the way the theorem requires. The useful version is the one that throws away its own guarantee. Tabu search never had a guarantee, and it became one of the strongest tools for scheduling anyway. For hard combinatorial problems, the better question to ask about a method is not whether it will find the optimum but how it decides to accept a worse solution. A search that never takes a bad step stops in the first valley. Which bad steps it takes, and why, decides most of what it can find.

    By: Max Johnson B.

  • Knowledge Distillation and Pruning: Getting a Small Model to Do a Large Model’s Job

    Models Larger Than Their Task Requires

    The most accurate neural networks are usually the largest ones, and large models are expensive to run. They need memory that a phone or a microcontroller does not have, they answer slowly, and at the scale of millions of requests per day, every extra millisecond and watt adds to the bill.

    Researchers noticed early that trained networks are heavily redundant. Much of what makes a network large is needed to train it well, not to represent what it learned. That observation led to a family of techniques, grouped under the name model compression, that start from a large trained model and produce a smaller one with nearly the same accuracy. The three main ones are knowledge distillation, pruning and quantization. This post focuses on the first two.

    Teachers and Students

    The core idea of distillation appeared in 2006, when Cristian Bucilă, Rich Caruana and Alexandru Niculescu-Mizil published a paper called «Model Compression.» They had large ensembles of models that were accurate but slow, and they showed that a single small neural network could be trained to imitate the ensemble’s predictions and keep most of its accuracy.

    Geoffrey Hinton, Oriol Vinyals and Jeff Dean gave the method its current form and name in «Distilling the Knowledge in a Neural Network» (2015). A large, already trained model is the teacher, and a smaller model is the student. The student is trained to reproduce the teacher’s outputs, not only the correct labels from the dataset.

    Soft Targets and Temperature

    Imitating a teacher works better than learning from labels alone because the teacher’s output contains more information than the label does. A label says an image is a dog and nothing else. A trained classifier’s output might say 90% dog, 9% wolf, 0.9% cat, and almost zero for truck. Those small probabilities encode which classes look alike, something the label alone never tells the student. Hinton called this «dark knowledge.»

    In a well-trained network, though, the wrong-class probabilities are often so tiny that they barely affect the loss. The fix is a temperature parameter T in the softmax. Dividing the logits by a temperature above 1 flattens the distribution, so the small probabilities become large enough to matter. The student is trained with the same temperature to match these softened outputs, usually alongside the ordinary loss on the true labels, with a weight balancing the two. At inference the temperature goes back to 1.

    Distilling Internal Representations

    Matching only the final outputs gives the student a limited signal. FitNets, from Adriana Romero and colleagues (ICLR 2015), added «hints»: the student is also trained to reproduce the teacher’s activations at an intermediate layer, through a small adapter that accounts for the difference in size. This made it possible to train students that were thinner and deeper than the teacher.

    Later methods transfer other internal quantities, such as attention maps or the relationships between examples in a batch. In Transformers, it is common to match hidden states and attention matrices layer by layer, as TinyBERT (Jiao and colleagues, 2020) does.

    DistilBERT

    The best-known example in language processing is DistilBERT, published in 2019 by Victor Sanh, Lysandre Debut, Julien Chaumond and Thomas Wolf at Hugging Face. The student has the same architecture as BERT-base with half the layers, 6 in place of 12, and was initialized by taking one layer out of every two from the teacher. It was trained with a combination of three losses: the usual masked language modeling loss, a distillation loss on the teacher’s softened output distribution, and a cosine loss that aligns the direction of the student’s hidden states with the teacher’s.

    The paper reports a model 40% smaller and 60% faster than BERT that retains 97% of its language understanding performance as measured on the GLUE benchmark. DistilBERT became one of the most downloaded models on the Hugging Face Hub, largely because it could run on CPUs at reasonable speed.

    Pruning

    Pruning removes parts of a trained network. The idea dates back to 1989, when Yann LeCun, John Denker and Sara Solla published «Optimal Brain Damage,» which used second-derivative information to estimate which weights could be deleted with the least effect on the error.

    The modern version is simpler. In 2015, Song Han and colleagues showed that removing the weights with the smallest absolute values, then retraining the remaining ones to recover accuracy, and repeating the cycle, could remove most of the connections in networks like AlexNet and VGG-16 without loss of accuracy. Their follow-up, «Deep Compression,» which won a best paper award at ICLR 2016, chained pruning with weight quantization and Huffman coding. It reduced the storage of AlexNet by 35 times, from 240 MB to 6.9 MB, and of VGG-16 by 49 times, from 552 MB to 11.3 MB, with no loss of accuracy.

    Unstructured and Structured Pruning

    There is an important practical distinction. Unstructured pruning deletes individual weights anywhere in the network. It reaches very high sparsity, but the result is a matrix of the same shape with many zeros in it, and ordinary hardware does not run a sparse matrix any faster unless the software and chips are built for it. The file gets smaller, and the speed often does not change.

    Structured pruning removes whole units: neurons, convolutional filters, attention heads or entire layers. The resulting network is smaller in the ordinary sense and runs faster on any hardware. The cost is that accuracy drops sooner, because removing a whole filter is a much coarser operation than removing scattered weights. Paul Michel and colleagues showed in 2019 that many attention heads in Transformers can be removed at test time with little effect, which made head pruning a common technique for language models.

    The Lottery Ticket Hypothesis

    If a pruned network works well, a natural question is whether the small network could have been trained directly from the start. For a long time the answer seemed to be no: sparse networks trained from scratch did worse than pruned ones.

    Jonathan Frankle and Michael Carbin proposed an explanation in «The Lottery Ticket Hypothesis,» which received a best paper award at ICLR 2019. They trained a network, pruned the smallest weights, and then reset the surviving weights to the exact values they had at initialization. Trained again from there, these sparse subnetworks, often 10 to 20% of the original size on MNIST and CIFAR-10, matched the accuracy of the full network. With a fresh random initialization, the same sparse structure did not.

    Their hypothesis is that a large random network contains small subnetworks whose initial weights happen to be well suited for training, the winning tickets, and that part of the benefit of a large network is that it holds many such candidates. On larger networks, later work found that the weights need to be rewound to a point early in training instead of to the very beginning. Finding the tickets still requires training the big network first, so the result is more important for understanding why over-parameterized networks train well than as a practical shortcut.

    Combining the Techniques

    Distillation, pruning and quantization act on different things, so they are usually combined. Distillation produces a smaller architecture. Pruning removes what is left of the redundancy. Quantization then stores each remaining weight with fewer bits, for example 8-bit integers in place of 32-bit floating-point numbers. A typical pipeline for deployment on a device applies all three, checking accuracy after each step.

    Distillation has also taken on a second role with large language models. A large model can generate answers, explanations or labels for a big set of inputs, and a much smaller model is then trained on that synthetic data. The student never sees the teacher’s internal probabilities, only its text, but the principle is the one from 2006: use an expensive model to teach a cheap one.

    Limits

    Compression always costs some accuracy. Average accuracy on a benchmark can stay almost unchanged while performance on rare cases gets worse. Sara Hooker and colleagues showed in 2019 that pruning affects a small subset of examples disproportionately, often atypical or underrepresented ones, even when the top-line accuracy barely moves. A student can also inherit its teacher’s errors and biases, since it is trained to copy them. And when the student is far smaller than the teacher, the gap can be too wide to bridge in one step, which is why some methods distill through an intermediate-size model first.

    Reflection

    Compression works because training a model and running it need different things. Training benefits from a large network, which has many possible paths to a good solution. Once the solution is found, most of that capacity is no longer needed. This is why the common practice is to train a large model first and then shrink it, and not to design a small model and train it directly. The lottery ticket experiments are the clearest evidence for this: the small network that works was inside the large one from the start, but it could only be identified after training the large one.

    By: Max Johnson B.

  • Retrieval-Augmented Generation: Giving a Language Model Documents to Read Before It Answers

    What a Language Model Does Not Know

    A large language model stores what it knows in its weights, and those weights stop changing when training ends. That has three consequences. The model knows nothing about events after its training cutoff. It knows nothing about private information, such as a company’s internal manuals or a hospital’s protocols. And when asked about something it has little information on, it often produces a fluent, confident and wrong answer, the behavior usually called hallucination.

    Retraining a model every time a document changes is too expensive to be practical. Retrieval-augmented generation (RAG) takes a different route. When a question arrives, the system searches a collection of documents for relevant passages, places them in the prompt together with the question, and asks the model to answer using that material. The knowledge sits in a database that can be updated at any time, and the model’s job shifts from recalling facts to reading and summarizing them.

    The 2020 Paper

    The name comes from «Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks,» by Patrick Lewis and colleagues at Facebook AI Research, University College London and New York University, presented at NeurIPS 2020. Their system combined two trained components: a retriever based on Dense Passage Retrieval (DPR), which searched an index of about 21 million Wikipedia passages, and BART, a sequence-to-sequence model that generated the answer conditioned on the retrieved passages. The two were fine-tuned together.

    The paper described the combination as parametric memory (the weights of the generator) plus non-parametric memory (the document index). It set state-of-the-art results on several open-domain question answering benchmarks and generated text that was more specific and factual than BART alone. It also pointed out a property that later became the main selling point: the index can be swapped to update what the system knows without retraining anything.

    Modern RAG systems rarely train the retriever and generator jointly. They connect an off-the-shelf embedding model, a vector database and a general-purpose language model. The principle is the same.

    Turning Documents Into a Searchable Index

    Before any question can be answered, the documents have to be prepared. They are first split into chunks, pieces of a few hundred words each. Chunking matters more than it seems. Chunks that are too large dilute the relevant sentence among unrelated text. Chunks that are too small lose the context needed to understand them (a paragraph that says «this method» without saying which one).

    Each chunk is then passed through an embedding model, which turns it into a vector of a few hundred to a few thousand numbers. The model is trained so that texts with similar meaning get nearby vectors. The vectors are stored in a vector database along with the original text. This step is done once, and repeated only for documents that change.

    Sparse and Dense Retrieval

    There are two families of search methods. Sparse retrieval matches words. Its standard algorithm is BM25, developed by Stephen Robertson and colleagues in the 1990s, which scores a document by how often the query’s terms appear in it, weighted by how rare each term is across the collection. It is fast, needs no training, and is very good with exact terms like product codes, names and error messages.

    Dense retrieval compares embeddings. Karpukhin and colleagues introduced DPR in 2020, training two BERT encoders, one for questions and one for passages, so that a question and the passage that answers it end up close together. On open-domain question answering datasets, DPR beat a strong BM25 system by 9 to 19 percentage points in top-20 retrieval accuracy. Dense retrieval can match a question to a passage that shares no words with it, which sparse methods cannot do.

    Each approach fails where the other works, so production systems often run both and merge the results, a setup called hybrid search. Searching millions of vectors exactly would be slow, so vector databases use approximate nearest neighbor indexes such as HNSW (Malkov and Yashunin), which trade a small loss in accuracy for a large gain in speed.

    Reranking and Building the Prompt

    The first retrieval step is tuned for speed and returns maybe 50 to 100 candidates. A second model, a reranker, then scores each candidate against the question more carefully. Rerankers are usually cross-encoders: they read the question and the passage together, which is more accurate than comparing two separately computed vectors but too slow to run on the whole collection.

    The top few passages are inserted into the prompt with an instruction along the lines of «answer using only the following sources and cite them.» The order matters. In «Lost in the Middle» (2023), Nelson Liu and colleagues showed that language models use information at the beginning and end of a long context much better than information in the middle, with accuracy dropping noticeably when the relevant passage was placed in the center.

    Ways RAG Fails

    RAG reduces hallucinations, but it does not eliminate them. Failures happen at each stage. The retriever can miss the relevant passage, and then the model answers from memory or invents something. The retriever can return a passage that looks related but is about something else, and the model follows it. The correct passage can be retrieved and then misread or ignored. And some questions need information scattered across many documents («what are the main themes in these 500 reports?»), which retrieving five chunks will not answer.

    A study from Stanford’s RegLab, by Varun Magesh and colleagues, tested commercial legal research tools built on RAG, whose vendors had advertised them as avoiding hallucinations. On about 200 legal queries scored by hand, the tools still gave incorrect or misgrounded answers a large share of the time: about 17% for Lexis+ AI and about 33% for Westlaw AI-Assisted Research. Those numbers were better than GPT-4 without retrieval, at 43%, but far from zero. In many cases the tools cited real documents that did not support the claim attributed to them.

    Refinements

    Much recent work targets those failure points. Query rewriting has a language model reformulate the user’s question, or break it into sub-questions, before searching. Self-RAG, from Akari Asai and colleagues (ICLR 2024), trains the model to decide when retrieval is needed and to critique whether the retrieved passages are relevant and whether its own answer is supported by them. GraphRAG, from Microsoft Research in 2024, uses a language model to extract entities and relationships from the documents into a knowledge graph and writes summaries of clusters in that graph, which helps with questions about the collection as a whole.

    Agentic approaches let the model run several searches in sequence, read the results, and decide what to look for next, closer to how a person researches a topic.

    RAG, Fine-Tuning and Long Context

    RAG is not the only way to adapt a model to new knowledge. Fine-tuning changes the weights, which works well for teaching a style, a format or a skill, but poorly for facts that change often, and it gives no source to cite. Context windows have grown to hundreds of thousands of tokens, so small collections can be placed directly in the prompt, at a higher cost per query and with the middle-of-context problem described above. For large or frequently updated collections, and wherever answers need to point to their sources, retrieval is still the practical option. Many systems combine the three.

    Evaluation

    A RAG system has to be evaluated in two parts. Retrieval is measured with metrics like recall at k: of the questions in a test set, how often is the passage with the answer among the top k results. Generation is measured on faithfulness (is every claim in the answer supported by the retrieved passages) and on whether the answer addresses the question. Looking only at the final answer hides which part is failing, and a wrong answer caused by bad retrieval needs a different fix than one caused by the model misreading a good passage.

    A Final Observation

    RAG changes where the difficulty of the problem is. The language model no longer has to know the answer, but the system has to find the right paragraph among millions, and most of the errors come from that search step. Search has been studied for decades in information retrieval, long before language models existed. A team building a RAG system will usually get more out of improving chunking, indexing and ranking than out of switching to a larger model.

    By: Max Johnson B.

  • Vision Transformers: Reading an Image as a Sequence of Patches

    An Architecture Borrowed From Language

    For most of the 2010s, computer vision meant convolutional neural networks. Convolutions build in two assumptions about images: nearby pixels are related, and a pattern means the same thing wherever it appears. Those assumptions are correct often enough that CNNs learned well from modest amounts of data, and every major vision benchmark was led by one.

    The Transformer came from machine translation and has neither assumption. It treats its input as a set of tokens and lets every token look at every other token through self-attention. In October 2020, a team at Google Research led by Alexey Dosovitskiy posted a paper titled «An Image is Worth 16×16 Words: Transformers for Image Recognition at Scale,» later published at ICLR 2021. It asked what would happen if a standard Transformer were applied to images with as few changes as possible. The model was called the Vision Transformer, or ViT.

    From Pixels to Tokens

    A Transformer needs a sequence, and an image is a grid. Treating every pixel as a token is not practical, because self-attention compares every token with every other one, so its cost grows with the square of the sequence length. A 224 by 224 image has more than 50,000 pixels.

    ViT cuts the image into square patches instead, typically 16 by 16 pixels. A 224 by 224 image gives 196 patches. Each patch is flattened into a vector and passed through one linear layer to produce a patch embedding. These 196 vectors are the «words» of the title.

    Two more pieces are added. A sequence of patches has no inherent order, so a learned position embedding is added to each patch to tell the model where it came from. And an extra learnable token, the class token, is placed at the start of the sequence. After the Transformer layers, the output at that position is used as the representation of the whole image and fed to a classifier.

    Everything after that is a standard Transformer encoder: alternating layers of multi-head self-attention and small feed-forward networks, with residual connections and layer normalization. Nothing in it is specific to images.

    What Attention Sees in an Image

    In a CNN, a neuron in an early layer sees only a small neighborhood, and the network needs many layers before information from opposite corners of the image can meet. In a ViT, every patch can attend to every other patch in the very first layer.

    The original paper measured how far attention reaches in each layer. Some attention heads in the earliest layers already covered most of the image, while others stayed local, behaving a lot like convolutions. Deeper layers attended globally. Raghu and colleagues compared the internal representations of ViTs and ResNets in 2021 and found that ViTs have more uniform representations across layers and carry spatial information further into the network.

    The Need for Data

    The most important result in the ViT paper was about data. Trained on ImageNet alone, with about 1.3 million images, ViT was worse than a comparable ResNet. Without the built-in assumptions of convolution, the model had to learn from scratch that nearby patches are related, and 1.3 million images were not enough for that.

    With more data, the result reversed. Pre-trained on ImageNet-21k (14 million images), ViT matched the best CNNs. Pre-trained on JFT-300M, Google’s internal dataset of 300 million images, the largest model (ViT-H/14) reached 88.55% top-1 accuracy on ImageNet after fine-tuning, ahead of the best convolutional models of the time. The authors summarized it by saying that large-scale training trumps inductive bias.

    That left a practical problem, since almost nobody outside Google had 300 million labeled images.

    DeiT and Training on ImageNet Alone

    A few months later, Hugo Touvron and colleagues at Facebook AI showed that the data requirement was partly a matter of training recipe. Their Data-efficient image Transformers (DeiT), published at ICML 2021, trained a ViT on ImageNet only and reached 81.8% top-1 accuracy (83.1% at a higher input resolution), competitive with CNNs of similar size. The changes were heavy data augmentation, strong regularization and a longer, carefully tuned schedule.

    DeiT also introduced a distillation token. Alongside the class token, a second special token is trained to reproduce the prediction of a teacher network, and the best teachers turned out to be CNNs. The student Transformer picks up some of the convolutional network’s inductive bias through the teacher’s outputs.

    Swin and Hierarchical Designs

    Plain ViT has two limits for tasks beyond classification. It produces features at a single, coarse resolution, while object detection and segmentation need features at several scales. And the quadratic cost of global attention becomes too high for large images.

    Swin Transformer, from Ze Liu and colleagues at Microsoft Research Asia, addressed both. It computes self-attention only inside small local windows, so cost grows linearly with image size. Between consecutive layers, the windows are shifted so that information can flow across window borders. Patches are merged as the network gets deeper, which produces a pyramid of feature maps at decreasing resolution, like a CNN. Swin set records in object detection on COCO and semantic segmentation on ADE20K and received the Marr Prize for best paper at ICCV 2021.

    Self-Supervised Pre-Training: MAE and DINO

    The other way around the labeled data problem is to not use labels. Masked Autoencoders (MAE), from Kaiming He and colleagues at CVPR 2022, hide 75% of the patches of an image and train a ViT to reconstruct the missing pixels from the remaining quarter. The encoder only processes the visible patches, so pre-training is fast, and a ViT-H trained this way on ImageNet-1k alone reached 87.8% accuracy after fine-tuning.

    DINO, from Mathilde Caron and colleagues in 2021, trains a student network to match the output of a slowly updated teacher on different crops of the same image. A side effect got a lot of attention: the attention maps of the class token outlined objects in the image, although the model had never seen a segmentation label. DINOv2, released by Meta in 2023, scaled the approach to a curated set of 142 million images and produced features that work for classification, segmentation and depth estimation with no fine-tuning.

    The CNN Response

    ViT’s success raised the question of whether attention was the reason, or whether the improvements came from everything around it: bigger models, better augmentation, new training schedules. In «A ConvNet for the 2020s» (CVPR 2022), Zhuang Liu and colleagues took a standard ResNet and modernized it step by step, borrowing design choices from Swin such as larger kernels, fewer activation functions and layer normalization. The resulting ConvNeXt matched or beat Swin at similar size on ImageNet, detection and segmentation, using only convolutions.

    The conclusion most researchers took from this is that the two families are closer than the early results suggested. Convolutions remain efficient on small data and limited hardware. Transformers scale better with data and combine naturally with text and other modalities, since everything becomes tokens.

    ViT as a Backbone

    That last property explains where ViTs ended up. CLIP, from OpenAI in 2021, trains an image encoder (in its best versions a ViT) together with a text encoder so that matching images and captions get similar embeddings. Vision-language models that answer questions about pictures typically feed ViT patch embeddings into a language model. The Segment Anything Model (Kirillov and colleagues, 2023) uses a ViT pre-trained with MAE as its image encoder and was trained on 11 million images with over a billion masks. Diffusion models for image generation have also moved from convolutional U-Nets to Transformer backbones operating on patches.

    Closing Observation

    ViT is a useful case for a general question in machine learning: how much knowledge should the designer build into a model, and how much should be left for the data to teach it. Convolutions build in knowledge about images, and that helps when data is scarce. ViT removes most of it, does worse with little data, and does better once there is enough. Neither choice is right in general. The better one depends on how much data and compute are available, and for image models that amount grew very quickly in the years after 2020.

    By: Max Johnson B.

  • Graph Neural Networks: Deep Learning for Data Made of Connections

    Data That Does Not Fit in a Grid

    Convolutional networks work on images because an image is a regular grid: every pixel has the same number of neighbors, arranged in the same way. Recurrent networks and transformers work on text because a sentence is a sequence with a clear order. A lot of important data has neither structure.

    A molecule is a set of atoms connected by bonds. A social network is a set of people connected by friendships. A road network is a set of intersections connected by streets. A citation network, a power grid, a supply chain, a protein’s contact map: all of them are graphs, made of nodes and the edges between them. The number of neighbors varies from node to node, there is no natural ordering of the nodes, and the connections themselves carry most of the information. Graph neural networks (GNNs) are the family of models built for this kind of data.

    Message Passing

    Most GNNs follow one pattern, which Justin Gilmer and colleagues formalized in 2017 as message passing. Each node starts with a feature vector, for example the element type and charge of an atom. In each layer, every node collects messages from its neighbors, combines them with a function that does not depend on their order (such as a sum, mean or max), and uses the result to update its own vector.

    After one layer, each node’s representation contains information about its direct neighbors. After two layers, it includes neighbors of neighbors. After k layers, each node has a summary of its k-hop neighborhood. The same learned functions are applied at every node, similar to how a convolutional filter is applied at every position in an image, so the model works on graphs of any size and shape.

    The node representations can then be used in three ways. Node-level tasks predict something about each node, such as the topic of a paper in a citation network. Edge-level tasks predict whether a connection exists or what kind it is, such as whether a user will interact with an item. Graph-level tasks pool all node vectors into one and predict a property of the whole graph, such as whether a molecule is toxic.

    From the First GNNs to GCN

    Franco Scarselli and colleagues described a graph neural network model in 2009, in which node states were updated repeatedly until they reached a fixed point. A different line of work started from spectral graph theory. Bruna and colleagues in 2014 defined convolution on graphs through the eigenvectors of the graph Laplacian, the graph equivalent of Fourier analysis. It was mathematically elegant but expensive and tied to one specific graph.

    Thomas Kipf and Max Welling simplified this in their 2017 ICLR paper on graph convolutional networks (GCN). They approximated the spectral convolution so heavily that each layer became a simple operation: average the features of each node and its neighbors, with a normalization based on node degrees, then apply a linear transformation and a nonlinearity. It was cheap, easy to implement, and gave strong results on semi-supervised node classification in citation networks, where only a small fraction of nodes have labels. GCN is still the model most people learn first.

    GraphSAGE, Attention and Expressive Power

    GCN as originally described needs the whole graph at training time. GraphSAGE, from William Hamilton, Rex Ying and Jure Leskovec (2017), made GNNs work on very large and changing graphs by sampling a fixed number of neighbors for each node and learning aggregation functions that apply to nodes never seen during training.

    Graph attention networks (GAT), from Petar Veličković and colleagues in 2018, replaced the fixed averaging weights with learned attention: each node decides how much to listen to each neighbor, based on both of their features. This helps when some neighbors are much more relevant than others.

    A theoretical question followed: which graphs can a GNN tell apart? Keyulu Xu and colleagues showed in 2019 that standard message-passing GNNs are at most as powerful as the Weisfeiler-Lehman graph isomorphism test, a classic algorithm that iteratively relabels nodes based on their neighbors’ labels. There are simple pairs of non-identical graphs that the test, and therefore these GNNs, cannot distinguish. They proposed the Graph Isomorphism Network (GIN), which uses sum aggregation with a multilayer perceptron and reaches that upper bound. Much later research has tried to go beyond it by adding substructure counts, positional encodings, or higher-order message passing.

    Known Weaknesses

    Stacking many GNN layers does not work as well as stacking many CNN layers. The first reason is over-smoothing: each layer averages node features with their neighbors, and after many layers all nodes in a connected region end up with nearly the same representation, losing the information that distinguished them. The second is over-squashing: information from an exponentially growing neighborhood must be compressed into a fixed-size vector, so signals from distant nodes get lost. For these reasons most practical GNNs are shallow, often two to four layers.

    Many early models also assumed homophily, the tendency of connected nodes to be similar. That holds in citation networks, where papers cite related papers, but not in graphs where edges connect different kinds of things, such as fraudsters and their victims. Scaling is another challenge, since a node’s neighborhood can explode in size after a few hops in graphs with billions of edges. Graph transformers, which let every node attend to every other node and add information about the graph structure through positional encodings, are one response to over-squashing, at a higher computational cost.

    Molecules and Drug Discovery

    Chemistry is one of the most natural applications, because a molecule is a graph. Gilmer and colleagues introduced their message passing framework while predicting quantum chemical properties of small molecules, reaching accuracy close to that of expensive quantum calculations at a fraction of the cost.

    In 2020, Jonathan Stokes and colleagues at MIT published in Cell the discovery of halicin, an antibiotic identified with a message-passing neural network trained to predict whether molecules inhibit the growth of E. coli. After screening a drug repurposing library and then more than 100 million molecules from a larger database, they found a compound structurally different from existing antibiotics that was effective against several drug-resistant bacteria in lab and mouse experiments. GNNs are also used to predict material properties, and to reason about protein structure, where residues in contact form a natural graph.

    Maps, Weather and Recommendations

    In 2020, DeepMind described a GNN deployed in Google Maps to predict travel times. The road network was divided into «supersegments» of adjacent roads that share traffic, and the model learned how congestion spreads between them. According to DeepMind, it improved the accuracy of real-time arrival estimates by up to 50% in cities such as Berlin, Jakarta, São Paulo, Sydney, Tokyo and Washington D.C.

    GraphCast, published by Remi Lam and colleagues in Science in 2023, uses a GNN to forecast global weather up to ten days ahead. The Earth’s surface is represented as a multi-resolution mesh of nodes, and message passing over that mesh moves information around the globe. It outperformed the European Centre for Medium-Range Weather Forecasts’ high-resolution deterministic system (HRES) on 90% of 1,380 verification targets, and it produces a forecast in under a minute on a single TPU, compared to hours on a supercomputer for traditional numerical models.

    Recommendation systems are another major use, since users and items form a bipartite graph. PinSage, from Rex Ying and colleagues at Pinterest in 2018, applied GraphSAGE-style convolutions to a graph with billions of nodes to recommend pins. Banks and payment companies use GNNs for fraud detection, since fraudulent accounts often reveal themselves through their connections more than through their individual features.

    Reflection

    The main idea behind graph neural networks is that a node is described by its neighborhood as much as by its own features. An atom’s role depends on what it is bonded to, and a road’s traffic depends on the roads feeding into it. Convolutional networks relied on the same idea, with the neighborhood fixed by the pixel grid. GNNs let the data define the neighborhood, which is why the same kind of model can work on molecules, road networks and the whole planet’s atmosphere.

    By: Max Johnson B.

  • Neural Architecture Search: Letting Algorithms Design Neural Networks

    Architecture Design by Hand

    Every famous neural network was designed by people. AlexNet, VGG, ResNet and the Transformer came from researchers who tried ideas, trained models, read the results and tried again. How many layers, what kind of layers, how wide each one should be, where to put skip connections, which activation function to use: each decision was the result of intuition and a lot of trial and error.

    Neural architecture search (NAS) tries to automate that process. It treats the architecture itself as something to optimize: define a space of possible networks, search it with an algorithm, and evaluate candidates by training them and measuring their performance. Any NAS method can be described by three choices. The search space defines which architectures are possible. The search strategy decides which ones to try. The performance estimation strategy decides how to judge a candidate without spending too much compute on it. Thomas Elsken, Jan Hendrik Metzen and Frank Hutter used this framing in their 2019 survey, and it is still the standard way to compare methods.

    The Reinforcement Learning Approach

    The paper that started the modern field was «Neural Architecture Search with Reinforcement Learning» by Barret Zoph and Quoc Le at Google, presented at ICLR 2017. A recurrent neural network acted as a controller that generated a description of an architecture one decision at a time: filter size, stride, number of filters for each layer. That architecture was trained, its validation accuracy was used as a reward, and the controller was updated with a policy gradient method to produce better architectures over time. The networks it found were competitive with the best hand-designed ones on CIFAR-10, and the recurrent cell it found for language modeling beat the LSTM on Penn Treebank.

    The cost was enormous, because every sample meant training a network from scratch. The authors used hundreds of GPUs in parallel. The follow-up, NASNet (Zoph, Vasudevan, Shlens and Le, 2018), made the search more practical by searching for a small building block, a «cell,» instead of a whole network. The best cell was then stacked many times to build the full model, and a cell found on the small CIFAR-10 dataset transferred to ImageNet with state-of-the-art results. Even so, the search took on the order of 2,000 GPU days.

    Evolutionary Approaches

    Evolutionary algorithms were used on neural networks long before deep learning. NEAT, by Kenneth Stanley and Risto Miikkulainen in 2002, evolved both the weights and the topology of small networks. For modern deep networks, evolution usually searches only the architecture and leaves the weights to gradient descent.

    Esteban Real and colleagues at Google published AmoebaNet in 2019, using what they called regularized evolution, also known as aging evolution. It is a tournament-selection algorithm with one change: when a new architecture is added to the population, the oldest one is removed, not the worst. That forces the population to keep producing good architectures instead of relying on a single lucky one that happened to get a high score from a noisy evaluation. In a direct comparison with the reinforcement learning approach on the same search space, evolution reached similar or better accuracy and got there faster. The cost was still around 3,150 GPU days.

    Evolution has a natural advantage when there are several goals. NSGA-Net (Lu and colleagues, 2019) used NSGA-II to search for architectures that trade off accuracy against computational cost, returning a whole set of options instead of one network.

    Weight Sharing and One-Shot Models

    Thousands of GPU days put NAS out of reach for almost everyone outside a few large companies. The idea that changed this was weight sharing. ENAS (Pham and colleagues, 2018) observed that all architectures in a cell-based search space are subgraphs of one large graph containing every possible operation. If that «supernet» is trained once, each candidate architecture can inherit its weights from the supernet instead of being trained from scratch. The paper reported a search cost about 1,000 times lower than the original NAS.

    DARTS (Differentiable Architecture Search), from Hanxiao Liu, Karen Simonyan and Yiming Yang at ICLR 2019, went a step further. Instead of choosing one operation for each connection in the cell, DARTS places all candidate operations there at once and combines their outputs with a softmax over learnable weights. The architecture becomes continuous, so it can be optimized with gradient descent alongside the network weights. After the search, the operation with the largest weight on each edge is kept. DARTS found cells comparable to NASNet and AmoebaNet in 1.5 to 4 GPU days.

    The speed came with problems. Later studies found that DARTS often collapses toward architectures full of skip connections, which are easy to train in the supernet but perform poorly when trained alone. Supernet performance also did not always predict how well an architecture would do when trained on its own. Many variants since then have focused on fixing this instability.

    Hardware-Aware Search

    The best architecture depends on where it will run. A network for a phone has to meet a latency budget, and the number of floating-point operations is a poor proxy for real speed because it ignores memory access and hardware-specific kernels. MnasNet (Tan and colleagues, 2019) included latency measured on actual phones in the search objective. EfficientNet (Tan and Le, 2019) used a similar search to find a small baseline network, then scaled its depth, width and input resolution together with a fixed ratio, producing a family of models that was more accurate than previous networks at every size.

    Once-for-All (Cai and colleagues, 2020) trained a single large network in a way that makes many of its subnetworks usable directly, so a specialized model for a new device can be extracted without retraining. This moves most of the cost into one training run and makes deployment on many devices cheap.

    Is Search Better Than Random?

    As results piled up, a skeptical question appeared: how much of the performance comes from the search algorithm and how much from a well-designed search space? Liam Li and Ameet Talwalkar showed in 2019 that random search with weight sharing was competitive with DARTS and ENAS on standard benchmarks. Yang and colleagues reported in 2020 that on several popular search spaces, randomly sampled architectures already performed very well, and that details of the training pipeline mattered more than the search method.

    To make comparisons fair and cheap, researchers built tabular benchmarks. NAS-Bench-101 (Ying and colleagues, 2019) trained all of about 423,000 unique architectures in a fixed cell space and published their results, so a search algorithm can be tested by looking up accuracies instead of training anything. NAS-Bench-201 and others followed. These benchmarks made it possible to compare algorithms reproducibly, and they also made it clearer that many published gains were small.

    The Cost Question

    NAS also became an example in debates about the environmental cost of AI. In 2019, Emma Strubell, Ananya Ganesh and Andrew McCallum estimated the carbon emissions of training NLP models, and the scenario with the largest footprint was a Transformer found through NAS, estimated at roughly 626,000 pounds of CO2. The figure was widely quoted. In 2021, David Patterson and colleagues at Google argued that it was far too high for the actual search that had been run, because it assumed the full search was performed at the largest model size on older hardware. The disagreement is a useful reminder that estimates of AI’s energy use depend heavily on assumptions about hardware, data centers and exactly what was run.

    Where NAS Stands Now

    NAS produced real, widely deployed models, especially for mobile vision, and weight sharing and hardware-aware objectives are now standard tools. At the same time, the biggest architectural shift of recent years, the move to Transformers and their scaling, came from human design and experiments with scale, not from search. For large models, the practical questions are usually about scaling width, depth and data, and search is applied to smaller choices like the shape of attention blocks, activation functions or the configuration of mixture-of-experts layers. In industry, NAS is most common when a model has to fit a specific device under strict latency, memory or energy constraints.

    Reflection

    NAS started as an effort to have algorithms design networks without human input, and it ended up showing how much the human choices still matter. The search space is designed by people, and it often decides most of the result before the search even begins. The strongest work in the field is honest about that. It treats NAS as a way to search carefully through a space that experts defined, not as a replacement for the experts.

    By: Max Johnson B.