Gradient become None

username: Atiqur_Rahman
cooked: <p>Hello everyone. I am facing an issue. I am explaining what I am trying to do.<br>
I have a Traffic and Road sign dataset that contains 43 classes. I am trying to classify the images. I am using the resnet34 pre-trained model. I have <a href="https://www.amd.com/en/products/graphics/amd-radeon-rx-6600" rel="noopener nofollow ugc">AMD RX6600</a> GPU that I use for running the model. For running the model on my AMD GPU I am using <a href="https://pypi.org/project/pytorch-directml/" rel="noopener nofollow ugc">Pytorch Directml</a> and using this code</p>
<pre><code class="lang-auto">import torch_directml
dml = torch_directml.device()
</code></pre>
<p>to find the device. Using this dml instance, I push the mode and training data to the GPU. But the problem is weights do not update. After a lot for debugging, I found that the model grad becomes none in the training loop when using GPU. But in the CPU it works totally fine.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/a/0/a042b1acfc7e0218d503f4e32d56786c10d6aa8e.png" alt="image" data-base62-sha1="mRJhLIeqPgigQC3ivwqxpHRZceO" width="479" height="217"><br>
When I want to see the grad values, I found this issue. When the model is in CPU, this print statement prints some numbers. But when I run same code in GPU the following error occurs.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/4/f4374b4b23d5e21e288455cab857f9e85c75ef2d.png" data-download-href="https://discuss.pytorch.org/uploads/default/f4374b4b23d5e21e288455cab857f9e85c75ef2d" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/4/f4374b4b23d5e21e288455cab857f9e85c75ef2d.png" alt="image" data-base62-sha1="yQqY8eNXiPgSodS9hklnQAego1L" width="690" height="232" data-dominant-color="F6F5F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">891×300 21.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
you can see p.grad become none. And that’s why when I use the optimizer.step() nothing is updated and the model does not learn anything. Can anyone help me with this issue?<br>
<strong>Thanks in advance</strong></p>

username: srishti-git1110
cooked: <p>Hi,<br>
Please try to post a minimum executable snippet enclosed within ``` the reproduces the error.</p>

username: Atiqur_Rahman
cooked: <p>Ok, I will. Thank you for your information.</p>

username: ginofft
cooked: <p>You training loop is wrong, <code>torch.no_grad()</code> turn of dynamic graph (gradient) so of course no gradient is updated.</p>
<p>You use <code>torch.no_grad()</code> when you want to test (or validate) the model <strong>not</strong> training it.</p>
<p>Take a look at this <a href="https://pytorch.org/tutorials/beginner/basics/optimization_tutorial.html" rel="noopener nofollow ugc">tutorial</a>.</p>

username: ConvolutionalAtom
cooked: <p>General practice: use as much as text possible instead of images</p>
<ol>
<li>It can be copied and pasted more easily</li>
<li>Text is more standard and robust whereas image size, resolution might affect readability</li>
</ol>

username: Atiqur_Rahman
cooked: <p>Ok, so I have figured out something. After one backward pass, I printed the value using this code</p>
<pre><code class="lang-auto">for param in base_model.parameters():
    print((param.grad.data).cpu().sum())
</code></pre>
<p>when the model is in the CPU, it prints sum numbers.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/2/a259143da6fda8c57d605eab241bb3d9d9c17046.png" data-download-href="https://discuss.pytorch.org/uploads/default/a259143da6fda8c57d605eab241bb3d9d9c17046" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/2/a259143da6fda8c57d605eab241bb3d9d9c17046.png" alt="image" data-base62-sha1="nacctwXmAty9QRg3qc8CbijAI8m" width="690" height="381" data-dominant-color="F5F6F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">904×500 25.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>But when the model is in the GPU this error happens.</p>
<pre><code class="lang-auto">tensor(-4.5475e-13)
tensor(2.2737e-13)
tensor(4.5475e-13)
tensor(-1.3588e-06)
tensor(-1.8833e-06)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[91], line 2
      1 for param in base_model.parameters():
----&gt; 2     print((param.grad.data).cpu().sum())

AttributeError: 'NoneType' object has no attribute 'data'
</code></pre>
<p>Some of the model parameters are none. I think this is the root cause of the issue.</p>
<p>My manual one iteration code is given below</p>
<pre><code class="lang-auto">num_classes = 2
num_epochs = 40
learning_rate = 1e-4
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.AdamW(base_model.parameters(), lr=learning_rate)

#taking only the first batch
for batch in dataloaders['train']:
    batch = {k: v.to(dml) for k, v in batch.items()}
    break

#forward pass
base_model.train()
outputs = base_model(**batch)
labels = batch['Type']

#backward pass
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()

#to see any parameters get updated
for param in base_model.parameters():
    print((param.grad.data).cpu().sum()) 
</code></pre>
<p>Can you please tell me why this happens when I run the model in GPU?</p>

username: ginofft
cooked: <p>Is the model on GPU ? If so, whats the point of <code>.cpu()</code> ?</p>

username: Atiqur_Rahman
cooked: <p>when the model is on the GPU I cannot print the model param. That’s why I have to use .cpu() to print the value otherwise it throws an error.</p>