I have something like this:
import numpy as np
x = np.random.rand(10) # I risk a zero entry
log_x = np.log(x) # Risk of a divide by 0
What is the best way to enforce numerical stability here in the np.log
call?
Current Solution
import numpy as np
def safe_log(x: np.ndarray) -> np.ndarray:
while not x.all():
x = np.nextafter(x, 1)
return np.log(x)
source https://stackoverflow.com/questions/73101705/enforcing-numerical-stability-in-numpy-log
Comments
Post a Comment