Minibatch training with class weight

You could use a WeightedRandomSampler as described here. Note that it’s a random sampling process and thus a perfectly balanced batch is not guaranteed.