Use class weights when you have an imbalanced dataset and want to improve single-label classification results.
Class weights on the Peltarion Platform are inversely proportional to the class frequency in the training data.
Why use class weights
Class weights give all the classes equal importance on gradient updates, on average, regardless of how many samples we have from each class in the training data. This prevents models from predicting the more frequent class more often just because it’s more common.
On an imbalanced dataset containing 900 examples of class A and 100 images of class B, a classification model that always predicts A would achieve a relatively low loss and a 90% accuracy.
By scaling up the error from misclassified B examples, class weighting pushes models to learn a better representation of each class.
How to use class weights
Select the Target block of your model and make sure to check the Use class weights box. This enables class weighting in the calculation of the loss function.
If you want to learn more about how to measure the performance of your imbalanced dataset, read this article.
How does class weighting work
During model training, a total loss is computed for each batch, and the model parameters are then iteratively updated in a direction that reduces this loss. By default, each sample counts equally into this total loss.
With class weighting enabled, the sum is replaced by a weighted sum instead so that each sample contributes to the loss proportionally to the sample’s class weight.
The Peltarion Platform assigns class weights, which are inversely proportional to the class frequencies in the training data.
This means that samples belonging to the smaller class(es) give a higher contribution to the total loss. The learning algorithm will therefore focus equally on the smaller class(es) when the parameter update is performed. The model is prevented from over-classifying the larger class(es) simply based on their increased prior probability.
Test it on the platform
In the Use AI to detect fraud tutorial, we’ll show you how to use class weights.