Code Monkey home page Code Monkey logo

Comments (4)

HuanyuZhang avatar HuanyuZhang commented on August 16, 2024

Our DP optimizer requires per_sample_gradient for every trainable parameter in the model (https://github.com/pytorch/opacus/blob/main/opacus/optimizers/optimizer.py#L283C17-L283C18). However, in your case, there is some part of the model not activated during (sometime) in the training, thus leading to a miss of per_sample_gradient. A potential fix might be freezing those parameters (setting param.requires_grad = False).

from opacus.

Lhsakfheudsj35407 avatar Lhsakfheudsj35407 commented on August 16, 2024

Thanks for your advise! I've tried to fix the problem in my code through your suggestion, but the code still can't work perfectly. Could you please tell me how did you fixed that problem through your method?

from opacus.

HuanyuZhang avatar HuanyuZhang commented on August 16, 2024

Thanks for the update. Could you elaborate the meaning of "the code still can't work perfectly"? Is there any new error popping up?

from opacus.

Lhsakfheudsj35407 avatar Lhsakfheudsj35407 commented on August 16, 2024

Here's the thing, I modified the code according to your suggestion using my own method. I modified the set_submodel method under the convnet class: after generating the submodel, I first froze all parameters and then enabled the parameters of the pre-set submodel. The following is the code for that part:

def set_submodel(self, ind, strategy=None):
    self.ind = ind

    assert ind <= 3

    if strategy is None:
        strategy = self.strategy
    print(strategy, ind, self.dataset)

    if strategy == 'progressive':
        modules = []
        for i in range(ind + 1):
            modules.append(self.module_splits[i])
        self.enc = nn.Sequential(*modules)
        self.head = self.head_splits[ind]

        for param in self.parameters():
            param.requires_grad = False

        for module in self.enc:
            for param in module.parameters():
                param.requires_grad = True
        for param in self.head.parameters():
            param.requires_grad = True

    elif strategy == 'baseline':
        modules = []
        for i in range(len(self.module_splits)):
            modules.append(self.module_splits[i])
        self.enc = nn.Sequential(*modules)
        self.head = self.classifier

        for param in self.parameters():
            param.requires_grad = False

        for module in self.enc:
            for param in module.parameters():
                param.requires_grad = True
        for param in self.head.parameters():
            param.requires_grad = True

    else:
        raise NotImplementedError()

However, after rewriting it, the same error still occurs. I'm a bit confused about this. Could you please share the code you used to solve this problem?

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.