Hi Afsaneh!



 afsaneh_ebrahimi:

I know I can set requires_grad but I just want it for one element?


The short answer is to set the specific element in question back to
1 after calling opt.step().
As you’ve recognized, requires_grad applies to an entire tensor,
not separately to individual elements.  It is also true that an Optimizer
applies to entire tensors, and not to individual elements.  There are
a number of approaches to “freezing” individual elements of a tensor
that is otherwise being optimized, but simply resetting the desired
values after running opt.step() is probably the most straightforward.
Best.
K. Frank