Search

Search papers and lessons

Prior Preservation Loss

Level 4 · 10 min read

Prior preservation loss adds a second training term that keeps a fine-tuned diffusion model producing the general class it came from, so teaching it one subject does not erase the class.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Prior preservation loss is a second training term used when a Diffusion Model is fine-tuned on a few photos of one subject, such as a particular dog. Alongside the loss on those photos, the model is trained on images that the original, frozen model generates from the general class name, such as "a dog". Those generated images anchor the model to the whole class, so it keeps the general meaning of the word and does not copy the training photos. The two terms are added together with a weight that sets how strongly the class is protected.

Builds on: Diffusion Model, Mean Squared Error, Fine-Tuning · Leads to: the DreamBooth paper, where it was introduced

Where it's used

Personal image generation. A person who wants a text-to-image model to draw their own pet in new settings can fine-tune it on four to six photos of the pet. Without protection the model often starts to draw every "dog" as that one animal. With prior preservation the prompt "a dog in a park" still produces other dogs, while the personalised prompt produces the pet.

Product and character design. A brand or game studio may want one product or character to appear consistently in many scenes. The same recipe teaches the model that subject from a handful of reference images, and the class term keeps the surrounding objects and scenes general.

Research on subject-driven generation. The method from the DreamBooth paper (2022) became a baseline for later work, which compares new personalisation methods on how well they keep the subject and how well they keep the class.

How it works

We trace one tiny example with three numbers. The subject is one photo of a dog, and the class is "dog".

  1. Prepare the subject data. Collect the subject images, here one photo xsx_s of the pet. Give the subject a rare identifier word, such as "sks", so the prompt "a sks dog" points to this animal and not to the word "dog" in general. Check: the rare word has almost no meaning in the original model, so training can attach the new subject to it without disturbing other words.
  2. Generate the prior images. Before fine-tuning, run the original, frozen model on the prompt "a dog" and save a set of its outputs xpx_p. These are generic dogs chosen by the model itself. Check: the prior images show what the model believed a dog looked like before anything was changed.
  3. Compute the subject loss. Add noise to xsx_s at a random step, ask the network to predict that noise given the prompt "a sks dog", and measure the squared error. Say the error is 0.120.12. Check: this term teaches the new subject, and nothing else.
  4. Compute the prior loss. Add noise to one prior image xpx_p, ask the network to predict that noise given the prompt "a dog", and measure the squared error. Say the error is 0.080.08. Check: this term keeps the general dog where it was.
  5. Add them. With weight λ=1\lambda = 1, the total is 0.12+1×0.08=0.200.12 + 1 \times 0.08 = 0.20. Gradient descent lowers this total, so both terms must go down together. Check: if only the subject term existed, the network could lower its error by making every "dog" look like the pet.
flowchart LR
  F[Frozen original model] -->|prompt: a dog| P[Prior images]
  S[Subject photos, prompt: a sks dog] --> LS[Subject loss]
  P --> LP[Prior loss]
  LS --> T[Total loss]
  LP --> T
  T --> U[Update the fine-tuned model]

The math

Definition: Prior preservation loss

Let εθ(xt,t,c)\varepsilon_\theta(x_t, t, c) be the network's noise prediction at noisy image xtx_t, step tt and prompt cc. The total loss adds the subject term and a weighted prior term:

L=Lsubject+λ Lprior,\mathcal{L} = \mathcal{L}_{\text{subject}} + \lambda\, \mathcal{L}_{\text{prior}},

where λ≥0\lambda \ge 0 is a weight. The subject term uses the subject photos and the rare-identifier prompt, and the prior term uses the frozen model's own samples and the class prompt.

Step 1 in symbols: the two datasets

Let Ds\mathcal{D}_s be the subject images, each paired with the prompt csc_s containing the identifier. Let Dp\mathcal{D}_p be the prior images, generated before training by the frozen model θ0\theta_0 from the class prompt cpc_p, so xp∼pθ0(x∣cp)x_p \sim p_{\theta_0}(x \mid c_p). The class prompt cpc_p is the same without the identifier.

