Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

20: Fixed shapes and padding

H2001

The last batch isn’t padded. The loader pads the last batch of a loop over batches(n) to n with masked-out examples, so that every step has the same shapes, only when every operation across the batch axis has a masked version. Here one doesn’t, so the last batch keeps its own size. The program is correct either way: this is a hint, not a warning.

data Ex:
    x: [4]

fn ranked(batch: [b] Ex) -> [b 4]:
    return sort(batch.x, over=b)

fn epoch(train: Dataset[Ex]):
    for batch in train |> batches(8):
        print(mean(ranked(batch)))
hint[H2001]: the last batch isn't padded to the batch size: `sort` over `b` has no masked version

To pad it, compute what needs the operation per example, or outside the loop. See Fixed shapes and padding, which lists the operations with masked versions.