Solved by Youyoun in post #2 
              
              
                You are assigning x[idx] to tmp_val, but it’s not a copy, it’s simply a view of your tensor at that index. this means that tmp_val is referencing the same memory space as x[idx], which means that changing x[idx] changes tmp_val. 

You’re adding h=1e-4 to x[idx], which means that tmp_val is now poin…