I am trying to set class weights for a neural network with an imbalanced dataset.

Let's say I have the following values: I have 8000 images of class A, 1100 images of class B, 400 images of class C, and and 20 images of class D. Then how would I set class weights so that all classes are equally weighted?

My approach would be to do the following. If I wanted to find the class weight that I should assign to class A, then $$(\frac{A}{A+B+C+D})^{-1} = (\frac{8000}{8000+1100+400+20})^{-1}$$

Is this a good way to do it? Essentially I'm taking the reciprocal of the ratio.


An easy way to do this would be to simply assign weights so that they upweighted classes all have equal weight to the unweighted largest class. So in your case you would assign a weight of A/B to B, A/C to C, A/D to D and not weight A at all.

  • $\begingroup$ I think there's a quick typo in your answer. I think it should A/B to B. Also, is this a common practice? $\endgroup$ – Christian Jul 9 '18 at 18:44

Looks like you have a very small data set. It may behoove you to use some form of data augmentation; it would greatly improve your model's performance and assist with the imbalance in your data set.

Using data augmentation, you can over sample those classes (i.e. A, C) with smaller number of samples to balance out your data set. This may not completely fix the weighting problem, but should give you a good start.

This technical report describes some benefits of data augmentation, see: The Effectiveness of Data Augmentation in Image Classification using Deep Learning


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.