Git Product home page Git Product logo

Comments (5)

pepper-jk avatar pepper-jk commented on August 25, 2024 2

At least for exponential and normal distribution there appears to be an alternative approach.

One can create a Tensor and fill it with numbers from the given distribution in-place, as described here.
The in-place sampling even allows a generator, so the secure_generator (now called self.random_number_generator) created in the privacy_engine could be utilized and therefore urandom could be used as a source.
The device can be given to a tensor on declaration as well, so this appears to solve both problems.

However this approach lacks Laplace, again.
It is really problematic that the distributions are not standardized so that they behave the same (regarding parameters and configurations) no matter where we call them in the torch libs.

from opacus.

pepper-jk avatar pepper-jk commented on August 25, 2024

Please let me know if I missed something.

from opacus.

pepper-jk avatar pepper-jk commented on August 25, 2024

I updated our _generate_noise method to use a tensor for scale and rate to pass on the device.

So the only thing missing is the generator support. Hoping that our proposal in pytorch/pytorch#45115 gets addressed and we will be able to add it later and therefore use urandom as a source.

For our research purposes however this should be sufficient.

def _generate_noise(self, max_norm, parameter):
    if self.noise_multiplier > 0:
        mean = 0
        scale_scalar = self.noise_multiplier * max_norm

        scale = torch.full(size=parameter.grad.shape, fill_value=scale_scalar, dtype=torch.float32, device=self.device)

        if self.noise_type == "gaussian":
            dist = torch.distributions.normal.Normal(mean, scale)
        elif self.noise_type == "laplacian":
            dist = torch.distributions.laplace.Laplace(mean, scale)
        elif self.noise_type == "exponential":
            rate = 1 / scale
            dist = torch.distributions.exponential.Exponential(rate)
        else:
            dist = torch.distributions.normal.Normal(mean, scale)

        noise = dist.sample()

        return noise
    return 0.0

from opacus.

Darktex avatar Darktex commented on August 25, 2024

Hi @pepper-jk !

It looks like this is blocked on the PyTorch side, but there are a few comments I'd like to add: in general, while I agree that it would be very simple for us to extend support for more distributions, we are hesitant to do that because we don't see a use case where you would want to use an exponential or Laplace distribution for DP-SGD.

That being said, you should absolutely be able to plug in whatever RNG you want without you having to edit our lib or upstream a PR to us necessarily, just like you don't need to commit a new nn.Module you wrote to PyTorch every time. We are sorry this is not yet very doable, but we are working on making this lib a lot more modular for research for this exact reason :)

For now, I would recommend either editing in-place or monkeypatching in your script:

privacy_engine = PrivacyEngine(...)
privacy_engine._generate_noise = my_generate_noise

from opacus.

pepper-jk avatar pepper-jk commented on August 25, 2024

Just for the record, We since published a small library we extended to create exponantial and laplace noise using opacus.
In case anyone else needs this.

We are using it in the code for our Label Leakage from Gradients paper.

from opacus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.