# class weights formula for imbalanced dataset

I am trying to make some semantic segmentation. I have 7 imbalanced classes in my case. I found several methods for handling Class Imbalance in a dataset is to perform Undersampling for the Majority Classes or Oversampling for the minority classes. but the most used one is introducing weights in the Loss Function. And I found several formula to calculate weights such us: wj=n_samples / (n_classes * n_samplesj) or wj=1/n_samplesj

which is the best one?

I really don't suggest Under/Oversampling as it would change the distribution of dataset. we should consider distribution as a useful feature of dataset. so I think the weighted loss would have better performance in most cases. if you're using TF/Keras, this link would be useful. you can use a variety of loss functions, like the below one, to apply the weight.

tf.nn.weighted_cross_entropy_with_logits(
labels, logits, pos_weight, name=None
)


A value pos_weight > 1 decreases the false negative count, hence increasing the recall. Conversely setting pos_weight < 1 decreases the false positive count and increases the precision.

• Thank you for your answer, Actually, I have to introduce manually the class weights as a Tensor, and I don't no which formula I have to use?
– safa
May 2 '21 at 18:25
• the one you mentioned in your question is the one which is also introduced in the link I put from TF and works okay based on my experience w_j=n_samples / (n_classes * n_samples_j). but you can think of it as a hyperparameter so using the resulted weights, you might still need some tests and trials to find the best values for weights in order to get the highest accuracy. May 2 '21 at 18:42
• In fact on three classes it works really well. but when I go to the seven classes, I have the impression that it no longer works, for example I have a tensor of weight = [1.0, 2.51, 8.52, 2.83, 168.53, 469.19, 1.35] for the seven classes , do we need to standardize them?
– safa
May 2 '21 at 21:33
• hmm, I guess it is better to reduce 168 and 469 to something less than 100 or 80. it needs a kind of test and trial. I just use the weight function to get an idea about weights. May 2 '21 at 22:11
• Thank you very much for your help !
– safa
May 3 '21 at 6:22