I’m a little confused over your code, what is it you want to do?
It seems that you are writing a custom dataset but using datasets.MNIST, wouldn’t it be simpler to use torchvision to load MNIST and then change certain classes that are imbalanced in the way you want?
my_transforms = transforms.Compose([
    transforms.RandomRotation(degrees=5), 
    transforms.ToTensor(), 
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) 
    ])

dataset = torchvision.datasets.MNIST('./data', train=True, download=True, transform=my_transforms)
train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)

And then perform calculations on imbalanced classes