Skip to content
mlmentorship

Mini-Batches

Split examples into batches without dropping the final short batch.

Published · 4 min read ·Role-specific ·Intermediate

30-second answer map

Visual first · depth when needed

Advance one start cursor by batch size and yield the half-open slice; sequence slicing automatically clips the final end.

Preparing the visual…

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

How practice works

ML breadth · closed-book attempt

Mini-Batches

Explain the mechanism, why it works, when it fails, and one alternative.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Split examples into batches without dropping the final short batch.

Start with the concrete trace below. It shows the state the algorithm must carry as it runs.

Problem trace

Mini-Batches: Advance one start cursor by batch size and yield the half-open slice; sequence slicing automatically clips the final end.

Input and goalSplit examples into batches without dropping the final short batch.
Validate the batch sizeFor items [0,1,2,3,4,5,6] and batch_size=3, the positive-size guard passes.
start=00011last included=22233445566
batchSize3halfOpenSlice[0:3]guard3 > 0nextStart0

Recognize it
Use it when every item must appear once in consecutive batches and the final batch may be shorter than the requested size.
Keep true
Before each yield, indexes below start have appeared exactly once; items[start:start+batch_size] contributes the next non-overlapping range, clipped only by sequence length.
Reuse it
Use half-open slices and a fixed stride to partition ordered data without overlap; let bounded slicing handle a short tail explicitly.
Read it this way: For items [0,1,2,3,4,5,6] and batch_size=3, the positive-size guard passes. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.

Pattern: Step through a sequence by batch size.

Simple idea: Slice from each start position to start + batch_size. Python stops the last slice at the sequence end.

from collections.abc import Iterator, Sequence

def batches(items: Sequence, batch_size: int) -> Iterator[Sequence]:
   if batch_size <= 0:
      raise ValueError("batch_size must be positive")
   for start in range(0, len(items), batch_size):
      yield items[start : start + batch_size]

Cost: total iteration time and output per step.