Differences with torch.utils.data.SequentialSampler

View Source On Gitee

torch.utils.data.SequentialSampler

class torch.utils.data.SequentialSampler(data_source)

For more information, see torch.utils.data.SequentialSampler.

mindspore.dataset.SequentialSampler

class mindspore.dataset.SequentialSampler(start_index=None, num_samples=None)

For more information, see mindspore.dataset.SequentialSampler.

Differences

PyTorch: Samples elements sequentially.

MindSpore: Samples elements sequentially. Support for specifying sequential indexing and sample size.

Categories

Subcategories

PyTorch

MindSpore

Difference

Parameter

Parameter1

data_source

-

Dataset object to be sampled. MindSpore does not need this parameter.

Parameter2

-

start_index

Index to start sampling at

Parameter3

-

num_samples

Specify the number of samples returned by the sampler

Code Example

import torch
from torch.utils.data import SequentialSampler

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 = SequentialSampler(ds)
dataloader = torch.utils.data.DataLoader(ds, sampler=sampler)

for data in dataloader:
    print(data)
# Out:
# tensor([0])
# tensor([1])
# tensor([2])
# tensor([3])
import mindspore as ms
from mindspore.dataset import SequentialSampler

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 = SequentialSampler()
dataloader = ms.dataset.GeneratorDataset(ds, column_names=["data"], sampler=sampler)

for data in dataloader:
    print(data)
# Out:
# [Tensor(shape=[], dtype=Int64, value= 0)]
# [Tensor(shape=[], dtype=Int64, value= 1)]
# [Tensor(shape=[], dtype=Int64, value= 2)]
# [Tensor(shape=[], dtype=Int64, value= 3)]