skillfed

pytorch_revgrad

A pytorch module (and function) to reverse gradients.

pytorch-revgrad v0.2.0 224.3K downloads/30d#9,235 on PyPI158
Permissive license DORMANT released

What it is and what it does

pytorch-revgrad provides a single-purpose layer that reverses gradients during backpropagation, a technique used in adversarial training and domain adaptation. It wraps PyTorch's autograd mechanism to negate gradients flowing backward through the layer while preserving the forward pass unchanged. The layer integrates directly into torch.nn.Sequential or custom models as a drop-in module.

The package depends only on numpy and torch, making it lightweight to install. It targets a specific algorithmic need rather than a broad framework—if your model requires gradient reversal for adversarial objectives (such as domain-adversarial neural networks), this layer provides that functionality without additional overhead. The dormant maintenance status and age of the codebase mean you should verify compatibility with your PyTorch version before relying on it in production.

Use it for:

  • Domain adaptation: reverse gradients in a discriminator to train domain-invariant feature extractors.
  • Adversarial training: implement gradient reversal for adversarial loss objectives in multi-task learning.
  • Feature learning: use RevGrad to prevent a classifier from learning domain-specific features during transfer learning.
  • Research prototyping: quickly add gradient reversal to experimental architectures without custom autograd code.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Implements a gradient reversal layer for PyTorch neural networks, enabling adversarial training by reversing gradients during backpropagation.

Yes, if you need gradient reversal for adversarial training or domain adaptation and your PyTorch version is compatible. The low install friction and permissive license make it a straightforward addition. However, verify that the package works with your specific PyTorch version first, given the dormant maintenance status and last release in 2021. For new projects, consider whether a custom autograd Function might be more maintainable long-term.

Install

pytorch-revgrad on PyPI

pip

pip install pytorch-revgrad

uv

uv add pytorch-revgrad

poetry

poetry add pytorch-revgrad

Installing pytorch_revgrad

Before you install

Low friction install with only numpy and torch as dependencies. Dormant maintenance status—last release was 2021-01-09 and no commits since 2024-11-04—but the repository remains active and the package is straightforward enough that infrequent updates are not inherently a risk.

License in practice

MIT license (permissive) means you can use, modify, and distribute this package freely in both open and closed projects with minimal restrictions.

Quickstart

pip install pytorch-revgrad

import torch
from pytorch_revgrad import RevGrad

model = torch.nn.Sequential(
    torch.nn.Linear(10, 5),
    torch.nn.Linear(5, 2),
    RevGrad()
)

Verify before relying

  • Whether gradient reversal behavior matches current PyTorch conventions and autograd semantics in modern PyTorch versions.
  • Whether the package has been tested against recent PyTorch releases or if compatibility issues exist.

Package facts

License not declared (permissive)
Python support supports the current Python release (>=3.5)
Install friction low — pure-Python wheel
Runtime dependencies 2 — numpy, torch
Maintenance dormant — 2,043 days since the last release
Last repo commit
First released
Downloads 224,317/month — #9,235 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytorch_revgrad-0.2.0-py3-none-any.whl

License :: OSI Approved :: MIT License

Tags

gradient reversal pytorchadversarial training layerreverse gradients neural networkdomain adaptation pytorchgradient flip deep learningpytorch adversarial layerrevgrad module
adversarial-trainingdomain-adaptationgradient-manipulation

More Artificial Intelligence packages

Further reading