You could create a custom Dataset and return the index with the data and target tensors in the __getitem__ method. To do so, take a look at this tutorial 1 and or override the MNIST dataset with your custom __getitem__ implementation.