--- id: opacus version: "1.6.0" license: Apache-2.0 license_treatment: permissive maintenance: active --- # opacus — Train PyTorch models with Differential Privacy License: permissive · Maintenance: active · Downloads: 170.5K/mo ## What it is and what it does Opacus is a PyTorch library that adds differential privacy to model training through DP-SGD (Differentially Private Stochastic Gradient Descent). It wraps your existing model, optimizer, and data loader with privacy-aware counterparts via a PrivacyEngine, allowing you to train with formal privacy guarantees while monitoring privacy budget expenditure in real time. The library targets both ML practitioners seeking an accessible introduction to privacy-preserving training and differential privacy researchers needing flexibility for experimentation. It depends on numpy, torch, scipy, and opt-einsum, and recent updates have introduced Fast Gradient Clipping and Ghost Clipping to reduce memory overhead during training. Use it for: - Train models on sensitive data (medical records, financial data) with formal privacy guarantees for regulatory compliance - Federated learning scenarios where model updates must not leak information about individual training samples - Research into differential privacy algorithms and DP-SGD variants with minimal boilerplate - Building text classifiers on BERT with privacy (as shown in updated tutorials with LoRA/peft integration) - Image classification tasks where privacy budget must be tracked and controlled throughout training ## Worth the install? AI-flagged interpretation of the facts above — verify before relying. Opacus enables training PyTorch models with differential privacy using DP-SGD, requiring minimal code changes to add privacy guarantees while tracking privacy budget consumption. Yes, if you need to train PyTorch models with formal differential privacy guarantees. The library is actively maintained, has low installation friction, carries a permissive license, and integrates cleanly into existing PyTorch workflows. Install if privacy-preserving training is a requirement; skip if you have no privacy constraints. ## Install pip install opacus uv add opacus poetry add opacus ## Installing opacus Before you install: Low friction installation with a pure Python wheel. Active maintenance with recent updates (2024-12-18 and 2024-08-20) including Fast Gradient Clipping and Ghost Clipping features. Repository shows 1949 stars and active development. License in practice: Released under Apache-2.0 (permissive), allowing commercial and private use with minimal restrictions. You may use, modify, and distribute the code freely provided you include the license notice. Quickstart: pip install opacus from opacus import PrivacyEngine import torch model = Net() optimizer = torch.optim.SGD(model.parameters(), lr=0.05) data_loader = torch.utils.data.DataLoader(dataset, batch_size=1024) privacy_engine = PrivacyEngine() model, optimizer, data_loader = privacy_engine.make_private( module=model, optimizer=optimizer, data_loader=data_loader, noise_multiplier=1.1, max_grad_norm=1.0, ) Requires torch and numpy. Model architecture must be compatible with DP-SGD (some layer types may need validation via Opacus's module validator). Verify before relying: - Performance overhead compared to standard PyTorch training under typical workloads - Compatibility with specific PyTorch versions beyond the stated Python 3.7.5+ requirement - Memory savings quantified for the Ghost Clipping feature mentioned in recent updates ## Package facts - License: Apache-2.0 (permissive) - Python support: supports_current - Install friction: low - Maintenance: active - Downloads: 170.5K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags differential privacy pytorch, dp-sgd training, privacy preserving machine learning, privacy budget tracking, differentially private neural networks, pytorch privacy library, federated learning privacy, differential-privacy, pytorch-extension, privacy-preserving-ml [View on SkillFed](https://skillfed.io/packages/opacus) · [View on PyPI](https://pypi.org/project/opacus/)