I have an expression as below, where as data is a matrix and theta is a vector, python constantly raise divide by 0 error. But the only denominator is (1+e^x), every element in this matrix cannot be 0 anyway
np.sum(-data[:,-1]*np.log(1.0/(1.0+np.exp(-np.dot(data[:,:-1],theta.transpose())))))
Is there any occasion I've ignored that will generate this error?
Weird thing is, the program runs well and results is right even with this error showed every time.
From the error message given in the comments to the question:
py:21: RuntimeWarning: divide by zero encountered in log return (np.sum(-data[:,-1]*np.log(1.0/(1.0+np.exp(-np.dot(data[:,:-1],theta.transpose()))))-(1-data[:,-1])*np.log(1-1.0/(1.0+np.exp(-np.dot(data[:,:-1],theta.transpose())))))+np.sum(theta[1:]**2)*lamda/2.0)*1.0/data.shape[0]
it appears that you are computing np.log(0)
. E.g.
>>> import numpy as np
>>> np.log(0)
__main__:1: RuntimeWarning: divide by zero encountered in log
-inf
>>>
And that is likely happening because np.exp(...)
is overflowing, or 1.0 + np.exp(...)
is returning 1.0 (because np.exp(...)
is smaller than the "machine epsilon") and you are computing np.log(1.0 - 1.0/(1.0 + np.exp(...)))
.