20am847:

       tmp_val = x[idx]



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.



 20am847:

x[idx] = float(tmp_val) + h


You’re adding h=1e-4 to x[idx], which means that tmp_val is now pointing to the value x[idx]+h which is 3.0001 and 4.0001
Numpy index selection on the other hand doesn’t seem to give a view, but instead a copy (I thought it also provides a view, but maybe for one item it gives the copy).
This is why your code with numpy works as intended. To remedy to the problem in pytorch, simply use  tmp_val = x[idx].clone() to copy the tensor. This should yield the result you wanted.
One more thing: numpy by default uses double (float64) for float values, while pytorch uses float (float32). This means that your operations might not give the same results in terms of precision. You can set the default precision as per this
Hope this answers your question.