Search

Search papers and lessons

Gradient Reversal Layer

Level 4 · 11 min read

A gradient reversal layer passes its input forward unchanged but flips the sign of the gradient on the way back, so the layers before it learn to work against the signal that follows.

On this page

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

TL;DR

Training a network normally pushes every weight, by Gradient Descent, in the direction that lowers the loss. Sometimes you want one part of the network to push the opposite way from another part, so that its features stop carrying a particular kind of information. A gradient reversal layer is a layer that does nothing in the forward pass and multiplies the gradient by −λ-\lambda in the backward pass, which turns a normal descent into an adversarial one for everything before it.

Builds on: Backpropagation, Domain Adaptation, Minimax Game · Leads to: Domain-Adversarial Training of Neural Networks, Adversarial Training

Where it's used

Domain-adversarial training. In the 2015 work that introduced the layer, a feature extractor is followed by two heads, a label predictor and a domain classifier. The label predictor uses source labels. The domain classifier tries to tell whether a feature came from the source or the target. A reversal layer between the feature extractor and the domain classifier makes the features as hard as possible to assign to a domain, so the classifier that follows can work on target data it has never labeled. The paper tested on image benchmarks where the source and target images look different. Domain Adaptation is the problem this layer was built to attack.

Removing a nuisance variable. The same trick removes information you do not want a representation to carry. If a model should predict a diagnosis but not depend on the patient's sex, a head that predicts sex can be attached through a reversal layer. The feature extractor, which produces a learned representation of the patient, is then pushed to hide sex while still supporting the diagnosis. This is an educational example of the idea, not a clinical method.

Adversarial game training. Generative adversarial networks pit a generator against a discriminator in a Minimax Game, and the gradient reversal layer offers a single-network way to write the same kind of opposition. It is not the only way, but it makes the game explicit inside one backward pass.

How it works

The layer has two passes. In the forward pass it copies its input to its output, so the rest of the network sees the same numbers it would see without the layer. In the backward pass it receives the gradient from the layer after it, multiplies by −λ-\lambda, and passes that to the layers before it. The number λ\lambda is a positive weight that sets how strongly the reversal acts.

We trace one step on a toy. Let the feature extractor have a single parameter θ\theta, and let the domain loss that the features feed into be Ld(θ)=(θ−2)2L_d(\theta) = (\theta - 2)^2. The number 2 stands in for the value the domain classifier would need. Take λ=1\lambda = 1 and a learning rate of 0.10.1. Start from θ=1\theta = 1.

  1. Forward pass. The feature is θ=1\theta = 1. The layer copies it unchanged, and the domain loss is Ld=(1−2)2=1L_d = (1 - 2)^2 = 1. Check: the forward pass has no effect from the layer.
  2. Ordinary gradient. Without the layer, the Chain Rule gives the gradient of LdL_d with respect to θ\theta is 2(θ−2)=−22(\theta - 2) = -2. Descent would move θ\theta by −0.1×(−2)=+0.2-0.1 \times (-2) = +0.2 to 1.21.2, which lowers LdL_d toward its minimum at 2. Check: LdL_d falls from 1 to 0.64.
  3. Reversed gradient. The layer multiplies the gradient by −1-1, so the value sent back is +2+2. Descent moves θ\theta by −0.1×2=−0.2-0.1 \times 2 = -0.2 to 0.80.8. Check: the feature moved away from 2.
  4. Result. The loss at the new point is (0.8−2)2=1.44(0.8 - 2)^2 = 1.44, which is higher than 1. The feature extractor is now working to increase the domain loss, while the domain classifier, which has its own parameters and no reversal on them, keeps trying to lower it.

The toy shows the mechanism: one sign flip changes which way the feature extractor moves. The next section writes the same steps in symbols for a full network.

flowchart LR
    X["Input"] --> F["Feature extractor"]
    F --> Y["Label predictor (source labels)"]
    F --> R["Gradient reversal layer"]
    R --> D["Domain classifier"]

The math

Definition: Gradient reversal layer
For input xx, the layer computes Rλ(x)=xR_\lambda(x) = x in the forward pass. In the backward pass, for an upstream gradient gg, it returns −λg-\lambda g. The parameter λ≥0\lambda \ge 0 is a fixed scalar, and the layer has no weights of its own.

Write θf\theta_f for the feature extractor's weights, θy\theta_y for the label predictor's, and θd\theta_d for the domain classifier's. The label loss LyL_y depends on θf\theta_f and θy\theta_y, and the domain loss LdL_d depends on θf\theta_f and θd\theta_d.

Step 1 in symbols: the forward pass

The features are z=Gf(x;θf)z = G_f(x; \theta_f). The layer passes them on unchanged, Rλ(z)=zR_\lambda(z) = z, so both heads see the same features they would see without the layer.

