What would be a pythonic (and PyTorchy) way of changing the behavior of __getitem__() for a custom PyTorch Dataset() object?

sometimes I want the dataloader to return samples from the dataset sequentially and other times in pairs, tuples etc? for now I added an attribute within the dataset class and I change the value of this attribute based on how I want __getitem__() to behave. Inside __getitem__() I check the value of this attribute and return samples accordingly… pretty ugly, I know, but any suggestions? software design patterns that may apply??

Use a custom sampler that signifies the dataset which way you want it to behave

Great pointer, thank you. I have not used custom samplers yet (learning Torch) I will look for examples.

1 Like