What is a diffusion model?
dear reader,
I recently found this paper on arxiv that talks about something called guidance and variation matching in diffusion models. Naturally, I asked myself, what exactly is a diffusion model?
So what is a diffusion model?
A diffusion model is a type of generative model that is used to generate data like text, video, audio, images, etc. These models are different from more traditional machine learning models since they are taught to remove noise from an image rather than build one from scratch.
The models that we interact with daily like GPT and Claude are something called autoregressive models. They essentially predict what comes next based on the previous outputs. Diffusion models work differently. They start with an extremely noisy, unrecognizable image and incrementally remove the noise to create a clear image.
Why go from noise to clear picture?
Learning what something looks like is difficult. Let’s say we want to teach a model what a cat looks like. We run into the issue of figuring out what a cat even is. Is it white or orange? Does it look like Garfield or Hello Kitty or something in your alley? Instead of trying to describe a cat directly, we take real pictures of cats and sprinkle in a known amount of random noise. Then we ask the model to guess what noise we added. Since we know the right answer, we can measure how far off its guess was and nudge it to do better next time. We repeat this over and over, from barely noisy images to images that are pure static.
Here’s the sneaky part. To get good at removing noise from cats, the model has to learn what cats look like. Once it’s trained, we can hand it pure static and let it clean it up step by step until a cat appears.
Now, even I don’t know what a cat means?
There’s still one more question, what does a cat even mean in the context of a model? Every time the model makes a single guess, it plays it safe and leans towards the average of all the right answers. After all, it is trying to please everything from calico to sphynx at once. Thanks to the step-by-step cleanup, the final image still comes out sharp, but it might only loosely look like a cat, or barely like a cat at all. To deal with this, we can add a new variable that asks the model to exaggerate a cat.
Now, instead of a vaguely cat-ish image, we get one that leans into cat-like features like pointy ears and whiskers.
So what is this mystery variable?
To understand this, first let’s look at how the diffusion model works.
Once the prompt is encoded into embeddings, those embeddings steer the model while it removes noise. The model uses the prompt as a hint about what it should be cleaning the static up into.
At every step, the model actually makes two guesses. One uses our prompt (“a cat”) and one uses an empty prompt, which is basically the model’s idea of a generic image. The difference between the two guesses is the direction that points towards “cat”.
The mystery variable, w, is called the guidance scale. It decides how far we push in that direction:
guess = empty_prompt_guess + w * (cat_guess - empty_prompt_guess)
- When
w = 1, we just use the regular cat guess. - When
wis bigger than 1, we push past it, exaggerating the cat’s features to create a more defined image. - When
wis too big, we overshoot the mark and end up with an oversaturated, cartoonish cat, and every cat starts to look the same.
So w is a dial between a dull, barely-a-cat image and a cartoon. Picking the right value is what keeps the cat looking like a cat.
xo, Aadhya