What's the correct way to calculate sample weight in a multi-task model?

Concretely, I have a model that outputs a 400 class [multi-class] classification, as well as a 5 class multi-label classification. The classes are all disproportional and out of balance. Without any sample weighting, the multi-class classifier gets ~90% top 3 accuracy, and good AUC on some of classes in the multi-label classifications. The goal is to improve performance in one of the classes in the multi-label classification task. This class is fairly unbalanced.

What I'm attempting is to set the sample weights to a fixed value (say, 0.5) when the class of interest is not set and some other arbitrary fixed value (say, 2.5) when it is.


1 Answer 1


In keras, there is a class_weight argument. Create a dictionary of weights per class and then pass that into the .fit method of the model.

class_weight = {0:.5, 1:2.5, 2:2.5, 3:2.5}

    validation_data=(val_features, val_targets),

The weight values can be hand-picked or tuned like any other hyperparameter.

  • $\begingroup$ Thanks, but I'm looking for sample weight, not class weight. Specifically, what if the model outputs multiple outputs (or non-softmaxed single output)? $\endgroup$ May 25, 2023 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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