Search

Search papers and lessons

Feature Matching Loss

Level 3 · 11 min read

Feature matching trains a GAN generator to match the average of an intermediate discriminator layer's features on real and fake data, a steadier signal than fooling the final output.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Neural network basics
  • Generative models
  • ReadReady to readNot yet

TL;DR

In a GAN, the generator is usually trained to make the discriminator's final score say "real" for its fake samples. Feature matching changes that target. The generator is trained so that the average of an intermediate layer's features on its fake samples equals the average of the same features on real data. The discriminator's features then guide the generator with a steadier signal, and the generator no longer chases one final score.

Builds on: Generative Adversarial Network, Gradient Descent · Leads to: HiFi-GAN and other GAN recipes that use it as one term

Where it's used

Semi-supervised image classification. Salimans and colleagues introduced feature matching in 2016 as one of several techniques for training GANs. They used it in a setup where the discriminator also classifies images, and reported results on image benchmarks such as MNIST, SVHN and CIFAR-10. The matching term kept the generator's samples useful as training signal for the classifier.

Neural speech synthesis. HiFi-GAN is a vocoder that turns a mel spectrogram into a waveform. Its training combines an adversarial loss, a mel-spectrogram reconstruction loss and a feature matching loss. The feature matching term compares the feature maps of real and generated audio, layer by layer, so the generated waveform must match the real one in the internal patterns the discriminator sees.

Stabilising image GANs. Many GAN recipes add feature matching to the usual adversarial loss, because the usual loss can give weak or unstable gradients once the discriminator becomes confident. Feature matching gives the generator a target that stays informative in that case.

How it works

Here is one tiny instance traced from start to finish. The discriminator has an intermediate layer with one unit, so each sample has one feature number. This layer is the part of the discriminator that we match. The features of a batch of real samples are 1, 2 and 3, so their mean is 2. The generator's current fake samples have features 0, 0.5 and 1.5, so their mean is 1.

  1. Compute the real mean. The real features are 1, 2 and 3, and their average is (1+2+3)/3=2(1 + 2 + 3)/3 = 2. Check: this is the target the generator should reach.
  2. Compute the fake mean. The fake features are 0, 0.5 and 1.5, and their average is (0+0.5+1.5)/3=1(0 + 0.5 + 1.5)/3 = 1. Check: the fake samples are too low in this feature.
  3. Measure the gap and square it. The gap is 2−1=12 - 1 = 1, so the loss is 12=11^2 = 1. Check: a gap of 0 would give a loss of 0.
  4. Find the direction to move. The loss falls as the fake mean rises. Its slope with respect to the fake mean is −2×(2−1)=−2-2 \times (2 - 1) = -2, and in this tiny example we treat how the generator's parameters move the mean as 1. Check: a negative slope means increasing the fake mean lowers the loss.
  5. Update the generator only. With learning rate η=0.2\eta = 0.2, the step changes the fake mean by 0.2×2=0.40.2 \times 2 = 0.4, from 1 to 1.4. The discriminator's weights stay as they are during this step. Check: the new fake mean is 1+0.4=1.41 + 0.4 = 1.4.
  6. Recompute the loss. The gap is now 2−1.4=0.62 - 1.4 = 0.6, and the loss is 0.62=0.360.6^2 = 0.36, down from 1. Check: the generator has moved toward the real feature mean.

The diagram below shows how the two halves of the loss meet.

flowchart LR
  R[Real samples] --> D1[Discriminator layer features]
  Z[Noise] --> G[Generator]
  G --> F[Fake samples] --> D2[Discriminator layer features]
  D1 --> M[Compare means]
  D2 --> M
  M --> L[Squared gap, used to update the generator]

The math

Definition: Feature matching loss

Let f(⋅)f(\cdot) be the activations of one intermediate layer of the discriminator DD, and let GG map noise zz to a sample. The generator minimises

LFM=∥Ex∼pdata[f(x)]−Ez∼pz[f(G(z))]∥22,\mathcal{L}_{\text{FM}} = \left\lVert \mathbb{E}_{x \sim p_{\text{data}}}\big[f(x)\big] - \mathbb{E}_{z \sim p_z}\big[f\big(G(z)\big)\big] \right\rVert_2^2,

where ∥⋅∥22\lVert \cdot \rVert_2^2 is the squared Euclidean norm. In words: the average feature vector of real data should equal the average feature vector of fake data.

Step 1 in symbols: the real feature mean

Write μreal=Ex∼pdata[f(x)]\mu_{\text{real}} = \mathbb{E}_{x \sim p_{\text{data}}}[f(x)]. In practice the expectation is estimated by averaging over a batch of real samples. In the walkthrough, μreal=(1+2+3)/3=2\mu_{\text{real}} = (1 + 2 + 3)/3 = 2.

