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.3+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,
    sample_rate=0.01,
    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.13.0.tar.gz (73.3 kB view details)

Uploaded Source

Built Distribution

opacus-0.13.0-py3-none-any.whl (102.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: opacus-0.13.0.tar.gz
  • Upload date:
  • Size: 73.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for opacus-0.13.0.tar.gz
Algorithm Hash digest
SHA256 0599a59504aea549671fef49395a7e444cd4a9794f6a073c4210a6eb63184155
MD5 3e09d33b192a17198e38dfbf279715ab
BLAKE2b-256 c3416fabab20e55be5fb5f5fdfa960820a3ef18ce00e897864f7e29729535b36

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: opacus-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 102.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for opacus-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9880b5498724e105cd7c1acce3333a3bc8e5177fd8ee0d620e7df26e7c12f6be
MD5 1fd9ca40bacd85068d10dba347acc705
BLAKE2b-256 68c436f07b54413e607f07858d5b668379991948f0de9979185a895fee9c2208

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