Penalize some classes more than others in cross entropy (but only for a specific class!)

I have three classes: small drone, large drone and bird.

I want my loss function to penalize small drone and large drone predictions equally when classifying a bird sample.
However, if the sample is actually a small drone – it should penalize large drone misclassification (small drone sample misclassified as large drone) less compared to a bird misclassification (small drone sample misclassified as bird).
Similarly, if the sample is actually a large drone – it should penalize small drone misclassification (large drone sample mis classified as small drone) less compared to a bird misclassification (large drone sample mis classified as bird).

How would I achieve this? Do I need to make a custom loss function or is this supported in pytorch?

You could do multi-stage binary classification.

  1. Stage: 0 for bird, 1 for drone
  2. Stage: only if drone in 1st stage.

Final loss would be combination of the above with additional weighting if required

Hi Rohit!

If I understand your question, you will need a custom loss function.

I believe that your use case is addressed by this post (and its entire thread):

Best.

K. Frank