You can use built in function torch.utils.data.random_split(dataset, lengths).
Check docs here: https://pytorch.org/docs/stable/data.html#torch.utils.data.random_split 16.4k
Check source here: https://pytorch.org/docs/stable/_modules/torch/utils/data/dataset.html#random_split 2.6k
Also, you can see this nice example 7.4k.