Mini-Batches: Keep or Drop the Leftovers

Data ProcessingEasy+10 XP+10

Mini-Batches: Keep or Drop the Leftovers

Training rarely feeds a model one example at a time, and almost never the whole dataset at once. It works through mini-batches: small consecutive chunks of the (already shuffled) example order.

The shuffling is done for you — order is the list of example positions, already in the order you should use. Your job is the chunking.

Task: write make_batches(order, batch_size, drop_last) returning a list of batches, where each batch is a list of positions taken from order in sequence.

  • Cut order into consecutive chunks of batch_size. The first batch is the first batch_size entries, the next batch the ones after that, and so on.
  • The final chunk is often short, because the dataset rarely divides evenly. drop_last decides what happens to it: True throws that short chunk away, False keeps it as a smaller batch.
  • A chunk that happens to be exactly batch_size long is never short, so drop_last leaves it alone.
  • If batch_size is larger than the whole list, you get one short batch — or, with drop_last=True, no batches at all.

Why anyone would throw data away: a short final batch gives a noisier gradient than the others, and some layers (batch normalisation especially) behave oddly on a batch of one. Dropping it keeps every step identical in shape.

Examples

order = [3, 0, 1, 4, 2]
batch_size = 2
drop_last = False
returns [[3, 0], [1, 4], [2]]
order = [3, 0, 1, 4, 2]
batch_size = 2
drop_last = True
returns [[3, 0], [1, 4]]

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