You could create the weights using the code snippet from my previous post and replace the SubsetRandomSampler with the WeightedRandomSampler using the train_idx.
Since you are already using a subset via the train_idx, note that you would also only need to calculate the weights for these indices and use it in the WeightedRandomSampler.