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.
- Compute the real mean. The real features are 1, 2 and 3, and their average is . Check: this is the target the generator should reach.
- Compute the fake mean. The fake features are 0, 0.5 and 1.5, and their average is . Check: the fake samples are too low in this feature.
- Measure the gap and square it. The gap is , so the loss is . Check: a gap of 0 would give a loss of 0.
- Find the direction to move. The loss falls as the fake mean rises. Its slope with respect to the fake mean is , 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.
- Update the generator only. With learning rate , the step changes the fake mean by , from 1 to 1.4. The discriminator's weights stay as they are during this step. Check: the new fake mean is .
- Recompute the loss. The gap is now , and the loss is , 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
Let be the activations of one intermediate layer of the discriminator , and let map noise to a sample. The generator minimises
where 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 . In practice the expectation is estimated by averaging over a batch of real samples. In the walkthrough, .
Step 2 in symbols: the fake feature mean
Write , estimated the same way from a batch of generated samples. In the walkthrough, .
Step 3 in symbols: the gap
The loss is . With one feature, the norm is an absolute value, so the loss is .
Step 4 in symbols: the gradient for the generator
By the chain rule, the gradient with respect to the generator's parameters is
The factor is the way the generator's parameters move the fake feature mean. In the walkthrough, the first part is , so the generator's step raises .
Step 5 in symbols: the update
Gradient descent gives . 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
The discriminator layer has two units. The real mean is and the fake mean is .
- The difference is .
- The squared norm is , so .
- The gradient with respect to is .
- 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.
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.
- Real mean: .
- Fake mean: .
- Gap: , so .
- 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 , which equals the real mean, so the loss is .
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.
- Take one layer whose feature map has four values. The real audio gives , and the generated audio gives .
- Absolute differences: , , and .
- Their sum is 3, and averaging over the 4 values gives for this layer.
- 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.
- 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 and generated values . What is its averaged term? Answer: the absolute differences are and , the sum is 1, and averaging over 2 values gives .
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 objective | What it asks | Typical weakness |
|---|---|---|
| Fool the final discriminator score | Final output says real | Weak or unstable gradients when the discriminator is confident |
| Feature matching | Intermediate features have matching averages | Matches only the average, so it needs other terms |
| Wasserstein Distance objective | A distance between the distributions | Needs 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.