1. Home
  2. Explore
  3. Computer Science
  4. Diffusion Models and Brownian Motion

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Diffusion Models and Brownian Motion

Speaker 1Speaker 2

Diffusion Models and Brownian Motion

18:00
0:00-18:00
  1. Michael

    Text-to-video systems can now produce strikingly realistic clips, but beneath the surface is an idea with a surprising connection to physics: diffusion. We’ll build that connection step by step, then see how text prompts steer the process.

  2. Anna

    And we’ll begin with what a modern video model actually does. In the example with the open-source model WAN 2.1, generation starts by asking a random number generator for a video made of random pixel values. At first, it looks like pure visual noise.

  3. Michael

    That noise goes into a transformer—the same general type of model used in large language models, though here it produces video rather than text. The output is still noisy, but it has a little more structure. The model’s output is combined with the current video, and the result is fed through the model again.

  4. Anna

    Repeat that many times and the structure builds up: after a sequence of denoising steps, the random pixels become a coherent video. And the prompt can influence what emerges. A longer prompt might ask for an astronaut holding a flag, a laptop, or a meeting; with a shorter prompt, there’s less specific guidance.

  5. Michael

    Even an empty prompt can still produce a video, because the model can generate something from its learned data without a particular text description. That example raises two big questions: what does this iterative noise-removal process have to do with Brownian motion, and how does language guide it?

  6. Anna

    We’ll need two pieces to answer that. First is a shared way to represent images and words, illustrated by CLIP. Second is the diffusion process itself: how models learn to reverse noise, why the training and sampling algorithms are less simple than they might seem, and how text guidance gets added.

  7. Michael

    Let’s start with CLIP, introduced by OpenAI in 2021. It has two encoders: one processes images, and one processes text. Each turns its input into a vector with 512 values. The goal is to make an image vector similar to the vector for its matching caption, while making it dissimilar to vectors for unrelated captions.

  8. Anna

    The training trick is to use all the pairings in a batch, not just the correct ones. Imagine a batch with pictures of a cat, a dog, and a man, and captions saying “a photo of a cat,” “a photo of a dog,” and “a photo of a man.” The matching pairs lie along the diagonal of an image-caption similarity table; the other pairings are mismatches.

  9. Michael

    CLIP’s contrastive objective pushes the matching pairs to have high similarity and the mismatches to have low similarity. That’s the “C” in CLIP: contrastive. Similarity is measured with cosine similarity, which compares the directions of two vectors. If they point in the same direction, their angle is zero and their cosine similarity is one.

  10. Anna

    So CLIP learns a shared, high-dimensional space—also called an embedding space or latent space—in which related pictures and phrases align. The geometry can represent meaningful differences, not just whole images. For example, take one image vector of a person wearing a hat and subtract the vector of the same person without a hat.

  11. Michael

    That difference vector can be compared with text vectors. In the example, among a few hundred common words, “hat” was the closest match, followed by “cap” and “helmet.” That’s a striking demonstration that vector arithmetic can capture a change in visual content as a direction in the shared space.

  12. Anna

    CLIP can also classify an image without a separate classifier for every possible label. Encode the image, encode candidate captions such as “a photo of a cat” or “a photo of a dog,” and select the caption whose vector has the highest cosine similarity to the image vector.

  13. Michael

    But there’s an important limit: CLIP’s encoders map images and text into vectors. They don’t, by themselves, turn a vector back into an image. That’s why a shared representation is useful for matching and understanding, but not sufficient for image generation.

  14. Anna

    The generation story took a major step forward with the 2020 paper Denoising Diffusion Probabilistic Models, or DDPM. Its basic setup is to take training images and gradually add noise until their original content is destroyed, then train a neural network to help reverse the process.

  15. Michael

    A tempting first guess is to train the model to undo one noise step at a time: given the image after step two, predict step one; given step three, predict step two; and so on. Then, at generation time, start from pure noise and repeatedly make the image a little cleaner.

  16. Anna

    But that simple picture misses two important parts of DDPM. First, the method adds random noise during generation, too. At each step, the model predicts a less noisy result, and then the algorithm adds scaled random noise before continuing. In the paper’s approach, that noise is not an optional flourish.

  17. Michael

    You can see why that matters in an example with Stable Diffusion 2. Using DDPM sampling to generate a tree in a desert can produce a good image. Remove the random-noise addition from that same process, and the result can collapse into a tiny, blurry tree.

  18. Anna

    The second surprise is the training target. Rather than teaching the model to reverse just one step, DDPM starts with a clean image, called x-zero, adds scaled random noise, called epsilon, and trains the network to predict the total noise that was added.

  19. Michael

    That may sound like a harder question: ‘What was all the noise?’ rather than ‘How do I undo this one little step?’ The physics perspective helps explain why this training target is useful, and why adding random noise again during generation helps produce better results.

  20. Anna

    Let’s make the geometry visible with a toy example. An image can be treated as a point in a high-dimensional space, with each pixel intensity corresponding to a coordinate. If we shrink an image to two pixels, we can draw it in two dimensions: one axis for the first pixel’s intensity and another for the second.

  21. Michael

    For instance, an image with a black first pixel and a white second pixel is the point zero, one; an all-white image is one, one. Real images occupy structured parts of this space. For a simple illustration, imagine our data points arranged in a spiral.

  22. Anna

    Adding random noise changes each pixel by a random amount. In the two-dimensional picture, that means taking a step in a random direction. Keep adding noise and the point wanders through a random walk. That is closely related to Brownian motion—the kind of random movement associated with particles diffusing—and it’s the physical process that gives diffusion models their name.

  23. Michael

    The model is trained on many such noisy paths and learns how to reverse them: begin at a random-looking point and move toward the structure of the original data. In that sense, it learns to play the diffusion process backward, in a high-dimensional space.

  24. Anna

    Consider the final point in a 100-step random walk. A one-step training objective would ask the model to predict the point at step 99 from the point at step 100. Any individual last step is random, but across many walks there’s a pattern: on average, the paths have been diffusing away from the original spiral.

  25. Michael

    DDPM’s noise-prediction objective instead asks for the total noise added along the walk. Geometrically, that points from the final point back toward the original starting point. Under the relationship described in the source, learning to predict the final-step noise is mathematically equivalent to learning the total noise divided by the number of steps.

  26. Anna

    So the model’s ultimate job is still to learn how to move back toward the data. But predicting the total noise gives it a training signal with lower variance than trying to infer a single random step. That can make the learning process more efficient without changing the underlying objective.

  27. Michael

    At each location, the learned direction toward more likely, less noisy data is called a score function. But the right direction depends on how much noise has been added. Far from the spiral, a useful direction may point generally toward its center; closer to it, the model needs to understand the spiral’s finer shape.

  28. Anna

    That’s why the model also receives a time value, representing how far along the noise process it is. At a large time value it can learn a coarse direction; as time approaches zero, it can learn more detailed structure. Conditioning on time lets one model represent a changing vector field rather than a single fixed set of directions.

  29. Michael

    A visualization of that field can show a striking shift: as time changes, the directions can go from pointing toward the center of the spiral to pointing toward the spiral itself. It can look like a phase change. That changing field is a useful way to understand what the model has learned.

  30. Anna

    Now we can return to the mystery of random noise during generation. In DDPM sampling, start from a random location, take a small step guided by the model’s vector field, then add a scaled random step. Repeat. The random steps may make the path look chaotic at first, but the step sizes decrease as the process continues, and the points can end up distributed along the spiral.

  31. Michael

    If we instead remove those random steps from the DDPM procedure without making any other changes, many points can move toward the center or average of the spiral and then toward just part of it. That means less diversity. In image space, averaging different plausible images often looks blurry.

  32. Anna

    There’s a caveat to the toy analogy. If every point on our spiral corresponds to a realistic image, then points that still land on the spiral should also look realistic. But real image generation is more complicated: generated points may not reach the manifold of realistic images, and the average of different images can be an unrealistic blur.

  33. Michael

    The theory gives a reason random sampling matters. With Gaussian noise in the forward process, and sufficiently small steps, the reverse process can be described by a Gaussian distribution. The model learns the mean of that distribution, conditioned on the current point and time.

  34. Anna

    But a mean is not a complete sample. To draw from the learned Gaussian distribution, you also need zero-mean Gaussian noise. DDPM adds random noise after the model’s prediction, which is precisely the sampling step that helps prevent every trajectory from collapsing onto an average.

  35. Michael

    This averaging effect is especially clear early in reverse diffusion, when the noisy examples are far from the data. The learned directions can point toward the average of the whole dataset. The added noise lets the process explore different possibilities rather than forcing all generated results toward that same mean.

  36. Anna

    DDPM showed that diffusion could generate high-quality images, but it required many steps, and every step meant another full pass through a potentially large neural network. That made generation computationally expensive.

  37. Michael

    Then work from teams at Stanford and Google used a result from statistical mechanics to show how to get the same final distribution without random steps along the way. DDPM generation can be represented by a stochastic differential equation: one part describes movement guided by the model, and another part describes random motion.

  38. Anna

    The Fokker-Planck equation describes how a distribution of points evolves under that stochastic motion. The Google Brain team showed that an ordinary differential equation, with no random component, can produce the same final distribution. This gives a deterministic route to the same distribution of generated images.

  39. Michael

    That sampling approach is generally called DDIM. It resembles DDPM generation, but changes the step-size scaling and removes the random noise addition at each step. Those step sizes matter: simply deleting the random steps from DDPM is not the same as switching to DDIM.

  40. Anna

    In the spiral illustration, DDIM’s step scaling helps trajectories follow the contours of the vector field and land on the distribution, rather than collapsing toward the center as in the naive noise-removal experiment. Applied to the desert-tree example, it can produce good images in significantly fewer steps, without changing how the model was trained.

  41. Michael

    And ‘the same result’ here means the same final distribution, not that DDPM and DDIM must generate identical individual images. One samples stochastically and the other deterministically, but the theory says their final distributions can match. WAN 2.1 uses a generalization of DDIM called flow matching.

  42. Anna

    We have generation, and we have a way to represent text and images, but we still need to connect prompts to the generation process. A natural idea is to encode a prompt with CLIP’s text encoder and use its vector to guide a diffusion model toward an image or video that matches the prompt.

  43. Michael

    OpenAI’s 2022 method unCLIP did something related: it trained a diffusion model using image-caption pairs to invert the CLIP image encoder. The model is better known by its commercial name, DALL·E 2, and the approach achieved a striking level of detail and prompt adherence.

  44. Anna

    One way to use a text vector is conditioning. Give that vector to the diffusion model as additional input and train the model to remove noise from images paired with captions. The idea is that the caption provides context, helping the model denoise toward an image that fits the text.

  45. Michael

    This is conceptually similar to giving the toy model its time value: extra information helps it choose the right behavior. There are different ways to supply text information, including cross-attention mechanisms that connect image and text features, appending or adding the text vector to the model input, or combining approaches.

  46. Anna

    But conditioning alone may not make the prompt influence strong enough. In the Stable Diffusion desert example, conditioning on a prompt asking for a tree can give you a desert and a shadow, but no tree. So we need a way to amplify the effect of the prompt.

  47. Michael

    That brings us back to the spiral, now divided into classes: imagine the inner region represents people, the middle region dogs, and the outer region cats. Train a model on coordinates, time, and class labels, and it can learn to guide points toward the appropriate part of the spiral.

  48. Anna

    Yet the class-conditioned model may not fit each class well. It is trying to point toward the data distribution in general and toward a particular class at the same time. The general task can overpower the class-specific signal, so a point may head in the right direction but fail to reach the intended region.

  49. Michael

    Classifier-free guidance addresses this by comparing two predictions: one from a model given the class or prompt, and one from the same model without that information. Rather than training two entirely separate models, training examples can omit the class information for a subset of the data.

  50. Anna

    The unconditioned prediction points toward the data generally; the conditioned prediction points toward the data with the requested class. Subtract the unconditioned vector from the conditioned one to isolate the class-specific difference, then multiply that difference by a scale and add it to the unconditioned prediction.

  51. Michael

    In the spiral example, the unconditioned field may point toward the center, while the cat-conditioned field turns toward the cat region. Amplifying their difference makes the cat-specific direction stronger, so a point that previously fell short can reach the intended part of the spiral.

  52. Anna

    The same idea works for dogs or people: each class has its own conditioned direction, and guidance amplifies the difference between that direction and the general one. This method is called classifier-free guidance, and it has become an important part of many modern image and video generators.

  53. Michael

    In the desert example, increasing the guidance scale can make the requested tree appear and then become larger or more detailed. The model is being steered more strongly toward features associated with the prompt. The scale gives a way to adjust that influence.

  54. Anna

    WAN takes a related idea further with negative prompts. Instead of comparing the conditioned prediction with a prediction that has no text, the model can use a description of what to avoid. It subtracts the resulting direction from the positive-prompt output and amplifies the difference, steering away from unwanted features.

  55. Michael

    WAN’s standard negative prompt includes examples such as extra fingers and walking backwards, and the prompt is passed to its text encoder in Chinese. The source also shows how a video made without that negative prompt can have cartoonish parts that don’t fit together as well.

  56. Anna

    So the full picture is a blend of ideas. CLIP helps place language and images in a shared space; diffusion learns a time-dependent direction for moving from noise toward data; and conditioning plus guidance makes that movement respond more strongly to what we ask for—or steer away from what we don’t want.

  57. Michael

    The connection to physics is more than a metaphor: Brownian motion helps explain the noisy paths, while probability theory gives algorithms for sampling from the learned distribution. Those ideas also led to faster methods such as DDIM and, in WAN’s case, flow matching.

  58. Anna

    And the geometric intuition remains valuable even though real images live in enormously high-dimensional spaces. It helps explain why models need time information, why a simple mean can look blurry, why random sampling can preserve diversity, and how guidance can emphasize a particular concept.

  59. Michael

    That’s the remarkable result: language can direct a process that starts with random noise and gradually produces complex images or video. The underlying pieces are intricate, but their connections—from vector spaces to Brownian motion to guided sampling—give us a way to understand how the system works.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent