📘
Custom Dataset Class
MediumPyTorch Datasets
Problem Statement
Create a custom Dataset class that generates data on-the-fly instead of storing it all in memory.
Background
For large datasets, storing everything in memory isn't practical. PyTorch's Dataset interface lets you generate or load data on demand by implementing three methods: init, len, and getitem.
Your Task
Create a class SequenceDataset that generates sequential data. For index i, it should return:
- Feature: a tensor of [i, i+1, i+2]
- Label: the sum of the feature values
The class must accept a size parameter that determines how many samples it contains.
Output Format
Each sample should be a tuple of (feature_tensor, label_tensor).
Example:
Input:
features=[[1.0, 2.0], [3.0, 4.0]], labels=[0, 1]
Output:
Dataset with 2 samples, dataset[0] returns (tensor([1., 2.]), tensor(0))
Reasoning:
We create a Dataset class that stores features and labels and returns them as tensors
Constraints:
- Features and labels lists will have the same length
- Features can be 1D or 2D lists
- Labels are integers
- Return tensors (not lists) from getitem
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.