Categorical Cross-Entropy Loss
Last updated
Last updated
Also called Softmax Loss. It is a Softmax activation plus a Cross-Entropy loss. If we use this loss, we will train a CNN to output a probability over the classes for each image. It is used for multi-class classification.
In the specific (and usual) case of Multi-Class classification the labels are one-hot, so only the positive class keeps its term in the loss. There is only one element of the Target vector which is not zero . So discarding the elements of the summation which are zero due to target labels, we can write:
Where Sp is the CNN score for the positive class.
Defined the loss, now we’ll have to compute its gradient respect to the output neurons of the CNN in order to backpropagate it through the net and optimize the defined loss function tuning the net parameters. So we need to compute the gradient of CE Loss respect each CNN class score in ss. The loss terms coming from the negative classes are zero. However, the loss gradient respect those negative classes is not cancelled, since the Softmax of the positive class also depends on the negative classes scores.
After some calculus, the derivative respect to the positive class is:
And the derivative respect to the other (negative) classes is:
Caffe: SoftmaxWithLoss Layer. Is limited to multi-class classification.
Pytorch: CrossEntropyLoss. Is limited to multi-class classification.
TensorFlow: softmax_cross_entropy. Is limited to multi-class classification.
In this Facebook work they claim that, despite being counter-intuitive, Categorical Cross-Entropy loss, or Softmax loss worked better than Binary Cross-Entropy loss in their multi-label classification problem.
→ Skip this part if you are not interested in Facebook or me using Softmax Loss for multi-label classification, which is not standard.
The gradient has different expressions for positive and negative classes. For positive classes:
For negative classes:
This expressions are easily inferable from the single-label gradient expressions.
As Caffe Softmax with Loss layer nor Multinomial Logistic Loss Layer accept multi-label targets, I implemented my own PyCaffe Softmax loss layer, following the specifications of the Facebook paper. Caffe python layers let’s us easily customize the operations done in the forward and backward passes of the layer:
For full code, take a look at here.
The Caffe Python layer of this Softmax loss supporting a multi-label setup with real numbers labels is available here
The gradient expression will be the same for all except for the ground truth class , because the score of is in the nominator.
Where snsn is the score of any negative class in different from CpCp.
When Softmax loss is used is a multi-label scenario, the gradients get a bit more complex, since the loss contains an element for each positive class. Consider are the positive classes of a sample. The CE Loss with Softmax activations would be:
Where each in is the CNN score for each positive class. As in Facebook paper, I introduce a scaling factor to make the loss invariant to the number of positive classes, which may be different per sample.
Where is the score of any positive class.