15/30
Part 4 of 6 · Positions, targets, shapes and twinsLearn and do
Targets are the inputs shifted by one
Some bugs live outside the model, in the data loading.
def get_batch(
data: torch.Tensor, block_size: int, batch_size: int, generator: torch.Generator | None = None
) -> tuple[torch.Tensor, torch.Tensor]:
"""The practice's. data: (N,) token ids. Returns x and y, each (batch_size, block_size)."""
starts = torch.randint(0, len(data) - block_size - 1, (batch_size,), generator=generator)
x = torch.stack([data[s:s + block_size] for s in starts])
y = torch.stack([data[s:s + block_size] for s in starts])
return x, y
# the token after n is n + 1
counting = torch.arange(1_000)
x, y = get_batch(counting, 8, 4, torch.Generator().manual_seed(0))
print(x[0].tolist())
print(y[0].tolist())
print(torch.equal(y, x + 1))
counting is 0 to 999, so the token after n is n + 1. Read the practice's get_batch above. What does torch.equal(y, x + 1) print?
Pick one to see what it prints.
Pick an answer above to see the rest