Hi Fred!



 HaFred:

In such case how could I turn off the autograd for some specific elements as above snippet?


You can’t.  The whole Tensor is the Parameter, so its requires_grad
property applies to the whole Tensor, not just to individual elements.
The safest way to accomplish what you want is to store the values of
the tensor elements you want to freeze, call opt.step(), and then
rewrite the stored values back into the tensor.
(Some optimization algorithms will change elements even if their
gradients are zero.  If you know that your optimizer will not change
zero-gradient elements, you can zero the gradients of the elements
you want to freeze before calling opt.step().  But you risk breaking
your freezing if you change the details of your optimizer.)
Best.
K. Frank