Same Data, Different Handfuls

OptimizersMedium+20 XP+20

Same Data, Different Handfuls

Batch size is a dial between two costs. Small batches give many updates per pass through the data, but each one points in a wobbly direction. Large batches give steady directions, but only a few updates. This problem measures both sides of the dial on real numbers.

A one-weight model predicts y^=w x\hat{y} = w\,xy^​=wx and scores each example by its squared error (w xi−yi)2(w\,x_i - y_i)^2(wxi​−yi​)2. That example's gradient with respect to www is

gi=2 xi (w xi−yi)g_i = 2\,x_i\,(w\,x_i - y_i)gi​=2xi​(wxi​−yi​)

To measure the noise without training getting in the way, the weight is frozen at a single value www and never updated. The examples are read in the order given, which is already shuffled.

For one batch size BBB:

  • Split the examples into consecutive batches of BBB, in order. The last batch takes whatever is left over, so it can be smaller than BBB. It is never dropped. A batch size at least as large as the dataset gives a single batch that holds everything.
  • A batch's gradient g(k)g^{(k)}g(k) is the average of gig_igi​ over the examples actually in that batch. Each batch would produce one update, so the number of batches KKK is the number of updates in one pass.
  • The full-batch gradient GGG is the average of gig_igi​ over all nnn examples.
  • The noise is the root-mean-square gap between the batch gradients and GGG:

noise=1K∑k=1K(g(k)−G)2\text{noise} = \sqrt{\frac{1}{K}\sum_{k=1}^{K}\left(g^{(k)} - G\right)^2}noise=K1​∑k=1K​(g(k)−G)2​

Task: write batch_size_sweep(xs, ys, w, batch_sizes). For each batch size in batch_sizes, in the order given, return the pair [updates, noise]: the number of batches KKK as a whole number, and the noise rounded to 4 decimal places. The result is a list of these pairs.

xs and ys have the same length, at least 1. Every batch size is a whole number of at least 1.

Worked through, batch_size_sweep([1.0, 2.0, 3.0], [1.0, 3.0, 2.0], 1.0, [2]):

examplexix_ixi​yiy_iyi​gi=2xi(wxi−yi)g_i = 2x_i(w x_i - y_i)gi​=2xi​(wxi​−yi​)
1112⋅1⋅(1−1)=02 \cdot 1 \cdot (1 - 1) = 02⋅1⋅(1−1)=0
2232⋅2⋅(2−3)=−42 \cdot 2 \cdot (2 - 3) = -42⋅2⋅(2−3)=−4
3322⋅3⋅(3−2)=62 \cdot 3 \cdot (3 - 2) = 62⋅3⋅(3−2)=6

G=(0−4+6)/3=0.6667G = (0 - 4 + 6)/3 = 0.6667G=(0−4+6)/3=0.6667. With B=2B = 2B=2 the batches are examples 1–2, with gradient (0−4)/2=−2(0 - 4)/2 = -2(0−4)/2=−2, and example 3 alone, with gradient 6/1=66/1 = 66/1=6. So K=2K = 2K=2 and

noise=12[(−2−0.6667)2+(6−0.6667)2]=12(7.1111+28.4444)=4.2164\text{noise} = \sqrt{\tfrac{1}{2}\left[(-2 - 0.6667)^2 + (6 - 0.6667)^2\right]} = \sqrt{\tfrac{1}{2}(7.1111 + 28.4444)} = 4.2164noise=21​[(−2−0.6667)2+(6−0.6667)2]​=21​(7.1111+28.4444)​=4.2164

The answer is [[2, 4.2164]].

Running several batch sizes over the same data puts both costs side by side: how many updates a pass buys, and how far each of those updates strays from the direction the whole dataset would have given.

Examples

xs = [-2.8, 1.0, 1.6, 0.4, 2.3, -1.1, 1.2, 0.6]
ys = [-6.4, 1.6, 4.2, -0.8, 3.4, -2.0, 3.6, 1.7]
w = 1.5
batch_sizes = [1, 2, 4, 8]
returns [[8, 4.1904], [4, 2.1568], [2, 1.4175], [1, 0.0]]
xs = [0.5, -1.0, 2.0, 1.5, -2.5, 3.0, 1.0]
ys = [1.2, -1.8, 4.4, 2.6, -5.3, 6.1, 2.4]
w = 1.0
batch_sizes = [1, 3, 4, 7]
returns [[7, 6.438], [3, 4.1913], [2, 4.0406], [1, 0.0]]

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