The problem I have to solve is a classification problem. The costs of a misclassification are very different (but known) for the various observations, so I plan to include them by assigning weights to each observation accordingly. My issue is that additionally, the costs of misclassification are different for different classes (and these differences depend on the observations). So in theory, I would need to incorporate into the loss function weights $w_{ij}$ for each observation $i$ and class $j$. But I have no idea how to do this for example for a neural network in keras (or with any other classifier I didn't build from scratch myself).

Any ideas on how to do this would be greatly appreciated!

  • $\begingroup$ Maybe you should be more specific, many algorithms do have an argument to pass case weights or something similar already implemented. $\endgroup$ – user2974951 Dec 21 '18 at 11:38

What you are referring to is called a weighted loss function. In Keras:

  1. Define a dictionary with your labels and their associated weights or just a list of the weights (by class order):

    loss_weights = {0: 0.5, 1: 0.2, 2: 1.5}...

  2. Feed the it to the compile method:

    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics='acc', loss_weights=loss_weights)

The loss value that will be minimized by the model will then be the weighted sum of all individual losses, weighted by the loss_weights coefficients.

  • $\begingroup$ Thank you for the input, but as far as I can tell, loss weights are the same for each observation, e.g. the loss weight for the class 0 is always 0.5. I'm looking for something similar, where each combination of (observation, class) can have an individual weight. $\endgroup$ – nope Dec 21 '18 at 12:20
  • $\begingroup$ You mean that for class A, you have an individual set of weighted losses and for class B you have another different set of weighted losses, and so on? $\endgroup$ – Mark.F Dec 21 '18 at 12:57
  • $\begingroup$ Yes, that would be ideal! I'm afraid that otherwise the cost function of the model and the real world costs are too different to give me useful results $\endgroup$ – nope Dec 21 '18 at 13:19
  • $\begingroup$ I don't have any personal experience with this but there is a similar thread at github.com/keras-team/keras/issues/2115. They give there a lot of code solutions. I'm sure you could find one compatible with what you need. $\endgroup$ – Mark.F Dec 21 '18 at 13:24

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.