“log_alpha” is a variable(I think), every time I update its data with its grad, the grad will change into None.
code as follow:
1 alpha_loss = -torch.mean(self.log_alpha * (log_pis.double() + self._target_entropy))
3 alpha_loss.backward()
5 self.log_alpha = self.log_alpha - grad * 3e-4
but if I use line 2 code, no error. So I’m a little confused about the grad clearing mechanism.
& I’m not sure if the grad will be added all the time.

It seems you are replacing `self.log_alpha` in your example.
Have a look at these example, where we 1) replace `x`, 2) assign to a new tensor and 3) modify `x` inplace:

``````# 1) replace tensor
loss = torch.mean(x)
loss.backward()
> tensor([0.5000, 0.5000])
> None

# 2) assign to other tensor
loss = torch.mean(x)
loss.backward()
> tensor([0.5000, 0.5000])
> tensor([0.5000, 0.5000])
> None

# 3) inplace
loss = torch.mean(x)
loss.backward()
> tensor([0.5000, 0.5000])