30

I have the following expression: log = np.sum(np.nan_to_num(-y*np.log(a+ 1e-7)-(1-y)*np.log(1-a+ 1e-7)))

it is giving me the following warning:

RuntimeWarning: invalid value encountered in log
  log = np.sum(np.nan_to_num(-y*np.log(a+ 1e-7)-(1-y)*np.log(1-a+ 1e-7)))

I don't understand what might be the invalid value or why am I getting it. Any and every help is appreciated.

NOTE: This is a cross-entropy cost function where I added 1e-7 to avoid having zeros inside log. y & a are numpy arrays and numpy is imported as np.

helix
  • 1,017
  • 3
  • 12
  • 30
  • Note that a similar warning is due to `np.log(np.NaN)`. E.g. when working with pandas data frames with missing values. – Giuppox Jul 25 '23 at 09:21

3 Answers3

32

You probably still have negative values inside the log, which gives nan with real numbers.

a and y should represent probability between 0 to 1, So you need to check why do you have smaller/larger values there. Adding 1e-7 shows there is something fishy, because np.log(0) gives -inf, which I think is the value you want.

Elad Joseph
  • 2,998
  • 26
  • 41
4

You can use math.log() replacing numpy.log(), which could raise error

>>> import numpy
>>> numpy.log(0)
-inf
>>> numpy.__version__
'1.3.0'
>>> import math
>>> math.log(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 2
    A minor comment: `math.log()` take one real number as an input while `numpy.log()` can take a list of real as input. Of course, this difference does not matter in OP's case. – RandomWalker Jun 13 '19 at 20:44
0

log(x) where x<=0 edge cases

np.log(0)

RuntimeWarning: divide by zero encountered in log

-inf
np.log(-1)

RuntimeWarning: invalid value encountered in log

nan
Kermit
  • 4,922
  • 4
  • 42
  • 74