Can you please explain a.is_leaf= True as I did not use this.

A Tensor is a leaf if it does not have any “history” in the autograd sense. Meaning that we cannot propagate gradients from this Tensor back to its parents.
There are two main cases here:

leaf that does not require gradient mean a Tensor for which gradients are not tracked.
leaf that requires gradient mean that the user explicitly asked for gradients here and so the .grad field will be populated when you call .backward().


How do I incorporate to my code?

You don’t need to  It is just in case you want to look in more details, you can check if a Tensor is a leaf or not.

Also, what do you mean by optimizer.step() ?

Yes exactly, we have builtin optimizer to do these kind of updates, but you can do them by hand as well.