Step 2 in symbols: the fake feature mean

Write μfake=Ez∼pz[f(G(z))]\mu_{\text{fake}} = \mathbb{E}_{z \sim p_z}[f(G(z))], estimated the same way from a batch of generated samples. In the walkthrough, μfake=(0+0.5+1.5)/3=1\mu_{\text{fake}} = (0 + 0.5 + 1.5)/3 = 1.

Step 3 in symbols: the gap

The loss is LFM=∥μreal−μfake∥22\mathcal{L}_{\text{FM}} = \lVert \mu_{\text{real}} - \mu_{\text{fake}} \rVert_2^2. With one feature, the norm is an absolute value, so the loss is (2−1)2=1(2 - 1)^2 = 1.

Step 4 in symbols: the gradient for the generator

By the chain rule, the gradient with respect to the generator's parameters θG\theta_G is

∂LFM∂θG=−2(μreal−μfake)⊤∂μfake∂θG.\frac{\partial \mathcal{L}_{\text{FM}}}{\partial \theta_G} = -2\left(\mu_{\text{real}} - \mu_{\text{fake}}\right)^{\top} \frac{\partial \mu_{\text{fake}}}{\partial \theta_G}.

The factor ∂μfake/∂θG\partial \mu_{\text{fake}}/\partial \theta_G is the way the generator's parameters move the fake feature mean. In the walkthrough, the first part is −2×1=−2-2 \times 1 = -2, so the generator's step raises μfake\mu_{\text{fake}}.

Step 5 in symbols: the update

Gradient descent gives θG←θG−η ∂LFM/∂θG\theta_G \leftarrow \theta_G - \eta \,\partial \mathcal{L}_{\text{FM}}/\partial \theta_G. This updates only the generator. The discriminator's features are treated as fixed for this step, and the discriminator is trained with its own loss in a separate step. In the walkthrough, the fake mean moves from 1 to 1.4, and the loss falls from 1 to 0.36.

Worked examples

Example
1. Tiny: a two-unit feature layer

The discriminator layer has two units. The real mean is μreal=(1,3)\mu_{\text{real}} = (1, 3) and the fake mean is μfake=(0,1)\mu_{\text{fake}} = (0, 1).

  1. The difference is μreal−μfake=(1,2)\mu_{\text{real}} - \mu_{\text{fake}} = (1, 2).
  2. The squared norm is 12+22=51^2 + 2^2 = 5, so LFM=5\mathcal{L}_{\text{FM}} = 5.
  3. The gradient with respect to μfake\mu_{\text{fake}} is −2×(1,2)=(−2,−4)-2 \times (1, 2) = (-2, -4).
  4. Interpretation: both features of the fake samples are too low, and the second is the worse of the two. The generator is pushed hardest on that second feature.
Example
2. Standard: a batch of four scalar features

A batch of four real samples has scalar features 2, 4, 6 and 8. A batch of four fake samples has features 1, 3, 3 and 5. The numbers are chosen for hand arithmetic.

  1. Real mean: (2+4+6+8)/4=5(2 + 4 + 6 + 8)/4 = 5.
  2. Fake mean: (1+3+3+5)/4=3(1 + 3 + 3 + 5)/4 = 3.
  3. Gap: 5−3=25 - 3 = 2, so LFM=22=4\mathcal{L}_{\text{FM}} = 2^2 = 4.
  4. Interpretation: the fake batch as a whole sits 2 below the real batch, even though its individual values overlap with the real ones. The loss targets the average, not each sample.

Try it: the generator changes so its fake features become 3, 5, 5 and 7. What is the new loss? Answer: the fake mean is (3+5+5+7)/4=5(3 + 5 + 5 + 7)/4 = 5, which equals the real mean, so the loss is 02=00^2 = 0.

Example
3. Realistic: a layer-by-layer audio comparison

A vocoder's discriminator has several layers, and each layer produces a feature map for real audio and for generated audio. Here the loss compares the two maps element by element instead of comparing means: each position contributes an absolute difference, and the differences are averaged within the layer. This per-element form is the one used in practice for speech, so it differs from the mean-based definition above. The numbers here are invented for illustration.

  1. Take one layer whose feature map has four values. The real audio gives (1,2,3,4)(1, 2, 3, 4), and the generated audio gives (2,2,1,4)(2, 2, 1, 4).
  2. Absolute differences: ∣1−2∣=1|1 - 2| = 1, ∣2−2∣=0|2 - 2| = 0, ∣3−1∣=2|3 - 1| = 2 and ∣4−4∣=0|4 - 4| = 0.
  3. Their sum is 3, and averaging over the 4 values gives 3/4=0.753/4 = 0.75 for this layer.
  4. Interpretation: the generated audio matches the real audio at two of the four positions. The first and third positions carry the error, and the generator is pushed to change those.
  5. The total feature matching term adds such layer terms across the discriminator, and the training objective combines it with the adversarial and reconstruction losses.

