Skip to main content

Train PyTorch models with Differential Privacy

Project description

Opacus


CircleCI Coverage Status PRs Welcome License

Opacus is 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 audience

This code release is aimed at two target audiences:

  1. ML practitioners will find this to be a gentle introduction to training a model with differential privacy as it requires minimal code changes.
  2. Differential Privacy researchers will find this easy to experiment and tinker with, allowing them to focus on what matters.

Installation

The latest release of Opacus can be installed via pip:

pip install opacus

OR, alternatively, via conda:

conda install -c conda-forge opacus

You can also install directly from the source for the latest features (along with its quirks and potentially occasional bugs):

git clone https://github.com/pytorch/opacus.git
cd opacus
pip install -e .

Getting started

To train your model with differential privacy, all you need to do is to instantiate a PrivacyEngine and pass your model, data_loader, and optimizer to the engine's make_private() method to obtain their private counterparts.

# define your components as usual
model = Net()
optimizer = SGD(model.parameters(), lr=0.05)
data_loader = torch.utils.data.DataLoader(dataset, batch_size=1024)

# enter PrivacyEngine
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,
)
# Now it's business as usual

The MNIST example shows an end-to-end run using Opacus. The examples folder contains more such examples.

Migrating to 1.0

Opacus 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 this Migration Guide

Learn more

Interactive tutorials

We've built a series of IPython-based tutorials as a gentle introduction to training models with privacy and using various Opacus features.

Technical report and citation

The technical report introducing Opacus, presenting its design principles, mathematical foundations, and benchmarks can be found here.

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 talks

If you want to learn more about DP-SGD and related topics, check out our series of blogposts and talks:

FAQ

Check out the FAQ page for answers to some of the most frequently asked questions about differential privacy and Opacus.

Contributing

See the CONTRIBUTING file for how to help out. Do also check out the README files inside the repo to learn how the code is organized.

License

This code is released under Apache 2.0, as found in the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opacus-1.4.0.tar.gz (137.1 kB view details)

Uploaded Source

Built Distribution

opacus-1.4.0-py3-none-any.whl (224.8 kB view details)

Uploaded Python 3

File details

Details for the file opacus-1.4.0.tar.gz.

File metadata

  • Download URL: opacus-1.4.0.tar.gz
  • Upload date:
  • Size: 137.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for opacus-1.4.0.tar.gz
Algorithm Hash digest
SHA256 7a41d49c34af8e4308189bc8c2b22e65cf1edc0373477a02a43ccd957bac970d
MD5 7e849ab55c79e5b701a22bbf14793514
BLAKE2b-256 4434c843a74742841a23904c1723e190eb122c4b6ccd38c178c5fbed4d1615b6

See more details on using hashes here.

Provenance

File details

Details for the file opacus-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: opacus-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 224.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for opacus-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44b5b8536138a1eac6a604a35f59d7236dfc280bb543375725eb9eb299516c04
MD5 0564466a250b318de923d531eda7b10b
BLAKE2b-256 e254fdc18fb7694ef67e577ba59a2a4be1df9cfce3838ca9b18cafd5683afbfa

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page