Lost Functions
Understanding Categorical Cross-Entropy Loss, Binary Cross-Entropy Loss, Softmax Loss, Logistic Loss, Focal Loss and all those confusing names.
People like to use cool names which are often confusing. When I started playing with CNN beyond single label classification, I got confused with the different names and formulations people write in their papers, and even with the loss layer names of the deep learning frameworks such as Caffe, Pytorch or TensorFlow. In this post I group up the different names and variations people use for Cross-Entropy Loss. I explain their main points, use cases and the implementations in different deep learning frameworks.
First, let’s introduce some concepts:
Tasks
Multi-Class Classification
One-of-many classification. Each sample can belong to ONE of C classes. The CNN will have C output neurons that can be gathered in a vector s (Scores). The target (ground truth) vector t will be a one-hot vector with a positive class and C − 1 negative classes. This task is treated as a single classification problem of samples in one of C classes.
Multi-Label Classification
Each sample can belong to more than one class. The CNN will have as well C output neurons. The target vector t can have more than a positive class, so it will be a vector of 0s and 1s with C dimensionality. This task is treated as C different binary ( ) and independent classification problems, where each output neuron decides if a sample belongs to a class or not.
Output Activation Functions
These functions are transformations we apply to vectors coming out from CNNs ( s ) before the loss computation.
Sigmoid
It squashes a vector in the range (0, 1). It is applied independently to each element of It’s also called logistic function.
Softmax
Softmax it’s a function, not a loss. It squashes a vector in the range (0, 1) and all the resulting elements add up to 1. It is applied to the output scores . As elements represent a class, they can be interpreted as class probabilities.
The Softmax function cannot be applied independently to each , since it depends on all elements of . For a given class , the Softmax function can be computed as:
Where are the scores inferred by the net for each class in . Note that the Softmax activation for a class depends on all the scores in .
Losses
Cross-Entropy loss
The Cross-Entropy Loss is actually the only loss we are discussing here. The other losses names written in the title are other names or variations of it. The CE Loss is defined as:
Where and are the groundtruth and the CNN score for each class in . As usually an activation function (Sigmoid / Softmax) is applied to the scores before the CE Loss computation, we write to refer to the activations.
In a binary classification problem, where , the Cross Entropy Loss can be defined as
Where it’s assumed that there are two classes: and . [0,1] and are the groundtruth and the score for , and and are the groundtruth and the score for . That is the case when we split a Multi-Label classification problem in binary classification problems. See next Binary Cross-Entropy Loss section for more details.
Logistic Loss and Multinomial Logistic Loss are other names for Cross-Entropy loss. [Discussion]
The layers of Caffe, Pytorch and Tensorflow than use a Cross-Entropy loss without an embedded activation function are:
Caffe: Multinomial Logistic Loss Layer. Is limited to multi-class classification (does not support multiple labels).
Pytorch: BCELoss. Is limited to binary classification (between two classes).
TensorFlow: log_loss.
Categorical Cross-Entropy loss
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 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 . 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.
The gradient expression will be the same for all except for the ground truth class , because the score of is in the nominator. After some calculus, the derivative respect to the positive class is:
Where is the score of any negative class in C different from .
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.
Where each in M is the CNN score for each positive class. As in Facebook paper, I introduce a scaling factor 1 / M to make the loss invariant to the number of positive classes, which may be different per sample.
The gradient has different expressions for positive and negative classes. For positive classes:
Where is the score of any positive class. 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:
Forward pass: Loss computation
We first compute Softmax activations for each class and store them in probs. Then we compute the loss for each image in the batch considering there might be more than one positive label. We use an scale_factor and we also multiply losses by the labels, which can be binary or real numbers, so they can be used for instance to introduce class balancing. The batch loss will be the mean loss of the elements in the batch. We then save the data_loss to display it and the probs to use them in the backward pass.
Backward pass: Gradients computation
In the backward pass we need to compute the gradients of each element of the batch respect to each one of the classes scores s. As the gradient for all the classes C except positive classes M is equal to probs, we assign probs values to delta. For the positive classes in M we subtract 1 to the corresponding probs value and use scale_factor to match the gradient expression. We compute the mean gradients of all the batch to run the backpropagation.
Binary Cross-Entropy Loss
Also called Sigmoid Cross-Entropy loss. It is a Sigmoid activation plus a Cross-Entropy loss. Unlike Softmax loss it is independent for each vector component (class), meaning that the loss computed for every CNN output vector component is not affected by other component values. That’s why it is used for multi-label classification, were the insight of an element belonging to a certain class should not influence the decision for another class. It’s called Binary Cross-Entropy Loss because it sets up a binary classification problem between C′′=2 classes for every class in C, as explained above. So when using this Loss, the formulation of Cross Entroypy Loss for binary problems is often used:
Pytorch: BCEWithLogitsLoss
TensorFlow: sigmoid_cross_entropy.
Last updated