Splitting data with LightningDataModule
You will complete the setup method in a LightningDataModule. Proper dataset partitioning ensures that the model is trained on one subset and validated on another, preventing overfitting.
The dataset has already been pre-imported.
本练习是课程的一部分
Scalable AI Models with PyTorch Lightning
练习说明
- Import
random_splitto split the dataset into training and validation. - Split the dataset into training (80%) and validation (20%) using
random_split.
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import libraries
import lightning.pytorch as pl
from torch.utils.data import ____
class SplitDataModule(pl.LightningDataModule):
def __init__(self):
super().__init__()
self.train_data = None
self.val_data = None
def setup(self, stage=None):
# Split the dataset into training (80%) and validation (20%)
self.____, self.____ = random_split(dataset, [____, ____])