You are creating a non-leaf tensor with the torch.clamp operation:
created_image = torch.normal(mean=1, std=1, size=(1,1,28,28), requires_grad=True)
print(created_image.is_leaf)
> True
created_image = torch.clamp(created_image, -1, 1)
print(created_image.is_leaf)
> False

Try to change the output of torch.clamp to another variable name.
Also, there are some minor issues in the code:


Variables are deprecated since 0.4.0, so you can use tensors directly
Don’t call net.forward, as this will not call into registered hooks. Instead call the model as net(input)


class_loss.backward is probably a typo and should call the method as class_loss.backward()