TransWikia.com

Cross-entropy loss explanation

Data Science Asked on January 14, 2021

Suppose I build a neural network for classification. The last layer is a dense layer with Softmax activation. I have five different classes to classify. Suppose for a single training example, the true label is [1 0 0 0 0] while the predictions be [0.1 0.5 0.1 0.1 0.2]. How would I calculate the cross entropy loss for this example?

5 Answers

The cross entropy formula takes in two distributions, $p(x)$, the true distribution, and $q(x)$, the estimated distribution, defined over the discrete variable $x$ and is given by

$$H(p,q) = -sum_{forall x} p(x) log(q(x))$$

For a neural network, the calculation is independent of the following:

  • What kind of layer was used.

  • What kind of activation was used - although many activations will not be compatible with the calculation because their outputs are not interpretable as probabilities (i.e., their outputs are negative, greater than 1, or do not sum to 1). Softmax is often used for multiclass classification because it guarantees a well-behaved probability distribution function.

For a neural network, you will usually see the equation written in a form where $mathbf{y}$ is the ground truth vector and $mathbf{hat{y}}$ (or some other value taken direct from the last layer output) is the estimate. For a single example, it would look like this:

$$L = - mathbf{y} cdot log(mathbf{hat{y}})$$

where $cdot$ is the inner product.

Your example ground truth $mathbf{y}$ gives all probability to the first value, and the other values are zero, so we can ignore them, and just use the matching term from your estimates $mathbf{hat{y}}$

$L = -(1times log(0.1) + 0 times log(0.5) + ...)$

$L = - log(0.1) approx 2.303$

An important point from comments

That means, the loss would be same no matter if the predictions are $[0.1, 0.5, 0.1, 0.1, 0.2]$ or $[0.1, 0.6, 0.1, 0.1, 0.1]$?

Yes, this is a key feature of multiclass logloss, it rewards/penalises probabilities of correct classes only. The value is independent of how the remaining probability is split between incorrect classes.

You will often see this equation averaged over all examples as a cost function. It is not always strictly adhered to in descriptions, but usually a loss function is lower level and describes how a single instance or component determines an error value, whilst a cost function is higher level and describes how a complete system is evaluated for optimisation. A cost function based on multiclass log loss for data set of size $N$ might look like this:

$$J = - frac{1}{N}left(sum_{i=1}^{N} mathbf{y_i} cdot log(mathbf{hat{y}_i})right)$$

Many implementations will require your ground truth values to be one-hot encoded (with a single true class), because that allows for some extra optimisation. However, in principle the cross entropy loss can be calculated - and optimised - when this is not the case.

Correct answer by Neil Slater on January 14, 2021

The answer from Neil is correct. However I think its important to point out that while the loss does not depend on the distribution between the incorrect classes (only the distribution between the correct class and the rest), the gradient of this loss function does effect the incorrect classes differently depending on how wrong they are. So when you use cross-ent in machine learning you will change weights differently for [0.1 0.5 0.1 0.1 0.2] and [0.1 0.6 0.1 0.1 0.1]. This is because the score of the correct class is normalized by the scores of all the other classes to turn it into a probability.

Answered by Lucas Adams on January 14, 2021

Let's see how the gradient of the loss behaves... We have the cross-entropy as a loss function, which is given by

$$ H(p,q) = -sum_{i=1}^n p(x_i) log(q(x_i)) = -(p(x_1)log(q(x_1)) + ldots + p(x_n)log(q(x_n)) $$

Going from here.. we would like to know the derivative with respect to some $x_i$: $$ frac{partial}{partial x_i} H(p,q) = -frac{partial}{partial x_i} p(x_i)log(q(x_i)). $$ Since all the other terms are cancelled due to the differentiation. We can take this equation one step further to $$ frac{partial}{partial x_i} H(p,q) = -p(x_i)frac{1}{q(x_i)}frac{partial q(x_i)}{partial x_i}. $$

From this we can see that we are still only penalizing the true classes (for which there is value for $p(x_i)$). Otherwise we just have a gradient of zero.

I do wonder how to software packages deal with a predicted value of 0, while the true value was larger than zero... Since we are dividing by zero in that case.

Answered by zwep on January 14, 2021

The problem is that the probabilities are coming from a 'complicated' function that incorporates the other outputs into the given value. The outcomes are inter-connected, so this way we are not deriving regarding to the actual outcome, but by all the inputs of the last activation function (softmax), for each and every outcome.

I have found a very nice description here where the author shows that the actual derivative is $p_i - y_i$.

Other neat description can be found here.

I think that using a simple sigmoid as a last activation layer would lead to the approved answer, but using softmax indicates different answer.

Answered by guyko on January 14, 2021

Let's start with understanding entropy in information theory: Suppose you want to communicate a string of alphabets "aaaaaaaa". You could easily do that as 8*"a". Now take another string "jteikfqa". Is there a compressed way of communicating this string? There isn't is there. We can say that the entropy of the 2nd string is more as, to communicate it, we need more "bits" of information.

This analogy applies to probabilities as well. If you have a set of items, fruits for example, the binary encoding of those fruits would be $log_2(n)$ where n is the number of fruits. For 8 fruits you need 3 bits, and so on. Another way of looking at this is that given the probability of someone selecting a fruit at random is 1/8, the uncertainty reduction if a fruit is selected is $-log_{2}(1/8)$ which is 3. More specifically,

$$-sum_{i=1}^{8}frac{1}{8}log_{2}(frac{1}{8}) = 3$$ This entropy tells us about the uncertainty involved with certain probability distributions; the more uncertainty/variation in a probability distribution, the larger is the entropy (e.g. for 1024 fruits, it would be 10).

In "cross"-entropy, as the name suggests, we focus on the number of bits required to explain the difference in two different probability distributions. The best case scenario is that both distributions are identical, in which case the least amount of bits are required i.e. simple entropy. In mathematical terms,

$$H(bf{y},bf{hat{y}}) = -sum_{i}bf{y}_ilog_{e}(bf{hat{y}}_i)$$

Where $bf{hat{y}}$ is the predicted probability vector (Softmax output), and $bf{y}$ is the ground-truth vector( e.g. one-hot). The reason we use natural log is because it is easy to differentiate (ref. calculating gradients) and the reason we do not take log of ground truth vector is because it contains a lot of 0's which simplify the summation.

Bottom line: In layman terms, one could think of cross-entropy as the distance between two probability distributions in terms of the amount of information (bits) needed to explain that distance. It is a neat way of defining a loss which goes down as the probability vectors get closer to one another.

Answered by Hassaan on January 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP