PyTorch What Is a Dataset
A dataset is a set of rows, and each row pairs an input with a target. The target is the right answer for that input.
One row of a dataset
One row pairs one input with one target. This row pairs 4 hours of study with a score of 71.
row_input = torch.tensor([4.0])
row_target = torch.tensor(71.0)
print(row_input)
print(row_target)Two aligned tensors
Two tensors can hold a whole dataset. Row i of the targets answers row i of the features.
features = torch.tensor([[2.0], [3.0], [4.0]])
targets = torch.tensor([55.0, 62.0, 71.0])
print(features.shape)
print(targets.shape)Rows in order
Matching indexes read matching rows. The same index works in both tensors.
for i in range(3):
print(i, features[i], targets[i])Example
import torch
features = torch.tensor([[2.0], [3.0], [4.0], [5.0], [6.0]])
targets = torch.tensor([55.0, 62.0, 71.0, 79.0, 88.0])
print("Rows:", len(features))
print("Feature shape:", features.shape)
print("Target shape:", targets.shape)
for i in range(len(features)):
hours = features[i].item()
score = targets[i].item()
print(f"Row {i}: input {hours:.0f} hours, target {score:.0f}")Key points
- Dataset: a set of rows, each row pairing an input with its target
- Input: the numbers the model reads for one row
- Target: the right answer the model should learn to give
- Aligned tensors: two tensors where row i of one matches row i of the other
- Each row holds one input and its target, in the same order in both tensors
In this example, features holds one input per student. The inputs are hours studied, shaped as one column. targets holds one score per student, in the same order. Row 0 pairs 2 hours with 55, and row 4 pairs 6 hours with 88. The two tensors form one small dataset because their rows stay aligned.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What makes two tensors a dataset?