Step 2 in symbols: the gradient at the layer

Let ∂Ld/∂z\partial L_d / \partial z be the gradient of the domain loss with respect to the features. The layer sends back

∂Ld∂z→−λ ∂Ld∂z\frac{\partial L_d}{\partial z} \to -\lambda \, \frac{\partial L_d}{\partial z}

Because the layer has no weights, this is the only change. Everything after the layer, including the domain classifier, is computed as usual.

Step 3 in symbols: the combined update

The objective that the reversal layer produces, a Minimax Game between the feature extractor and the domain classifier, is

E(θf,θy,θd)=∑iLyi(θf,θy)−λ∑iLdi(θf,θd)E(\theta_f, \theta_y, \theta_d) = \sum_i L_y^i(\theta_f, \theta_y) - \lambda \sum_i L_d^i(\theta_f, \theta_d)

Gradient descent gives the three updates, with learning rate μ\mu:

θy←θy−μ∂Ly∂θy,θd←θd−μ∂Ld∂θd,θf←θf−μ(∂Ly∂θf−λ∂Ld∂θf)\theta_y \leftarrow \theta_y - \mu \frac{\partial L_y}{\partial \theta_y}, \qquad \theta_d \leftarrow \theta_d - \mu \frac{\partial L_d}{\partial \theta_d}, \qquad \theta_f \leftarrow \theta_f - \mu \left( \frac{\partial L_y}{\partial \theta_f} - \lambda \frac{\partial L_d}{\partial \theta_f} \right)

The domain classifier descends its own loss, the label predictor descends the label loss, and the feature extractor descends the label loss while ascending the domain loss. The minus sign in front of λ\lambda in the last update is what the layer produces.

Check the toy in symbols

In the toy there is no label loss, so the label term is zero, and ∂Ld/∂θ=2(θ−2)\partial L_d / \partial \theta = 2(\theta - 2). At θ=1\theta = 1 this is −2-2. With λ=1\lambda = 1, the feature update is θ←1−0.1(0−1⋅(−2))=1−0.2=0.8\theta \leftarrow 1 - 0.1 (0 - 1 \cdot (-2)) = 1 - 0.2 = 0.8, which matches step 3 of "How it works."

Worked examples

Example
The toy with a different starting point

This is the toy from "How it works" with λ=1\lambda = 1 and μ=0.1\mu = 0.1.

  1. Start at θ=3\theta = 3. The domain loss is (3−2)2=1(3 - 2)^2 = 1.
  2. Ordinary gradient: 2(3−2)=22(3 - 2) = 2. Descent would move to 3−0.2=2.83 - 0.2 = 2.8, away from the minimum at 2.
  3. Reversed gradient: −2-2. Descent moves to 3+0.2=3.23 + 0.2 = 3.2.
  4. Check the new domain loss: (3.2−2)2=1.44(3.2 - 2)^2 = 1.44, which is higher than 1.

Interpretation: at this point the reversed update moves the feature away from the value the domain classifier wants, and the domain loss rises. Try it: start at θ=2.5\theta = 2.5 with the same λ\lambda and μ\mu. What is the reversed update, and does the domain loss rise? Answer: the gradient is 2(2.5−2)=12(2.5 - 2) = 1, the reversed value is −1-1, and the update is 2.5+0.1=2.62.5 + 0.1 = 2.6. The domain loss is (2.6−2)2=0.36(2.6 - 2)^2 = 0.36, which is higher than (2.5−2)2=0.25(2.5 - 2)^2 = 0.25.

Example
One example through the two heads

A feature extractor outputs a single number zz for one source example. The label predictor and the domain classifier each send back a gradient to zz. Suppose the label gradient is 0.50.5 and the domain gradient is 0.40.4, and λ=0.5\lambda = 0.5.

  1. Without a reversal layer and ignoring the domain head, the feature gets 0.50.5.
  2. With no reversal, adding the domain head gives 0.5+0.4=0.90.5 + 0.4 = 0.9. This pushes the features to help the domain classifier, which is the opposite of the goal.
  3. With the reversal layer, the domain gradient becomes −0.5×0.4=−0.2-0.5 \times 0.4 = -0.2.
  4. The total gradient on the feature is 0.5+(−0.2)=0.30.5 + (-0.2) = 0.3.

Interpretation: the reversal reduces the label signal's pull from 0.50.5 to 0.30.3, and it is the price of making the features less useful for the domain. Try it: with the same gradients, what is the total if λ=1\lambda = 1? Answer: the domain term is −1×0.4=−0.4-1 \times 0.4 = -0.4, and the total is 0.5−0.4=0.10.5 - 0.4 = 0.1, so the reversal now cancels most of the label signal.

Example
A domain classifier at chance