Step 3 in symbols: the subject loss

Using the noise-prediction form from Diffusion Model, with xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon,

Lsubject=Exs∼Ds,  ε,  t∥ε−εθ(xt,t,cs)∥2.\mathcal{L}_{\text{subject}} = \mathbb{E}_{x_s \sim \mathcal{D}_s,\; \varepsilon,\; t}\left\| \varepsilon - \varepsilon_\theta(x_t, t, c_s) \right\|^2.

Here ε\varepsilon is the noise, tt is a random step, and ∥⋅∥2\|\cdot\|^2 is the squared size of the error. This is the same Mean Squared Error as in ordinary diffusion training, applied to subject images only.

Step 4 in symbols: the prior loss

The prior term has the same form, but its data and prompt come from the class:

Lprior=Exp∼Dp,  ε′,  t′∥ε′−εθ(xt′′,t′,cp)∥2.\mathcal{L}_{\text{prior}} = \mathbb{E}_{x_p \sim \mathcal{D}_p,\; \varepsilon',\; t'}\left\| \varepsilon' - \varepsilon_\theta(x'_{t'}, t', c_p) \right\|^2.

The noisy input xt′′x'_{t'} is made from xpx_p and its own noise ε′\varepsilon' in the same way. It is the ordinary noise-prediction loss of the diffusion model, applied to prior images only.

Step 5 in symbols: the combined objective

The full objective is the sum above, with the two expectations taken on each training step. For a batch of BsB_s subject samples and BpB_p prior samples, each term is averaged over its own batch, so λ\lambda alone sets the balance. Because the prior images come from the original model, pulling them toward the subject would raise the prior term, so the sum keeps the two goals in tension.

Worked examples

Example
Example 1: adding two errors by hand

A training step gives a subject squared error of 0.250.25 and a prior squared error of 0.040.04. Find the total loss for λ=1\lambda = 1, then for λ=0.5\lambda = 0.5.

  1. With λ=1\lambda = 1: 0.25+1×0.04=0.290.25 + 1 \times 0.04 = 0.29.
  2. With λ=0.5\lambda = 0.5: 0.25+0.5×0.04=0.25+0.02=0.270.25 + 0.5 \times 0.04 = 0.25 + 0.02 = 0.27.
  3. Interpretation: a smaller λ\lambda lowers the total, but it also gives the class term less influence on the gradient. A very small λ\lambda offers little protection, and a very large one slows the learning of the subject.
Example
Example 2: a batch of two subject and two prior samples

A batch has two subject images with squared errors 0.300.30 and 0.100.10, and two prior images with squared errors 0.050.05 and 0.150.15. Use λ=1\lambda = 1 and average each term over its batch.

  1. Subject average: (0.30+0.10)/2=0.20(0.30 + 0.10) / 2 = 0.20.
  2. Prior average: (0.05+0.15)/2=0.10(0.05 + 0.15) / 2 = 0.10.
  3. Total: 0.20+1×0.10=0.300.20 + 1 \times 0.10 = 0.30.
  4. Interpretation: the two terms enter the gradient equally in this case. If the prior average rose to 0.400.40 while the subject average stayed at 0.200.20, the total would rise to 0.600.60, and the update would push the model to lower the prior error first.
Example
Example 3: a pet photo model, checked after training

A team fine-tunes a text-to-image model on five photos of their dog Biscuit, using the identifier "sks". Before training they generate 200 images from the prompt "a dog" with the frozen model. Each training step uses one subject photo and one prior image, with λ=1\lambda = 1. On one step the subject loss is 0.120.12 and the prior loss is 0.080.08.

  1. Total loss for this step: 0.12+1×0.08=0.200.12 + 1 \times 0.08 = 0.20.
  2. Over many steps, the average total falls, and the subject loss falls faster than the prior loss.
  3. After training, sample from the prompt "a sks dog on a beach". The output should show the subject in a new setting.
  4. Sample from the prompt "a dog on a beach". The output should show a generic dog, not Biscuit, because the prior term kept that meaning intact.
  5. If the second prompt returns Biscuit, the prior term was too weak or too few steps were taken, and the team should raise λ\lambda or check the prior set.
  6. Interpretation: the test that matters is the pair of prompts, not the training loss. Low training loss on the five photos alone does not show that the class survived.

