๐ŸŽ“ A1Learner
๐Ÿ”

PyTorch Splitting Data

Splitting data holds back some rows as a validation slice. Training uses the rest, and the held-back rows give an honest score.

A shuffled index

randperm returns the row numbers in a mixed order. A fixed seed makes the mix repeatable.

generator = torch.Generator().manual_seed(3)
order = torch.randperm(10, generator=generator)
print(order.tolist())

Cut the index in two

Slicing the mixed index cuts it into two parts. The first part names the training rows.

train_index = order[:8]
val_index = order[8:]
print(len(train_index))
print(len(val_index))

Pick rows by index

An index tensor in square brackets picks those rows. Each part holds only its own rows.

rows = torch.arange(10, dtype=torch.float32)
print(rows[train_index].tolist())
print(rows[val_index].tolist())

Example

import torch

rows = torch.arange(100, dtype=torch.float32).reshape(-1, 1)
targets = rows * 2.0

generator = torch.Generator().manual_seed(7)
order = torch.randperm(100, generator=generator)
train_rows = rows[order[:80]]
val_rows = rows[order[80:]]

print("Training rows:", len(train_rows))
print("Validation rows:", len(val_rows))
print("First training indexes:", order[:5].tolist())
print("First validation indexes:", order[80:85].tolist())

Key points

  • Validation slice: rows held back from training, used only to score the model
  • Use torch.randperm to shuffle row numbers with a fixed seed
  • Use an index tensor in square brackets to pick rows by number
  • An 80/20 split sends 80 of 100 rows to training and 20 to validation
  • Held-back rows stay out of training, so their score is honest

In this example, rows holds 100 rows and targets holds the matching answers. A generator seeded at 7 shuffles the row numbers with randperm. The first 80 numbers pick the training rows. The last 20 numbers pick the validation rows. The prints confirm the sizes and show the first indexes of each part.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

Why are validation rows held back from training?