Classification loss function: how to implement individual weights for each observation and class 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!
What you are referring to is called a weighted loss function.
In Keras:


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}...
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.