In that case you could write a custom Dataset, deriving from the MNIST dataset, and return the sample index with the actual data and target sample.
In the provided code snippet, you would get the additional class index and could store it for further processing.