Most Common Loss Functions in Machine Learning
Tags: machinelearning, datascience, python, computerscience
Published: 2020-07-09 06:11:13


#### Every Machine Learning Engineer should know about these common Loss functions in Machine Learning and when to use them.
> # In [mathematical optimization](https://en.wikipedia.org/wiki/Mathematical_optimization) and [decision theory](https://en.wikipedia.org/wiki/Decision_theory), a loss function or cost function is a function that maps an [event](https://en.wikipedia.org/wiki/Event_(probability_theory)) or values of one or more variables onto a [real number](https://en.wikipedia.org/wiki/Real_number) intuitively representing some “cost” associated with the event.
> #  — [Wikipedia](https://en.wikipedia.org/wiki/Loss_function)

![Photo by [Josh Rose](https://unsplash.com/@joshsrose?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/7132/1*l3kNYfW54bLC2b1VW4whmA.jpeg)

As a core element, the Loss function is a method of evaluating your Machine Learning algorithm that how well it models your featured dataset. It is defined as **a measurement of how good your model is in terms of predicting the expected outcome.**

The *Cost function* and *Loss function* refer to the same context. The cost function is a function that is calculated as the average of all loss function values. Whereas, the loss function is calculated for each sample output compared to its actual value.

*__The Loss function is directly related to the predictions of your model that you have built. So if your loss function value is less, your model will be providing good results. Loss function or I should rather say, the Cost function that is used to evaluate the model performance, needs to be minimized in order to improve its performance.__*

Lets now dive into the Loss functions.

Widely speaking, the Loss functions can be grouped into two major categories concerning the types of problems that we come across in the real world — [**Classification**](https://en.wikipedia.org/wiki/Loss_functions_for_classification) and **Regression**. In Classification, the task is to predict the respective probabilities of all classes that the problem is dealing with. In Regression, oppositely, the task is to predict the continuous value concerning a given set of independent features to the learning algorithm.

    **Assumptions:**
        n/m — Number of training samples.
        i — ith training sample in a dataset.
        y(i) — Actual value for the ith training sample.
        y_hat(i) — Predicted value for the ith training sample.

## Classification Losses

### 1. Binary Cross-Entropy Loss / Log Loss

This is the most common Loss function used in Classification problems. The cross-entropy loss decreases as the predicted probability converges to the actual label. It measures the performance of a classification model whose predicted output is a probability value between 0 and 1.

**When the number of classes is 2, _Binary Classification_**

![](https://cdn-images-1.medium.com/max/2278/1*-nJTj5mXtAAz9F107398sQ@2x.png)

**When the number of classes is more than 2, _Multi-class Classification_**

![](https://cdn-images-1.medium.com/max/2000/1*wcTVsYh3d4IQnYcUGdu66Q@2x.png)

![](https://cdn-images-1.medium.com/max/2000/1*3l1R-sWJVpDb1URHy5XFVA.png)

The Cross-Entropy Loss formula is derived from the regular likelihood function, but with logarithms added in.

### 2. Hinge Loss

The second most common loss function used for Classification problems and an alternative to Cross-Entropy loss function is Hinge Loss, primarily developed for Support Vector Machine (SVM) model evaluation.

![](https://cdn-images-1.medium.com/max/2000/1*z9PRqqd_feIwV2EeJliIEA.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*axK_lLrWa20u8_tM0V7uBQ.png)
> *Hinge Loss not only penalizes the wrong predictions but also the right predictions that are not confident. It is primarily used with SVM Classifiers with class labels as -1 and 1. Make sure you change your malignant class labels from 0 to -1.*

![Photo by [Jen Theodore](https://unsplash.com/@jentheodore?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/10368/0*4ZxpnNHNK9ehInmM)

## Regression Losses

### 1. Mean Square Error / Quadratic Loss / L2 Loss

MSE loss function is defined as the average of squared differences between the actual and the predicted value. It is the most commonly used Regression loss function.

![](https://cdn-images-1.medium.com/max/2000/1*SRKwqe7YM2-cV3PMMN7VTg.png)

![](https://cdn-images-1.medium.com/max/2000/1*H1mkqlq7buDZ7rDWD_4qIw.png)

The corresponding cost function is the **Mean** of these **Squared Errors (MSE)**. The MSE Loss function penalizes the model for making large errors by squaring them and this property makes the MSE cost function less robust to outliers. Therefore, *it should not be used if the data is prone to many outliers.*

### 2. Mean Absolute Error / L1 Loss

MSE loss function is defined as the average of absolute differences between the actual and the predicted value. It is the second most commonly used Regression loss function. It measures the average magnitude of errors in a set of predictions, without considering their directions.

![](https://cdn-images-1.medium.com/max/2000/1*5A-4HIx11hquyDaOImEFkA.png)

![](https://cdn-images-1.medium.com/max/2000/1*ID63kMgh8F0fcgmsrmjS5A.png)

The corresponding cost function is the **Mean** of these **Absolute Errors (MAE)**. The MAE Loss function is more robust to outliers compared to MSE Loss function. Therefore, *it should be used if the data is prone to many outliers.*

### 3. Huber Loss / Smooth Mean Absolute Error

Huber loss function is defined as the combination of MSE and MAE Loss function as it approaches **MSE when 𝛿 ~ 0 and MAE when 𝛿 ~ ∞ (large numbers)**. It’s Mean Absolute Error, that becomes quadratic when the error is small. And to make the error quadratic depends on how small that error could be which is controlled by a hyperparameter, 𝛿 (delta), which can be tuned.

![](https://cdn-images-1.medium.com/max/2000/1*Vx7otH8Vzkkw_Gxzt9xpgg.png)

![](https://cdn-images-1.medium.com/max/2000/1*kBXeqZvMpfVMsYry0Chs5g.png)

The choice of the delta value is critical because it determines what you’re willing to consider as an outlier. Hence, the Huber Loss function could be less sensitive to outliers compared to MSE Loss function depending upon the hyperparameter value. Therefore, **_it can be used if the data is prone to outliers and_ we might need to train hyperparameter delta which is an iterative process.**

### 4. Log-Cosh Loss

The Log-Cosh loss function is defined as the logarithm of the hyperbolic cosine of the prediction error. It is another function used in regression tasks which is much smoother than MSE Loss. It has all the advantages of Huber loss, and it’s twice differentiable everywhere, unlike Huber loss as some Learning algorithms like XGBoost use Newton’s method to find the optimum, and hence the second derivative (*Hessian*) is needed.

![](https://cdn-images-1.medium.com/max/2000/1*GrNtSStzBEwM343vuZoIIQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*VQf8ToK0Td-XQjfuWrD-ew.png)
> *log(cosh(x)) is approximately equal to (x ** 2) / 2 for small x and to abs(x) - log(2) for large x. This means that ‘logcosh’ works mostly like the mean squared error, but will not be so strongly affected by the occasional wildly incorrect prediction.
 — [Tensorflow Docs](https://www.tensorflow.org/api_docs/python/tf/keras/losses/logcosh)

### **5. Quantile Loss**

A quantile is a value below which a fraction of samples in a group falls. Machine learning models work by minimizing (or maximizing) an objective function. As the name suggests, the quantile regression loss function is applied to predict quantiles. For a set of predictions, the loss will be its average.

![](https://cdn-images-1.medium.com/max/2000/1*S4LuBEEuOrPHv55YBisPNQ.png)

![](https://cdn-images-1.medium.com/max/2000/1*G9ei3IOtTOz2N8QCTj-37Q.png)

[Quantile loss function](https://towardsdatascience.com/deep-quantile-regression-c85481548b5a) turns out to be useful when we are interested in predicting an interval instead of only point predictions.

Thank you for reading! I hope this post has been useful. I appreciate feedback and constructive criticism. If you want to talk about this article or other related topics, you can drop me a text here or on my [LinkedIn](https://www.linkedin.com/in/imsparsh/) account.

![Photo by [Crawford Jolly](https://unsplash.com/@crawford?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)](https://cdn-images-1.medium.com/max/6030/1*NgtElaMk5jG3trBElXPG8A.jpeg)