'NoneType' object has no attribute 'data' - param.grad.data.clamp_(-1, 1)

username: yael-ai
cooked: <p>Hello,<br>
I am trying to create a neural network that has multiple heads:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdbc882115ecb0720c2d332d78410b7094cee2bd.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cdbc882115ecb0720c2d332d78410b7094cee2bd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdbc882115ecb0720c2d332d78410b7094cee2bd.jpeg" alt="image" data-base62-sha1="tm1O9gV5uHTJTJYk5H8jB2XLXxr" width="690" height="454" data-dominant-color="F4F2EE"><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">697×459 41.8 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>
I get the error:<br>
param.grad.data.clamp_(-1, 1)<br>
AttributeError: ‘NoneType’ object has no attribute ‘data’</p>
<p>My code looks like this:</p>
<pre><code class="lang-auto">if batch_1 and batch_2 and batch_3:
      sum_loss = loss_1 + loss_2 + loss_3
      self.step_optimizer(sum_loss)
      self.update_target_network()
</code></pre>
<pre><code class="lang-auto">def step_optimizer(self, loss):
     self.optimizer.zero_grad()
     loss.backward()
     for param in self.value_net.parameters():
          param.grad.data.clamp_(-1, 1)
     self.optimizer.step()
</code></pre>
<p>I do not understand what I am doing wrong…<br>
Thank you,<br>
Yael</p>

username: ptrblck
cooked: <p>Based on the error message some parameters of <code>self.value_net</code> didn’t receive a gradient and thus their <code>.grad</code> attribute is set to <code>None</code>.<br>
This could happen, e.g. if you’ve (accidentally) detached the computation graph or if these parameters were not used in the forward pass (and would thus not receive a gradient).</p>

username: rwandamc
cooked: <aside class="quote no-group" data-username="yael-ai" data-post="1" data-topic="124574">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yael-ai/48/39487_2.png" class="avatar"> yael-ai:</div>
<blockquote>
<p>AttributeError: ‘NoneType’ object has no attribute ‘data’</p>
</blockquote>
</aside>
<p>AttributeError means that there was an Error that had to do with an Attribute request. In general, when you write x.y, y is the purported attribute of x. NoneType means that instead of an instance of whatever Class or Object you think you’re working with, you’ve actually got None. That usually means that an assignment or function call up failed or returned an unexpected result.</p>
<p>mylist = mylist.sort()</p>
<p>The sort() method of a list sorts the list in-place, that is, mylist is modified. But the actual return value of the method is None and not the list sorted. So you’ve just assigned None to mylist. If you next try to do, say, mylist.append(1) <a href="http://net-informations.com/python/iq/default.htm" rel="noopener nofollow ugc">Python</a> will give you this error.</p>