This is a function

func = torch.sqrt((dudx - dudy)**2 + (dudy -dudx)**2)
where dudx, dudy is derivative of U wrt input x and y
Sine the square root of zero is infinite, so I want to impute some values of func where it is less than 0 by 0.1, and applied following function for it:
func[(func<=0.0).detach()] = 0.1
This is func value i got

> tensor([1.0000e-01, 1.0000e-01, 1.0000e-01, 1.0000e-01, 1.0000e-01, 1.0000e-01,
>         1.2064e-05, 4.1480e-06, 5.6752e-06, 1.0000e-01, 1.0000e-01, 9.2189e-06,
>         3.1551e-06, 4.3206e-06, 1.0000e-01, 1.0000e-01, 7.0325e-06, 2.4059e-06,
>         3.3041e-06, 1.0000e-01, 1.0000e-01, 1.0000e-01, 1.0000e-01, 1.0000e-01,
>         1.0000e-01], grad_fn=<IndexPutBackward>)

Now if i try to take derivative of func wrt inputs I get nan in place of imputed values as:
compute_grad(func, inputs)[:,0].reshape(nx,ny)

tensor([[        nan,         nan,         nan,         nan,         nan],
        [        nan, -1.7759e-05, -6.2834e-06, -8.6144e-06,         nan],
        [        nan, -1.3896e-05, -4.7949e-06, -6.5147e-06,         nan],
        [        nan, -1.0686e-05, -3.6399e-06, -4.9337e-06,         nan],
        [        nan,         nan,         nan,         nan,         nan]],
       grad_fn=<ViewBackward>)

Can someone help me to overcome this . I want the derivative of func without getting nan. Any help is appreciated. @smth, @ptrblck  could you please have a look