One Line, Three Batch Sizes

Variants of Gradient DescentMedium+20 XP+20

One Line, Three Batch Sizes

Linear regression predicts one number from another with a straight line,

y^=b0+b1x\hat{y} = b_0 + b_1 xy^​=b0​+b1​x

and gradient descent finds b0b_0b0​ and b1b_1b1​ by repeatedly stepping downhill on the mean squared error. Batch, stochastic and mini-batch gradient descent are the same algorithm with one difference: how many rows each update looks at. Your job is a single training function that runs all three.

For a batch of mmm rows, the cost is

J=1m∑i=1m(y^i−yi)2J = \frac{1}{m}\sum_{i=1}^{m}\left(\hat{y}_i - y_i\right)^2J=m1​i=1∑m​(y^​i​−yi​)2

and its slopes with respect to the two parameters are

g0=2m∑i=1m(y^i−yi),g1=2m∑i=1m(y^i−yi)xig_0 = \frac{2}{m}\sum_{i=1}^{m}\left(\hat{y}_i - y_i\right), \qquad g_1 = \frac{2}{m}\sum_{i=1}^{m}\left(\hat{y}_i - y_i\right)x_ig0​=m2​i=1∑m​(y^​i​−yi​),g1​=m2​i=1∑m​(y^​i​−yi​)xi​

The training recipe

  • Start at b0=0b_0 = 0b0​=0 and b1=0b_1 = 0b1​=0.
  • One epoch is one pass through the data. Walk through the rows in the order given (no shuffling) and cut them into consecutive batches of batch_size rows. If the rows don't divide evenly, the last batch is smaller. If batch_size is bigger than the dataset, the only batch is the whole dataset.
  • For each batch, compute every prediction y^i\hat{y}_iy^​i​ with the current b0b_0b0​ and b1b_1b1​. Then compute g0g_0g0​ and g1g_1g1​, with mmm equal to the number of rows in that batch. Only then update both parameters, with α\alphaα = learning_rate:
b0←b0−α g0,b1←b1−α g1b_0 \leftarrow b_0 - \alpha\, g_0, \qquad b_1 \leftarrow b_1 - \alpha\, g_1b0​←b0​−αg0​,b1​←b1​−αg1​

Task: write fit_line(xs, ys, learning_rate, epochs, batch_size) and return the tuple (b0, b1) after epochs epochs, each rounded to 4 decimal places.

Setting batch_size = len(xs) gives batch gradient descent, batch_size = 1 gives stochastic gradient descent, and anything in between is mini-batch.

Run the same data with all three batch sizes and compare. Stochastic gradient descent makes len(xs) updates per epoch where batch gradient descent makes one, so it travels much further in a single pass. Where it ends up depends on the data. If every row sits exactly on one straight line, all the one-row steps agree and stochastic gradient descent lands on that line. If the rows are scattered, each one-row step tugs the line toward that single row, so with a fixed learning rate it never comes to rest: it repeats the same tugs every epoch, around a point away from the line batch gradient descent closes in on. The bigger the learning rate, the further away that point is.

examples used
1
updates made
0

Stochastic: one example, one step. Very fast, very noisy — each step is based on a single opinion.

Examples

xs = [1, 2, 3, 4]
ys = [3, 5, 7, 9]
learning_rate = 0.05
epochs = 1
batch_size = 4
returns (0.6, 1.75)
xs = [1, 2, 3, 4]
ys = [3, 5, 7, 9]
learning_rate = 0.05
epochs = 1
batch_size = 1
returns (1.0031, 1.9994)

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