You could create a custom sampler, which is responsible to create the indices passed to Dataset.__getitem__. This custom sampler could use the target to create the valid indices for each batch such that the desired classes will be used.