Package: opacus
Opacusis a library that enables training PyTorch models with differential privacy.
It supports training with minimal code changes required on the client, has little impact on training performance, and allows the client to online track the privacy budget expended at any given moment.Target audienceThis code release is aimed at two target audiences:ML practitioners will find this to be a gentle introduction to training a model with differential privacy as it requires minimal code changes.Differential Privacy researchers will find this easy to experiment and tinker with, allowing them to focus on what matters.InstallationThe latest release of Opacus can be installed viapip:pipinstallopacusOR, alternatively, viaconda:condainstall-cconda-forgeopacusYou can also install directly from the source for the latest features (along with its quirks and potentially occasional bugs):gitclonehttps://github.com/pytorch/opacus.gitcdopacus
pipinstall-e.Getting startedTo train your model with differential privacy, all you need to do is to instantiate aPrivacyEngineand pass your model, data_loader, and optimizer to the engine'smake_private()method to obtain their private counterparts.# define your components as usualmodel=Net()optimizer=SGD(model.parameters(),lr=0.05)data_loader=torch.utils.data.DataLoader(dataset,batch_size=1024)# enter PrivacyEngineprivacy_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,)# Now it's business as usualTheMNIST exampleshows an end-to-end run using Opacus. Theexamplesfolder contains more such examples.Migrating to 1.0Opacus 1.0 introduced many improvements to the library, but also some breaking changes.
If you've been using Opacus 0.x and want to update to the latest release,
please use thisMigration GuideLearn moreInteractive tutorialsWe've built a series of IPython-based tutorials as a gentle introduction to training models
with privacy and using various Opacus features.Building an Image Classifier with Differential PrivacyTraining a differentially private LSTM model for name classificationBuilding text classifier with Differential Privacy on BERTOpacus Guide: Introduction to advanced featuresOpacus Guide: Grad samplersOpacus Guide: Module Validator and FixerTechnical report and citationThe technical report introducing Opacus, presenting its design principles, mathematical foundations, and benchmarks can be foundhere.Consider citing the report if you use Opacus in your papers, as follows:@article{opacus,
  title={Opacus: {U}ser-Friendly Differential Privacy Library in {PyTorch}},
  author={Ashkan Yousefpour and Igor Shilov and Alexandre Sablayrolles and Davide Testuggine and Karthik Prasad and Mani Malek and John Nguyen and Sayan Ghosh and Akash Bharadwaj and Jessica Zhao and Graham Cormode and Ilya Mironov},
  journal={arXiv preprint arXiv:2109.12298},
  year={2021}
}Blogposts and talksIf you want to learn more about DP-SGD and related topics, check out our series of blogposts and talks:Differential Privacy Series Part 1 | DP-SGD Algorithm ExplainedDifferential Privacy Series Part 2 | Efficient Per-Sample Gradient Computation in OpacusPriCon 2020 Tutorial: Differentially Private Model Training with OpacusDifferential Privacy on PyTorch | PyTorch Developer Day 2020Opacus v1.0 Highlights | PyTorch Developer Day 2021FAQCheck out theFAQpage for answers to some of the most frequently asked questions about differential privacy and Opacus.ContributingSee theCONTRIBUTINGfile for how to help out.
Do also check out the README files inside the repo to learn how the code is organized.LicenseThis code is released under Apache 2.0, as found in theLICENSEfile.