梯度下降算法不收敛

时间:2019-03-18 23:14:10

标签: python machine-learning artificial-intelligence classification logistic-regression

我正在尝试使用Python创建一个简单的分类器,这是我的代码(虽然不是全部,但我确定问题出在这部分)。

def grad(theta, X, y, m, lam):
    return (1 / m) * (X.T.dot(h(X, theta) - y)) + (lam / m) * theta

for i in range(400):
    # We shouldn't regularize the first theta so get a copy of it to change it back to normal
    first_t = theta[0]
    # Perform gradient descent
    theta = theta - a * grad(theta, X_train, Y_train, m_train, lam)
    # Change the value of the first theta to what it's supposed to be
    theta[0] = first_t - (a / m_train) * sum(X_train[:, 0] * (h(X_train, theta) - Y_train))

但是即使使用简单的训练案例,X = [[1,1],[1,2],[1,3],[1,4]] Y = [0,1,0,1]它表示一个数字是否为偶数(1表示是,0表示否)在经过400次迭代后仍然不收敛。我以为我会更改alpha,lambda或迭代次数的值,但没有作用,但是在如果您想知道a = 0.001lam = 0.0001! 预先感谢您的帮助!

0 个答案:

没有答案