TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
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)}}p=σ(Xw+b)=1+e−(Xw+b)1​

Loss (Binary Cross-Entropy):

L=−1N∑i=1N[yilog⁡pi+(1−yi)log⁡(1−pi)]\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} [y_i \log p_i + (1 - y_i) \log(1 - p_i)]L=−N1​i=1∑N​[yi​logpi​+(1−yi​)log(1−pi​)]

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(p−y)/N\nabla_w = X^T(p - y)/N∇w​=XT(p−y)/N and ∇b=mean(p−y)\nabla_b = \text{mean}(p - y)∇b​=mean(p−y).

Hint 2

Update parameters:w←w−lr⋅∇ww \leftarrow w - \text{lr} \cdot \nabla_ww←w−lr⋅∇w​ andb←b−lr⋅∇bb \leftarrow b - \text{lr} \cdot \nabla_bb←b−lr⋅∇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
Cross Entropy LossLog Loss Per SampleSigmoid NumpySoftmax FunctionLinear Regression Closed Form

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

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)}}p=σ(Xw+b)=1+e−(Xw+b)1​

Loss (Binary Cross-Entropy):

L=−1N∑i=1N[yilog⁡pi+(1−yi)log⁡(1−pi)]\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} [y_i \log p_i + (1 - y_i) \log(1 - p_i)]L=−N1​i=1∑N​[yi​logpi​+(1−yi​)log(1−pi​)]

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(p−y)/N\nabla_w = X^T(p - y)/N∇w​=XT(p−y)/N and ∇b=mean(p−y)\nabla_b = \text{mean}(p - y)∇b​=mean(p−y).

Hint 2

Update parameters:w←w−lr⋅∇ww \leftarrow w - \text{lr} \cdot \nabla_ww←w−lr⋅∇w​ andb←b−lr⋅∇bb \leftarrow b - \text{lr} \cdot \nabla_bb←b−lr⋅∇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
Cross Entropy LossLog Loss Per SampleSigmoid NumpySoftmax FunctionLinear Regression Closed Form

Sign in to take notes on this problem

Case 1

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.