Hi Abhisek!



 abhisek:

I still get the exact same runtime error


Could you post a trimmed-down, runnable, fully-self-contained script
that reproduces the error?  (Please have the script print out the version
of pytorch you’re using, torch.__version__.)
Please provide hard-coded (or random) sample data (with size and
dimensionality as small as possible while still reproducing the error).
Also delete any comments not relevant to the issue and see if you can
can get rid of the assert and if encode branch, and  simplify or leave
out some of the calculations without losing the error.


Is it because of the line
 gt = gt.scatter_(1, targets.to(dtype=torch.int64), 1)



In isolation, this in-place  scatter_() shouldn’t break anything.
(Note that the assignment to gt is redundant because gt is modified
in-place and gt.scatter_() merely returns the original gt object, as
modified.)


Is it necessary to perform gt.requires_grad = True since an entirely separate tensor is being created outside the graph?


No, this shouldn’t be necessary, and, in general, you don’t want to
be doing this.  In this specific case, doing so shouldn’t break anything,
but, more generally, you could be asking for trouble down the line.
As a general rule, if you find you need to set requires_grad = True
for anything other than an “upstream” tensor whose value you want
optimize, it’s a sign that you’re doing something wrong.
Best.
K. Frank