I have a "torch.utils.data.DataLoader". I want to rearrange the order of the samples. Is it possible?
Asked
Active
Viewed 1,198 times
0

ted
- 13,596
- 9
- 65
- 107

odbhut.shei.chhele
- 5,834
- 16
- 69
- 109
-
By rearrange, do you mean shuffle at every epoch? You can set `shuffle=True` to achieve this. – Jake Tae Mar 28 '22 at 03:12
-
But I don't want to randomly shuffle them. I want to re order them in a particular way. – odbhut.shei.chhele Mar 28 '22 at 04:34
-
@JakeTae But I don't want to randomly shuffle them. I want to re order them in a particular way. – odbhut.shei.chhele Mar 28 '22 at 04:34
-
Then you have to reorder the `Dataset` which is used to initialize the `Dataloader`. Your custom `Dataset` class should have an `__getitem__` method, which determines the order of how the samples are retrieved. – Jake Tae Mar 28 '22 at 05:34
-
@JakeTae Yes it is there. Is there any code example that I can see? Thank you very much. – odbhut.shei.chhele Mar 28 '22 at 15:13
1 Answers
1
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])]

hwaxxer
- 3,363
- 1
- 22
- 39