Problems
Loading...
1 / 1

Logistic Regression Training Loop

OptimizationLoss Functions
Medium

Train a binary logistic regression classifier using gradient descent. Implement the training loop and return the learned parameters (w, b).

Model:

p=σ(Xw+b)=11+e(Xw+b)p = \sigma(Xw + b) = \frac{1}{1 + e^{-(Xw + b)}}

Loss (Binary Cross-Entropy):

L=1Ni=1N[yilogpi+(1yi)log(1pi)]\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} [y_i \log p_i + (1 - y_i) \log(1 - p_i)]

Helper Functions Provided

  • _sigmoid(z): numerically stable sigmoid activation
Loading visualization...

Examples

Input: X=[[0],[1],[2],[3]], y=[0,0,1,1], lr=0.1, steps=500

Expected: Accuracy ≥ 95%

Hint 1

Compute gradients:w=XT(py)/N\nabla_w = X^T(p - y)/N and b=mean(py)\nabla_b = \text{mean}(p - y).

Hint 2

Update parameters:wwlrww \leftarrow w - \text{lr} \cdot \nabla_w andbblrbb \leftarrow b - \text{lr} \cdot \nabla_b. Repeat the steps.

Requirements

  • Initialize weights w as zeros and bias b as 0.0
  • Use gradient descent to minimize the loss function
  • Return tuple (w, b) where w is shape (D,) and b is a float
  • NumPy only, no sklearn or other ML libraries

Constraints

  • Input sizes: N ≤ 200, D ≤ 10
  • lr > 0, steps ≥ 1
  • Time limit: 1000 ms; Memory ≤ 128 MB
Try Similar Problems