Hi Dhorka!



 Dhorka:

I am trying to apply a torch.sqrt to a vector that contains 0 values. My goal is to avoid the 0 values. I mean only a pply the torch.sqrt to values that are different of 0.


I speculate that you wish to avoid torch.sqrt (0) because the
derivative of sqrt is infinite at 0.


How can I solve it? Is there any way to achieve my goal easily?


If your goal is, in fact, to avoid the infinite derivative, you could
simply add a small “epsilon” to your value before calling sqrt:
epsilon = 1.e-8
t = torch.sqrt (t + epsilon)

Now the infinite derivative for (elements of) t = 0 just becomes
a large derivative (specifically 1 / (2 * sqrt (epsilon))).
epsilon should be chosen to be comfortably smaller than the
typical small values that show up in this part of your computation.
Best.
K. Frank