Logistic Regression: Full-Batch Gradient Descent

OptimizationMedium+20 XP+15

Logistic Regression: Full-Batch Gradient Descent

Logistic regression is the smallest model that still has every moving part of deep learning in it: a linear score, a squashing function, a loss, and gradient descent. Build the training loop once and the rest of the field looks familiar.

For each example the model predicts p = sigmoid(w·x + b). Training minimises the mean binary cross-entropy, and the gradients come out unusually clean:

  • dL/dw = (1/n) * Σ (p - y) * x
  • dL/db = (1/n) * Σ (p - y)

Task: write train_logistic(features, labels, learning_rate, epochs) that runs full-batch gradient descent and returns [weights, bias], with every number rounded to 4 decimal places.

  • Start from all-zero weights and a zero bias.
  • Every epoch is one pass over the whole dataset — compute all gradients from the current parameters, then update once.
  • epochs = 0 returns the starting parameters unchanged.

The (p - y) term is the whole trick. The sigmoid's derivative and the log-loss's derivative cancel almost perfectly, leaving plain prediction error — which is why this loss is paired with this activation and not another.

Examples

features = [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]
labels = [0, 0, 1, 1]
learning_rate = 0.5
epochs = 200
returns [[1.8114, 1.8114], -5.0866]
features = [[1.0], [2.0], [3.0], [4.0]]
labels = [0, 0, 1, 1]
learning_rate = 0.1
epochs = 50
returns [[0.4744], -0.625]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run