← 返回 databricks 的题目列表Implement Gradient Descent for Linear Regression (MSE) and debug convergence
类型:online_judge
Implement linear regression trained by gradient descent.
Given X of shape (n_samples, n_features) and y of shape (n_samples,), use the model: y_pred = X @ w + b.
Use Mean Squared Error: MSE(w,b) = (1/n) * sum_i (y_i - (x_i^T w + b))^2.
Requirements:
Derive and implement gradients for w and b and update them with gradient descent.
Inputs include learning rate lr and max_iter.
Implement early stopping (e.g., stop if loss improvement < tol, optionally with patience).
Be able to debug non-converging code (common issues: using SSE instead of MSE, missing the mean, missing factors like 2/n, not updating bias, wrong broadcasting/shapes, overly large learning rate).
Constraints: n_samples up to 1e5, n_features up to 200.
Example
Input
X=[[1],[2],[3]]
y=[2,4,6]
lr=0.1
max_iter=2000
tol=1e-12
patience=20
Output
w≈[2.0], b≈0.0