KatInterview

Deterministic Mini-Batch Gradient Descent for a Line Fit

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashinggradient-descentlinear-regressionmini-batch

Last updated

Fit a slope and intercept to 2D points by running deterministic mini-batch gradient descent on mean squared error, with exact batching, update order and edge-case rules.

Fit a Line with Mini-Batch Gradient Descent

You are given a list of points (x, y) and must fit the line y_hat = slope * x + intercept by minimizing mean squared error. Implement a fixed, deterministic training procedure so the result is exactly reproducible.

Loss and gradients

For a batch B of points, the loss is

L = (1 / |B|) * sum over (x, y) in B of (slope * x + intercept - y)^2

Its gradients with respect to the parameters are

  • dL/dslope = (2 / |B|) * sum of (slope * x + intercept - y) * x
  • dL/dintercept = (2 / |B|) * sum of (slope * x + intercept - y)

Training procedure

  1. Start with slope = 0.0 and intercept = 0.0.
  2. Training runs for epochs epochs.
  3. In each epoch, walk through the points in the given order, without shuffling, and split them into consecutive batches of batch_size points. If the number of points is not a multiple of batch_size, the last batch of the epoch is smaller.
  4. For each batch, in order:
    • Compute both gradients using the current slope and intercept (before changing either one). Average over the actual number of points in that batch.
    • Update both parameters: param = param - learning_rate * gradient.
  5. Batches are processed in sequence, so each batch uses the parameters left by the previous batch.

Edge cases

  • If data is empty or epochs <= 0, return [0.0, 0.0].
  • If batch_size <= 0, treat it as 1.
  • If batch_size is at least the number of points, each epoch is a single full-batch step.

Task

Return the learned parameters as [slope, intercept] after all epochs finish. Floating-point results are compared against the expected values, so follow the procedure above exactly, including the order of operations per batch.

Constraints

  • data is a list of [x, y] pairs of real numbers; its length may be 0.
  • learning_rate is a positive real number.
  • epochs is an integer, possibly 0 or negative.
  • batch_size is an integer, possibly 0 or negative.
  • No shuffling, no learning-rate decay, no early stopping.

Examples

Example 1

Input: train_line_minibatch([[1, 3], [2, 5], [3, 7]], 0.05, 100, 1)

Output: [1.9999883766494664, 1.000034870051602]

Example 2

Input: train_line_minibatch([[0, 2], [1, 4], [2, 6], [3, 8]], 0.05, 50, 2)

Output: [2.0146224786564066, 1.9642415082297286]

Example 3

Input: train_line_minibatch([[-2, 5], [-1, 3], [0, 1], [1, -1], [2, -3]], 0.02, 100, 5)

Output: [-1.999521576250686, 0.9831296806411504]

Hints

Hint 1

Write the gradient for one batch first: both gradients share the residual `slope * x + intercept - y`; the slope gradient multiplies it by `x`.

Hint 2

Compute both gradients from the old parameters before updating either one, and divide by the real length of the current batch, which matters for a shorter last batch.

Hint 3

Use two nested loops: epochs on the outside, and slices `data[start:start + batch_size]` stepping by `batch_size` on the inside. Handle the empty-data, non-positive-epochs and non-positive-batch-size cases up front.