Try it: a second layer has real values (0,2)(0, 2) and generated values (1,2)(1, 2). What is its averaged term? Answer: the absolute differences are 11 and 00, the sum is 1, and averaging over 2 values gives 0.50.5.

Common mistakes and tradeoffs

It is tempting to think that matching the mean makes the samples realistic. In fact the basic loss matches only the average of one layer's features. Two different distributions can share a mean, so the feature matching term is a stabiliser and not a guarantee. The adversarial loss is still needed to shape the details.

It is tempting to think feature matching replaces the adversarial loss. In fact it is usually one term combined with others, as in the vocoder example. Used alone, it can give samples that have the right average feature but poor detail.

It is tempting to think any intermediate layer will do. In fact the layer decides what is matched, and choosing intermediate layers to supervise is the idea behind Deep Supervision. Early layers capture local texture and low-level patterns, and deeper layers capture more global structure. The layer should be chosen for what it encodes, and the choice can be tuned on validation samples.

It is tempting to think the discriminator's features are a fixed standard. In fact the discriminator keeps training, so its features change over time. Matching a moving target is usually still useful, but the training dynamics differ from matching a fixed reference.

Generator objectiveWhat it asksTypical weakness
Fool the final discriminator scoreFinal output says realWeak or unstable gradients when the discriminator is confident
Feature matchingIntermediate features have matching averagesMatches only the average, so it needs other terms
Wasserstein Distance objectiveA distance between the distributionsNeeds a constraint on the critic

When to use it. Use feature matching when the adversarial signal is unstable, when a discriminator's features are informative, or when a structured output such as audio must match real data in its internal patterns.

When not to use it. Avoid relying on it alone when fine detail matters, and avoid it when there is no sensible intermediate layer to match.

History

GANs were introduced in 2014 by Goodfellow and colleagues. The original paper showed that a generator and discriminator trained against each other could produce convincing images, but training was often unstable, and the generator could collapse onto a few kinds of output.

In 2016 Salimans and colleagues gathered several training techniques, including feature matching, in one paper. Feature matching was proposed as a more stable target for the generator, and the paper reported results in semi-supervised classification. The idea spread because it is a small change to the loss, and because it fits any network that has intermediate features. It is a close relative of Hint-Based Training, where a student network is trained to match a hidden layer of a teacher. Later, speech systems such as HiFi-GAN adopted it as one part of a combined loss.

Recap

  • Feature matching trains the generator so that the average of an intermediate discriminator layer's features matches on real and fake data.
  • The loss is the squared Euclidean norm of the difference between the two feature means.
  • The generator is updated while the discriminator's features are held fixed for that step.
  • It gives a steadier signal than the final score, but it matches only an average, so it is combined with the adversarial loss.
  • Read next: HiFi-GAN for a speech system that uses it, and Wasserstein Distance for a different way to stabilise GAN training.

Check yourself

QuestionRecall: what does feature matching ask the generator to match, and on which data?Show answer
It asks the generator to make the average of an intermediate discriminator layer's features on its fake samples equal the average of the same features on real samples.
QuestionComputation: real features are 4, 6 and 8, and fake features are 1, 2 and 6. What is the feature matching loss?Show answer
The real mean is (4+6+8)/3=6(4 + 6 + 8)/3 = 6, and the fake mean is (1+2+6)/3=3(1 + 2 + 6)/3 = 3. The gap is 6−3=36 - 3 = 3, so the loss is 32=93^2 = 9.
QuestionWhy: why might matching an intermediate layer give a more useful signal than only trying to change the discriminator's final score?Show answer
When the discriminator becomes confident, its final score on fake samples can saturate, so the gradient that reaches the generator is weak. An intermediate layer still carries graded information about how real and fake samples differ, so the generator gets a signal that stays informative for longer.
QuestionApplied: a team uses feature matching alone, with no adversarial term, and the samples have the right average look but no fine detail. What is the likely cause, and what should they change?Show answer
The loss matches only an average of one layer's features, which many different samples can share. The generator has no pressure to reproduce the details that the adversarial loss would enforce. They should add the adversarial loss back as a term, keep feature matching as a stabiliser, and check that the chosen layer captures the detail they need.

Sources