In the thread you posted is a valid solution:

  
    
    
    How to retrieve the sample indices of a mini-batch 
  
  
    One way to do this is to implement a subclass of torch.utils.data.Dataset that returns a triple (data, target, index) from its __getitem__ method. Then your loop would be: 
for data, target, index in train_loader:
    ....
  


Here is a small example using CIFAR10 from the other thread:

  
    
    
    How to retrieve the sample indices of a mini-batch 
  
  
    You could adapt this code: 
from torchvision import datasets, transforms
from torch.utils.data import Dataset, DataLoader

class MyDataset(Dataset):
    def __init__(self):
        self.cifar10 = datasets.CIFAR10(root='YOUR_PATH',
                                        download=False,
                                        train=True,
                                        transform=transforms.ToTensor())
        
    def __getitem__(self, index):
        data, target = self.cifar10[index]
  …