Hi,
The error states that you are modifying inplace a Tensor that you are slicing. So an op like your_tensor[ind] = xxx.
It states that because the original value of this Tensor is needed for backward computations, this is not valid.
You should replace this by and out of place operations.
In particular, in your last for-loop, you don’t mneed to do grid[i,:,:,:,:] = xxx (an inplace op) you can simply store that in a temporary Tensor and give it to the next line grid_elem = xxx.
If that does not fix the issue.
For the grid sample that fills in output_2, I usually store all the results in a list and then torch.cat() the result to get the final result.