ValueError: Unexpected grad_sample type: <class 'NoneType'> when trying to train with differential privacy

username: general
cooked: <p>Hello,</p>
<p>I am using PyTorch 1.10.0+cu102 and Opacus 1.0.0 and would like to use differential privacy for the training of an image classifier. When using optimizer.step() during training, I get the following ValueError: Unexpected grad_sample type: &lt;class ‘NoneType’&gt;</p>
<p>Specifically, this is my Traceback:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "run.py", line 102, in &lt;module&gt;
    sc.run_learning()
  File "/gpfs/home/user/diffp_project/run2.py", line 82, in run_learning
    le.run_learning_and_testing_process()
  File "/gpfs/home/user/diffp_project/LearningEnvironment.py", line 165, in run_learning_and_testing_process
    client.federated_round_local_train(model=self.global_model)
  File "/gpfs/home/user/diffp_project/Client.py", line 208, in federated_round_local_train
    epoch_loss_train = self.local_train(model=self.model)
  File "/gpfs/home/user/diffp_project/Client.py", line 176, in local_train
    running_loss = BatchIterator(model=model, phase=phase, Data_loader=train_loader, criterion=criterion, optimizer=optimizer, device=self.device)
  File "/gpfs/home/user/diffp_project/batchiterator.py", line 58, in BatchIterator
    optimizer.step()
  File "/home/user/miniconda3/envs/env5/lib/python3.7/site-packages/opacus/optimizers/optimizer.py", line 478, in step
    if self.pre_step():
  File "/home/user/miniconda3/envs/env5/lib/python3.7/site-packages/opacus/optimizers/optimizer.py", line 459, in pre_step
    self.clip_and_accumulate()
  File "/home/user/miniconda3/envs/env5/lib/python3.7/site-packages/opacus/optimizers/optimizer.py", line 366, in clip_and_accumulate
    g.view(len(g), -1).norm(2, dim=-1) for g in self.grad_samples
  File "/home/user/miniconda3/envs/env5/lib/python3.7/site-packages/opacus/optimizers/optimizer.py", line 313, in grad_samples
    ret.append(_get_flat_grad_sample(p))
  File "/home/user/miniconda3/envs/env5/lib/python3.7/site-packages/opacus/optimizers/optimizer.py", line 186, in _get_flat_grad_sample
    raise ValueError(f"Unexpected grad_sample type: {type(p.grad_sample)}")
ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;
</code></pre>
<p>Does anybody know how to fix it?</p>
<p>Here is some more information about my code:</p>
<p>The model is a torchvision.models.densenet121(pretrained=True), the optimizer is a torch.optim.SGD(self.model.parameters(), lr=0.1, momentum=0), the loss criterion is nn.BCELoss().to(self.device).</p>
<p>I also run model = ModuleValidator.fix(model), and of course model, optimizer, train_loader = self.privacy_engine.make_private(…) before the training.</p>
<p>Thanks in advance!</p>

username: ashkan_software
cooked: <p>Hi <a class="mention" href="/u/general">@general</a>,</p>
<p>Would you be able to share with us a reproducible code so we can debug it? I believe another user has reported this here:</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/opacus/issues/312">
  <header class="source">

      <a href="https://github.com/pytorch/opacus/issues/312" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/opacus</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/opacus/issues/312" target="_blank" rel="noopener nofollow ugc">Getting "ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;" for self-supervised training.</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-12-12" data-time="11:37:59" data-timezone="UTC">11:37AM - 12 Dec 21 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/vangorade" target="_blank" rel="noopener nofollow ugc">
          <img alt="vangorade" src="https://avatars.githubusercontent.com/u/71941335?v=4" class="onebox-avatar-inline" width="20" height="20">
          vangorade
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Congratulations on the new opacus version release!

I am trying to train a con<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">trastive learning model on time-series data with opacus version 1.0 by following the official [tutorial ](https://github.com/pytorch/opacus/blob/main/tutorials/building_image_classifier.ipynb).

Error:
"ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;"

Full error:
    model_optimizer.step()  -&gt; error starts from this line in my code.
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 478, in step
    if self.pre_step():
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 459, in pre_step
    self.clip_and_accumulate()
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 366, in clip_and_accumulate      
    g.view(len(g), -1).norm(2, dim=-1) for g in self.grad_samples
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 313, in grad_samples
    ret.append(_get_flat_grad_sample(p))
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 186, in _get_flat_grad_sample    
    raise ValueError(f"Unexpected grad_sample type: {type(p.grad_sample)}")
ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;

Thank you.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: general
cooked: <p>Hi, I have commented on GitHub with exemplary code. Thanks!</p>

username: Peter_Romov
cooked: <p>Answered on Github</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/opacus/issues/312#issuecomment-1016953478">
  <header class="source">

      <a href="https://github.com/pytorch/opacus/issues/312#issuecomment-1016953478" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/opacus</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/opacus/issues/312#issuecomment-1016953478" target="_blank" rel="noopener nofollow ugc">Getting "ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;" for self-supervised training.</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2021-12-12" data-time="11:37:59" data-timezone="UTC">11:37AM - 12 Dec 21 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2022-01-20" data-time="00:57:19" data-timezone="UTC">12:57AM - 20 Jan 22 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/vangorade" target="_blank" rel="noopener nofollow ugc">
          <img alt="vangorade" src="https://avatars.githubusercontent.com/u/71941335?v=4" class="onebox-avatar-inline" width="20" height="20">
          vangorade
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Congratulations on the new opacus version release!

I am trying to train a con<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">trastive learning model on time-series data with opacus version 1.0 by following the official [tutorial ](https://github.com/pytorch/opacus/blob/main/tutorials/building_image_classifier.ipynb).

Error:
"ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;"

Full error:
    model_optimizer.step()  -&gt; error starts from this line in my code.
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 478, in step
    if self.pre_step():
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 459, in pre_step
    self.clip_and_accumulate()
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 366, in clip_and_accumulate      
    g.view(len(g), -1).norm(2, dim=-1) for g in self.grad_samples
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 313, in grad_samples
    ret.append(_get_flat_grad_sample(p))
  File "/mnt/f/AI_research/TS-TCC-main/opacus/opacus/optimizers/optimizer.py", line 186, in _get_flat_grad_sample    
    raise ValueError(f"Unexpected grad_sample type: {type(p.grad_sample)}")
ValueError: Unexpected grad_sample type: &lt;class 'NoneType'&gt;

Thank you.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>