Try it: keep λ=2\lambda = 2, and suppose the subject loss is 0.100.10 and the prior loss is 0.070.07. What is the total loss?

Answer: 0.10+2×0.07=0.10+0.14=0.240.10 + 2 \times 0.07 = 0.10 + 0.14 = 0.24.

Common mistakes and tradeoffs

It is tempting to think that any extra images will protect the class. In fact the prior images must come from the frozen original model with the class prompt. Real photos of other dogs would teach a different distribution, and the model would drift toward those photos rather than its own sense of a dog.

It is tempting to think that if the subject fits well, the class is safe. In fact the subject loss can fall while the class meaning shrinks, because the model learns to paint every dog as the subject. The only reliable check is to sample from the plain class prompt after training.

It is tempting to think that prior preservation removes overfitting. In fact it reduces the problem but does not remove it. Long training on a few photos still memorises poses and backgrounds, so the number of steps and the variety of the subject photos still matter. This is the general Overfitting problem in a new setting.

When to use it: when you fine-tune a large diffusion model on a few images of one subject and need the model to keep the class for other prompts.

When not to use it: when you have many varied images of the target, or when you want to change the style of the whole model rather than add one subject. Then ordinary fine-tuning or a transfer learning recipe may be simpler.

Alternatives: textual inversion learns only a new word embedding and leaves the network weights unchanged, which limits drift but gives weaker fidelity. Low-rank adapters train far fewer parameters, which lowers the risk of forgetting. Both can be combined with a prior term.

History

Diffusion models for images became strong from 2020, following DDPM, and by 2022 large text-to-image models such as Imagen and Stable Diffusion could generate images from written prompts. Users quickly wanted to add their own subjects, but fine-tuning those models on a few photos caused the drift and overfitting described above.

In 2022 Ruiz and colleagues introduced DreamBooth. They combined a rare identifier word with the prior preservation loss, generating class images from the frozen model and training on them alongside the subject photos. The method caught on because it required only a few images and a short training run, and because the class term gave a simple, testable way to check whether the model had kept its general knowledge.

Recap

  • Fine-tuning on a few subject photos can teach the new subject and erase the class it belongs to.
  • Prior preservation generates class images from the frozen model before training and adds their noise-prediction loss to the subject loss.
  • The weight λ\lambda balances the two terms, and both must fall together during training.
  • The check is the class prompt after training, not the training loss.
  • Read next: the DreamBooth paper, which introduced the term, and Classifier-Free Guidance, which controls how strongly a prompt shapes the image.

Check yourself

QuestionRecall: what are the two datasets in prior preservation loss, and where does each come from?Show answer

The subject dataset is the few photos of the specific subject, paired with a prompt containing a rare identifier. The prior dataset is images generated by the frozen original model from the general class prompt, created before fine-tuning starts.

QuestionComputation: a training step gives a subject loss of 0.360.36 and a prior loss of 0.100.10. Find the total loss with λ=0.5\lambda = 0.5.Show answer

The total is 0.36+0.5×0.10=0.36+0.05=0.410.36 + 0.5 \times 0.10 = 0.36 + 0.05 = 0.41.

QuestionWhy: why would a model trained only on subject photos, with no prior term, tend to lose the meaning of the word dog?Show answer

The subject loss is lowest when every dog-like image is pulled toward the subject, because the training photos are the only target the loss ever sees. Nothing in the objective asks the model to keep other dogs, so the shared word "dog" drifts toward the one animal in the photos.

QuestionApplied: after training a model with prior preservation, a user prompts 'a dog in the snow' and gets only the subject. What went wrong, and what would you check first?Show answer

The prior term was too weak or absent, so the class meaning was not protected. Check first whether the prior images were generated from the frozen model with the class prompt, then whether λ\lambda was greater than zero, and whether the training ran for too many steps on too few subject photos.

Sources