Skip to main content

Train PyTorch models with Differential Privacy

Project description

Opacus


CircleCI

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 scientists 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

:warning: NOTE: This will bring in the latest version of our deps, which are on Cuda 10.2. This will not work if you environment is using an older Cuda version (for example, Google Colab is still on Cuda 10.1).

To install on Colab, run this cell first:

pip install torchcsprng==0.1.2+cu101 torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

Then you can just pip install opacus like before. See more context in this issue.

You can also install directly from the source for the latest features (along with its quirks and potentially ocassional 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 declare a PrivacyEngine and attach it to your optimizer before running, eg:

model = Net()
optimizer = SGD(model.parameters(), lr=0.05)
privacy_engine = PrivacyEngine(
    model,
    batch_size,
    sample_size,
    alphas=[10, 100],
    noise_multiplier=1.3,
    max_grad_norm=1.0,
)
privacy_engine.attach(optimizer)
# Now it's business as usual

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

FAQ

Checkout 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.

References

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-0.10.0.tar.gz (57.3 kB view details)

Uploaded Source

Built Distribution

opacus-0.10.0-py3-none-any.whl (77.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: opacus-0.10.0.tar.gz
  • Upload date:
  • Size: 57.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.9

File hashes

Hashes for opacus-0.10.0.tar.gz
Algorithm Hash digest
SHA256 b2196955c7480ef47e95ea47f456299430fee1bcabbf38a0a14ed43be679ced3
MD5 72371715084dbd4f71042e6d9d392921
BLAKE2b-256 24a4cb1f40fdf321a0063205cf84fd24376a21af4f24da20f55d965cce2983e2

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: opacus-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 77.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.6.9

File hashes

Hashes for opacus-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 653721c094ed361f5456cdf96019bcdbee385754a16877300378640d2cb9272a
MD5 8a827001d2832d0d64ec017820786873
BLAKE2b-256 0f06ec4530fcc8f435a86c177e7d056cb187d81559f2a47981cfb2429b12df2f

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