# Multi-Class and Cross Entropy Loss

Two basic functions:

![log(x)                                                              exp(x)    ](https://443921002-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGHUhl6VYqrZm4Re77O%2F-LGHaPkWO__LXugPbkd_%2F-LGHahVbkBXorvGY7X6O%2FScreen%20Shot%202018-06-30%20at%202.30.04%20PM.png?alt=media\&token=83bb63a3-fe57-43c6-bcaf-09d2c51875e0)

1. **Logits** -> Exponential Function -> Normalization -> **Probability**
2. **- P(true) \* Log ( Probability )** measures how small the target probability, and the smaller the loss larger.
3. **Cross Entropy loss:** if the right class is predicted as 1, then the loss would be 0; if the right class if predicted as 0 ( totally wrong ), then **the loss would be infinity**.
4. **At the first iteration**, each class probability would be like 1/C, and the expected initial loss would be -log(1 / C), and it equals to - ( log (1) - log (C)), equals to log (C) - 0, equals to log (C). This is **a good checking criterion**.
5. **SVM Loss:** The loss Li for any individual example, we will be going to perform a sum of all categories Y, except for the true category yi. So we will sum over all incorrect classes and compare the difference between the correct class and incorrect class. And if the correct class score is larger than the incorrect class score by a margin, the loss on this incorrect class would be zero; otherwise, the loss on this incorrect class would be [Yincorrect + 1 - Ycorrect](https://sisyphus.gitbook.io/project/~/edit/drafts/-LP7suJA2Th6Y7L3D_uU/deep-learning-basics/basics/hinge-loss). In one formulation, Li = max(Yj + 1 - Yi), for j in all classes && j != i.
6. **Difference between multi-class SVM loss:** In multi-class SVM loss, it mainly measures how wrong the non-target classes ( wants the target class score to be larger than others by a margin, and if the target loss is already larger than by the margin, jiggling it won’t influence the final loss); While cross-entropy loss always forces the target score to be as near 1 as possible.&#x20;
7. **Will the Cross Entropy Loss ever be the max (infinity)?** No. Because that means **the normalized logits of the correct class** should be **EXACTLY zero**, and which means **the exponential term, before normalization**, should be **zero** too, and again which means **the raw logits** before exponential function should be **negative infinity** and computer doesn’t do that well to generate infinity output. So the Infinity loss is not expected.

<div align="center"><img src="https://443921002-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGHUhl6VYqrZm4Re77O%2F-LGHYwvncirBGkeyLS8z%2F-LGH_1zvfJXzh6IBv8dZ%2FScreen%20Shot%202018-06-26%20at%204.57.26%20PM.png?alt=media&#x26;token=568a9ec5-1a55-4e63-8a9e-931e9ea21b1b" alt="What is Cross Entropy Loss"></div>

![Differences](https://443921002-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGHUhl6VYqrZm4Re77O%2F-LGHYwvncirBGkeyLS8z%2F-LGHa7nGk0U1Ch6QxT1i%2FScreen%20Shot%202018-06-29%20at%204.04.45%20PM.png?alt=media\&token=8e7530ab-4947-4562-abe3-f76f5bde71f3)

For example, the 3-element vector \[1.0, 2.0, 3.0] gets transformed into \[0.09, 0.24, 0.67]. The order of elements by relative size is preserved, and they add up to 1.0. Let's tweak this vector slightly into: \[1.0, 2.0, 5.0]. We get the output \[0.02, 0.05, 0.93], which still preserves these properties. Note that as **the last element** is farther away from the first two, it'**s softmax value is dominating the overall slice of size 1.0 in the output.** Intuitively, the softmax function is **a "soft" version of the maximum function.** Instead of just selecting one maximal element, softmax breaks the vector up into parts of a whole (1.0) with the maximal input element getting a proportionally larger chunk, but the other elements getting some of it as well

{% embed url="https://eli.thegreenplace.net/2016/the-softmax-function-and-its-derivative" %}

{% embed url="http://dataaspirant.com/2017/03/07/difference-between-softmax-function-and-sigmoid-function/" %}



