虽然仅使用numpy库实现逻辑回归,但我为成本函数编写了以下代码:
#sigmoid function
def sigmoid(z):
sigma = 1/(1+np.exp(-z))
return sigma
#cost function
def cost(X,y,theta):
m = y.shape[0]
z = X@theta
h = sigmoid(z)
J = np.sum((y*np.log(h))+((1-y)*np.log(1-h)))
J = -J/m
return J
Theta是一个(3,1)数组,X是形状(m,3)的训练数据。 X的第一列是1。对于theta = [0,0,0],成本函数输出0.693,这是正确的成本,但是对于theta = [1,-1,1],它输出:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:5: RuntimeWarning: divide by zero encountered in log
"""
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:5: RuntimeWarning: invalid value encountered in multiply
"""
nan
我的梯度下降代码是:
#gradientdesc function
#alpha is the learning rate, iter is the number of iterations
def gradientDesc(X,y,theta,alpha,iter):
m = y.shape[0]
#d represents the derivative term
d = np.zeros((3,1))
for iter in range(iter):
h = sigmoid(X@theta) - y
temp = h.T.dot(X)
d = temp.T
d/=m
theta = theta - alpha*d
return theta
但是这不能给出正确的θ值。我该怎么办?
X
中的值大吗?这可能会导致sigmoid
返回接近零的值,从而导致您看到警告。看一下这个线程:Divide-by-zero-in-log
除非您解决此值爆炸问题,否则您的梯度下降将无法正常工作。我还将考虑在您的费用函数中添加正则化。
J += C * np.sum(theta**2)