Differences with torch.utils.data.SubsetRandomSampler
torch.utils.data.SubsetRandomSampler
class torch.utils.data.SubsetRandomSampler(indices, generator=None)
For more information, see torch.utils.data.SubsetRandomSampler.
mindspore.dataset.SubsetRandomSampler
class mindspore.dataset.SubsetRandomSampler(indices, num_samples=None)
For more information, see mindspore.dataset.SubsetRandomSampler.
Differences
PyTorch: Samples the elements randomly from a sequence of indices, random generator can be set manually.
MindSpore: Samples the elements randomly from a sequence of indices, random generator is not supported.
Categories |
Subcategories |
PyTorch |
MindSpore |
Difference |
---|---|---|---|---|
Parameter |
Parameter 1 |
indices |
indices |
- |
Parameter 2 |
generator |
- |
Specifies sampling logic. MindSpore uses global random sampling. |
|
Parameter 3 |
- |
num_samples |
The number of samples obtained, which can be used to partially obtain the samples obtained by sampling |
Code Example
import torch
from torch.utils.data import SubsetRandomSampler
torch.manual_seed(0)
class MyMapDataset(torch.utils.data.Dataset):
def __init__(self):
super(MyMapDataset).__init__()
self.data = [i for i in range(4)]
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
ds = MyMapDataset()
sampler = SubsetRandomSampler(indices=[0, 2])
dataloader = torch.utils.data.DataLoader(ds, sampler=sampler)
for data in dataloader:
print(data)
# Out:
# tensor([2])
# tensor([0])
import mindspore as ms
from mindspore.dataset import SubsetRandomSampler
ms.dataset.config.set_seed(1)
class MyMapDataset():
def __init__(self):
super(MyMapDataset).__init__()
self.data = [i for i in range(4)]
def __getitem__(self, index):
return self.data[index]
def __len__(self):
return len(self.data)
ds = MyMapDataset()
sampler = SubsetRandomSampler(indices=[0, 2])
dataloader = ms.dataset.GeneratorDataset(ds, column_names=["data"], sampler=sampler)
for data in dataloader:
print(data)
# Out:
# [Tensor(shape=[], dtype=Int64, value= 2)]
# [Tensor(shape=[], dtype=Int64, value= 0)]