Suppose the source and target each contribute 1000 examples to a batch of 2000, and the domain classifier labels every example as source. It is right on the 1000 source examples and wrong on the 1000 target examples.

  1. Accuracy is 1000/2000=0.51000 / 2000 = 0.5, the same as guessing.
  2. A classifier at chance means the features no longer separate the two domains, at least for this classifier.
  3. Check the label accuracy on the target too. If the features have also lost class information, the label accuracy on the target falls, and the reversal has been too strong.
  4. The useful outcome is chance domain accuracy together with stable target label accuracy.

Interpretation: the domain number is a diagnostic, not a goal in itself. Try it: if the domain classifier gets 1200 of 2000 right after training, what is its accuracy, and is it above chance? Answer: 1200/2000=0.61200 / 2000 = 0.6, which is 10 points above chance, so the features still reveal the domain to some degree.

Common mistakes and tradeoffs

It is tempting to think that the reversal layer is a loss function. In fact it is an identity in the forward pass, so it changes the output of nothing. It changes only the gradient that flows back through it, which is why it can sit inside a network and still work with ordinary training code.

It is tempting to think that reversing the gradient makes the domain classifier learn the opposite task. In fact the domain classifier still descends its own loss and tries to tell the domains apart. Only the feature extractor, which sits before the layer, receives the reversed signal, and it is the only part that tries to hide the domain.

It is tempting to think that a larger λ\lambda always gives better adaptation. In fact a large λ\lambda can make the feature extractor destroy class information, and training can oscillate because the two objectives pull against each other. The original work increases λ\lambda gradually from 0 during training, so the features are shaped by the label loss first.

It is tempting to think that a domain classifier at chance proves that adaptation worked. In fact a weak domain classifier can also be at chance, and the features can still differ in ways that matter for the labels. Check label accuracy on labeled target data when you have it.

Use the layer when you want features that a particular head cannot use and you can afford adversarial training. Do not use it when training is unstable and the problem can be handled by matching feature statistics directly, since a divergence-based alignment is often simpler to tune. See Adversarial Training for the related idea of making a model robust to perturbed inputs, and Gradient Descent for the update rule that the layer modifies.

History

Adversarial objectives for representation learning were explored before the reversal layer. Generative adversarial networks, introduced by Ian Goodfellow and coauthors in 2014, framed learning as a two-player minimax game between a generator and a discriminator. That framing made it natural to ask whether the same kind of opposition could be written inside a single network.

In 2015 Yaroslav Ganin and Victor Lempitsky introduced the gradient reversal layer for unsupervised domain adaptation and showed that it works with standard backpropagation code. The 2015 arXiv preprint carries the title "Domain-Adversarial Training of Neural Networks", and a 2016 journal version extended the analysis. The layer caught on because it needed no change to the optimizer, only a custom backward rule.

Recap

  • The layer is an identity in the forward pass and multiplies the gradient by −λ-\lambda in the backward pass.
  • Placed between a feature extractor and a domain classifier, it makes the features work against the domain classifier while the classifier keeps learning normally.
  • The combined objective is the label loss minus λ\lambda times the domain loss, which is a minimax game inside one network.
  • Its strength λ\lambda is a sensitive setting, and the label accuracy on the target is the check that matters.

Read next: Domain-Adversarial Training of Neural Networks for the full method built on this layer, and Adversarial Training for a related use of adversarial objectives.

Check yourself

QuestionWhat does a gradient reversal layer do in the forward pass, and what does it do in the backward pass?Show answer
In the forward pass it passes its input through unchanged. In the backward pass it multiplies the incoming gradient by −λ-\lambda before sending it to the layers before it.
QuestionUsing the toy Ld(θ)=(θ−2)2L_d(\theta) = (\theta - 2)^2, λ=1\lambda = 1 and μ=0.1\mu = 0.1, starting from θ=1\theta = 1, what is the value of θ\theta after one reversed update?Show answer
The ordinary gradient is 2(1−2)=−22(1 - 2) = -2. The reversed gradient is +2+2, and the update is 1−0.1×2=0.81 - 0.1 \times 2 = 0.8.
QuestionWith λ=0.5\lambda = 0.5, the label gradient on a feature is 0.50.5 and the domain gradient is 0.40.4. What is the total gradient on the feature?Show answer
The domain term becomes −0.5×0.4=−0.2-0.5 \times 0.4 = -0.2, so the total is 0.5−0.2=0.30.5 - 0.2 = 0.3.
QuestionWhy is a domain classifier at chance accuracy not enough, by itself, to show that domain adaptation worked?Show answer
A weak classifier can also be at chance while the features still carry domain differences, and the reversal can destroy class information along with domain information. The label accuracy on labeled target data is needed to confirm that the features still support the task.

Sources