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

I am trying to create a neural network that has multiple heads:

I get the error:
param.grad.data.clamp_(-1, 1)
AttributeError: ‘NoneType’ object has no attribute ‘data’

My code looks like this:

if batch_1 and batch_2 and batch_3:
      sum_loss = loss_1 + loss_2 + loss_3
def step_optimizer(self, loss):
     for param in self.value_net.parameters():
          param.grad.data.clamp_(-1, 1)

I do not understand what I am doing wrong…
Thank you,

Based on the error message some parameters of self.value_net didn’t receive a gradient and thus their .grad attribute is set to None.
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).


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.

mylist = mylist.sort()

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) Python will give you this error.