aleemsidra:

Now the tensor has a unique weight for each of the target labels.


Don’t forget to create the weight for each sample, not only the classes, as shown in the example.



 aleemsidra:

Now for the intuition: the sampler picks the labels with higher weight more frequently in a batch than the samples with lower weight? So, overall in a batch, the sampling is balanced irrespective of the class distribution in the dataset?


Yes, the higher the weight the more likely this sample will be drawn. Also yes, you can balance the class distribution in a batch, but note that it’s still a random process so you shouldn’t expect to see a perfectly balanced batch in each iteration.



 aleemsidra:

I computed the weighted random sampler from the original dataset. After that, I split the dataset into the train and validation set. For both sets, the same sampler should be used. right? As it gives is weight is computed from overall distribution in the original set.


It depends on your use case and I don’t know if you want to balance the validation dataset. Usually it’s used as a proxy for the final test set (i.e. unknown, “real world” samples). If your final application would also use an imbalanced class distribution, you would have to decide which metric you care about.
My thinking would be to treat the validation dataset similar to the test one, so that you can indeed use the validation metric as a proxy for the test one, but let’s wait what others are thinking.