You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

梯度下降参数学习异常排查:Python实现无法得到预期结果

梯度下降算法实现问题排查

我刚参加机器学习(ML)课程,正在用Python实现梯度下降(Gradient Descent)算法。以下sigmoid、compute_cost、compute_gradient函数测试均正常,但在实现参数学习的梯度下降主函数后,无法得到预期输出与正确参数,生成的决策边界不符合预期。现附上全部代码,请求排查错误。

def sigmoid(z):
    sigma = 1/(1+np.exp(-z))
    return sigma

def compute_cost(X, y, w, b):
    y_hat = sigmoid((X * np.expand_dims(w, axis=0)).sum(axis=1) + b)
    total_cost =  (-y * np.log(y_hat) - (1-y) * np.log(1-y_hat)).mean()
    return total_cost

def compute_gradient(X, y, w, b): 
    z = w * X + b
    yhat = sigmoid(z)
    
    y1 = np.expand_dims(y, axis=1)
    error = yhat - y1

    db = error.mean()
    dw_j1 = (X * error)
    dw_j = np.mean(dw_j1,axis=0)

    return dw_j, db

在构建梯度下降主函数前,我已用训练数据测试上述函数,输出结果均正确。

参数学习的梯度下降主函数

def gradient_descent(X, y, w, b, alpha, num_iters): 
    m = len(X)
    
    J_history = []
    wb_history = []
    
    for i in range(num_iters):
      cost = compute_cost(X, y, w, b)
      dw_j, db = compute_gradient(X, y, w, b)   

      w = w - alpha * dw_j               
      b = b - alpha * db 

      wb_history.append((w,b))
      J_history.append(cost)

    if i % math.ceil(num_iters/10) == 0 or i == (num_iters-1):
            print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f}")
  
    return w, b, J_history, wb_history


np.random.seed(1)
initial_w = 0.01 * (np.random.rand(2) - 0.5)
initial_b = -8

iterations = 10000
alpha = 0.001

w, b, J_history, _ = gradient_descent(X_train ,y_train, initial_w, initial_b, alpha, iterations)

内容的提问来源于stack exchange,提问作者the-odd-hedgehog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 12:45:47