TL;DR
Specify the minimum value (maximum value) with np.clip
!
In the float type of python, the value less than 1e-323
will be 0.0.
The python float type can range from -1.7976931348623157e + 308 to 1.9796931348623157e + 308 in many environments.
$ python
>>> a = 1e-323
>>> b = 1e-324
>>> print(a)
1e-323
>>> print(b)
0.0
Since b becomes 0.0, if you log for this b, an error will occur and it will become -inf. Also, a 0% warning will appear.
>>> import numpy as np
>>> b = 1e-324
>>> np.log(b)
__main__:1: RuntimeWarning: divide by zero encountered in log
-inf
Specify the minimum value (maximum value) with np.clip
.
>>> import numpy as np
>>> b = 1e-324
>>> np.log(np.clip(a=b, a_min=1e-323, a_max=1e+10))
-743.7469247408213
By specifying the minimum value (maximum value), it will be possible to calculate even if a value outside the float range is entered. However, values that exceed the minimum value (maximum value) are rounded to the set values, which has the disadvantage of increasing the error.
Recommended Posts