I have a “torch.utils.data.DataLoader”. I want to rearrange the order of the samples. Is it possible?
Advertisement
Answer
Yes, you can use torch.utils.data.Subset
and specify the indices.
import numpy as np import torch from torch.utils.data import DataLoader, Subset, TensorDataset data = np.arange(5) ** 2 dataset = TensorDataset(torch.tensor(data)) # Subset with entire Dataset in rearranged order dataset_ordered = Subset(dataset, indices=[2, 1, 3, 4, 0]) for x in DataLoader(dataset_ordered): print(x) # [tensor([4])] # [tensor([1])] # [tensor([9])] # [tensor([16])] # [tensor([0])]