Loss functions for classification

In machine learning and mathematical optimization, loss functions for classification are computationally feasible loss functions representing the price paid for inaccuracy of predictions in classification problems (problems of identifying which category a particular observation belongs to).[1] Given ${\displaystyle X}$ as the vector space of all possible inputs, and Y = {–1,1} as the vector space of all possible outputs, we wish to find a function ${\displaystyle f:X\mapsto \mathbb {R} }$ which best maps ${\displaystyle {\vec {x}}}$ to ${\displaystyle y}$.[2] However, because of incomplete information, noise in the measurement, or probabilistic components in the underlying process, it is possible for the same ${\displaystyle {\vec {x}}}$ to generate different ${\displaystyle y}$.[3] As a result, the goal of the learning problem is to minimize expected risk, defined as

${\displaystyle I[f]=\displaystyle \int _{X\times Y}V(f({\vec {x}}),y)p({\vec {x}},y)\,d{\vec {x}}\,dy}$

where ${\displaystyle V(f({\vec {x}}),y)}$ is the loss function, and ${\displaystyle p({\vec {x}},y)}$ is the probability density function of the process that generated the data, which can equivalently be written as

${\displaystyle p({\vec {x}},y)=p(y\mid {\vec {x}})p({\vec {x}}).}$

For computational ease, it is standard practice to write loss functions as functions of only one variable ${\displaystyle \upsilon =yf({\vec {x}})}$. Within classification, loss functions are generally written solely in terms of the product of the true classifier ${\displaystyle y}$ and the predicted value ${\displaystyle f({\vec {x}})}$. Selection of a loss function within this framework

${\displaystyle V(f({\vec {x}}),y)=\phi (yf({\vec {x}}))=\phi (\upsilon )}$

impacts the optimal ${\displaystyle f_{\phi }^{*}}$ which minimizes the expected risk. Loss functions in this form are known as margin losses.

In the case of binary classification, it is possible to simplify the calculation of expected risk from the integral specified above. Specifically,

{\displaystyle {\begin{aligned}I[f]&=\int _{X\times Y}V(f({\vec {x}}),y)p({\vec {x}},y)\,d{\vec {x}}\,dy\\[6pt]&=\int _{X}\int _{Y}\phi (yf({\vec {x}}))p(y\mid {\vec {x}})p({\vec {x}})\,dy\,d{\vec {x}}\\[6pt]&=\int _{X}[\phi (f({\vec {x}}))p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))p(-1\mid {\vec {x}})]p({\vec {x}})\,d{\vec {x}}\\[6pt]&=\int _{X}[\phi (f({\vec {x}}))p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))(1-p(1\mid {\vec {x}}))]p({\vec {x}})\,d{\vec {x}}\end{aligned}}}

The second equality follows from the properties described above. The third equality follows from the fact that 1 and −1 are the only possible values for ${\displaystyle y}$, and the fourth because ${\displaystyle p(-1\mid x)=1-p(1\mid x)}$. The term within brackets ${\displaystyle [\phi (f({\vec {x}}))p(1\mid {\vec {x}})+\phi (-f({\vec {x}}))(1-p(1\mid {\vec {x}}))]}$ is known as the conditional risk.

One can solve for the minimizer of ${\displaystyle I[f]}$ by taking the functional derivative of the last equality with respect to ${\displaystyle f}$ and setting the derivative equal to 0. This will result in the following equation

${\displaystyle {\frac {\partial \phi (f)}{\partial f}}\eta +{\frac {\partial \phi (-f)}{\partial f}}(1-\eta )=0\;\;\;\;\;(1)}$

which is also equivalent to setting the derivative of the conditional risk equal to zero.

Given the binary nature of classification, a natural selection for a loss function (assuming equal cost for false positives and false negatives) would be the 0-1 loss function (0–1 indicator function), which takes the value of 0 if the predicted classification equals that of the true class or a 1 if the predicted classification does not match the true class. This selection is modeled by

${\displaystyle V(f({\vec {x}}),y)=H(-yf({\vec {x}}))}$

where ${\displaystyle H}$ indicates the Heaviside step function. However, this loss function is non-convex and non-smooth, and solving for the optimal solution is an NP-hard combinatorial optimization problem.[4] As a result, it is better to substitute continuous, convex loss function surrogates which are tractable for commonly used learning algorithms. In addition to their computational tractability, one can show that the solutions to the learning problem using these loss surrogates allow for the recovery of the actual solution to the original classification problem.[5] Some of these surrogates are described below.

In practice, the probability distribution ${\displaystyle p({\vec {x}},y)}$ is unknown. Consequently, utilizing a training set of ${\displaystyle n}$ independently and identically distributed sample points

${\displaystyle S=\{({\vec {x}}_{1},y_{1}),\dots ,({\vec {x}}_{n},y_{n})\}}$

drawn from the data sample space, one seeks to minimize empirical risk

${\displaystyle I_{S}[f]={\frac {1}{n}}\sum _{i=1}^{n}V(f({\vec {x}}_{i}),y_{i})}$

as a proxy for expected risk.[3] (See statistical learning theory for a more detailed description.)

Bayes consistency

Utilizing Bayes' theorem, it can be shown that the optimal ${\displaystyle f_{0/1}^{*}}$, i.e., the one that minimizes the expected risk associated with the zero-one loss, implements the Bayes optimal decision rule for a binary classification problem and is in the form of

${\displaystyle f_{0/1}^{*}({\vec {x}})\;=\;{\begin{cases}\;\;\;1&{\text{if }}p(1\mid {\vec {x}})>p(-1\mid {\vec {x}})\\\;\;\;0&{\text{if }}p(1\mid {\vec {x}})=p(-1\mid {\vec {x}})\\-1&{\text{if }}p(1\mid {\vec {x}}).

A loss function is said to be classification-calibrated or Bayes consistent if its optimal ${\displaystyle f_{\phi }^{*}}$ is such that ${\displaystyle f_{0/1}^{*}({\vec {x}})=\operatorname {sgn} (f_{\phi }^{*}({\vec {x}}))}$and is thus optimal under the Bayes decision rule. A Bayes consistent loss function allows us to find the Bayes optimal decision function ${\displaystyle f_{\phi }^{*}}$ by directly minimizing the expected risk and without having to explicitly model the probability density functions.

For convex margin loss ${\displaystyle \phi (\upsilon )}$, it can be shown that ${\displaystyle \phi (\upsilon )}$ is Bayes consistent if and only if it is differentiable at 0 and ${\displaystyle \phi '(0)=0}$.[6][1] Yet, this result does not exclude the existence of non-convex Bayes consistent loss functions. A more general result states that Bayes consistent loss functions can be generated using the following formulation [7]

${\displaystyle \phi (v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)]\;\;\;\;\;(2)}$,

where ${\displaystyle f(\eta ),(0\leq \eta \leq 1)}$ is any invertible function such that ${\displaystyle f^{-1}(-v)=1-f^{-1}(v)}$ and ${\displaystyle C(\eta )}$is any differentiable strictly concave function such that ${\displaystyle C(\eta )=C(1-\eta )}$. Table-I shows the generated Bayes consistent loss functions for some example choices of ${\displaystyle C(\eta )}$ and ${\displaystyle f^{-1}(v)}$. Note that the Savage and Tangent loss are not convex. Such non-convex loss functions have been shown to be useful in dealing with outliers in classification.[7][8] For all loss functions generated from (2), the posterior probability ${\displaystyle p(y=1|{\vec {x}})}$ can be found using the invertible link function as ${\displaystyle p(y=1|{\vec {x}})=\eta =f^{-1}(v)}$. Such loss functions where the posterior probability can be recovered using the invertible link are called proper loss functions.

Table-I
Loss name ${\displaystyle \phi (v)}$ ${\displaystyle C(\eta )}$ ${\displaystyle f^{-1}(v)}$ ${\displaystyle f(\eta )}$
Exponential ${\displaystyle e^{-v}}$ ${\displaystyle 2{\sqrt {\eta (1-\eta )}}}$ ${\displaystyle {\frac {e^{2v}}{1+e^{2v}}}}$ ${\displaystyle {\frac {1}{2}}\log({\frac {\eta }{1-\eta }})}$
Logistic ${\displaystyle {\frac {1}{\log(2)}}\log(1+e^{-v})}$ ${\displaystyle {\frac {1}{\log(2)}}[-\eta \log(\eta )-(1-\eta )\log(1-\eta )]}$ ${\displaystyle {\frac {e^{v}}{1+e^{v}}}}$ ${\displaystyle \log({\frac {\eta }{1-\eta }})}$
Square ${\displaystyle (1-v)^{2}}$ ${\displaystyle 4\eta (1-\eta )}$ ${\displaystyle {\frac {1}{2}}(v+1)}$ ${\displaystyle 2\eta -1}$
Savage ${\displaystyle {\frac {1}{(1+e^{v})^{2}}}}$ ${\displaystyle \eta (1-\eta )}$ ${\displaystyle {\frac {e^{v}}{1+e^{v}}}}$ ${\displaystyle \log({\frac {\eta }{1-\eta }})}$
Tangent ${\displaystyle (2\arctan(v)-1)^{2}}$ ${\displaystyle 4\eta (1-\eta )}$ ${\displaystyle \arctan(v)+{\frac {1}{2}}}$ ${\displaystyle \tan(\eta -{\frac {1}{2}})}$

The sole minimizer of the expected risk, ${\displaystyle f_{\phi }^{*}}$, associated with the above generated loss functions can be directly found from equation (1) and shown to be equal to the corresponding ${\displaystyle f(\eta )}$. This holds even for the nonconvex loss functions, which means that gradient descent based algorithms such as gradient boosting can be used to construct the minimizer.

Proper loss functions, loss margin and regularization

For proper loss functions, the loss margin can be defined as ${\displaystyle \mu _{\phi }=-{\frac {\phi '(0)}{\phi ''(0)}}}$ and shown to be directly related to the regularization properties of the classifier.[9] Specifically a loss function of larger margin increases regularization and produces better estimates of the posterior probability. For example, the loss margin can be increased for the logistic loss by introducing a ${\displaystyle \gamma }$ parameter and writing the logistic loss as ${\displaystyle {\frac {1}{\gamma }}\log(1+e^{-\gamma v})}$ where smaller ${\displaystyle 0<\gamma <1}$ increases the margin of the loss. It is shown that this is directly equivalent to decreasing the learning rate in gradient boosting ${\displaystyle F_{m}(x)=F_{m-1}(x)+\gamma h_{m}(x),}$ where decreasing ${\displaystyle \gamma }$ improves the regularization of the boosted classifier. The theory makes it clear that when a learning rate of ${\displaystyle \gamma }$ is used, the correct formula for retrieving the posterior probability is now ${\displaystyle \eta =f^{-1}(\gamma F(x))}$.

In conclusion, by choosing a loss function with larger margin (smaller ${\displaystyle \gamma }$) we increase regularization and improve our estimates of the posterior probability which in turn improves the ROC curve of the final classifier.

Square loss

While more commonly used in regression, the square loss function can be re-written as a function ${\displaystyle \phi (yf({\vec {x}}))}$ and utilized for classification. It can be generated using (2) and Table-I as follows

${\displaystyle \phi (v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)]=4({\frac {1}{2}}(v+1))(1-{\frac {1}{2}}(v+1))+(1-{\frac {1}{2}}(v+1))(4-8({\frac {1}{2}}(v+1)))=(1-v)^{2}.}$

The square loss function is both convex and smooth. However, the square loss function tends to penalize outliers excessively, leading to slower convergence rates (with regards to sample complexity) than for the logistic loss or hinge loss functions.[1] In addition, functions which yield high values of ${\displaystyle f({\vec {x}})}$ for some ${\displaystyle x\in X}$ will perform poorly with the square loss function, since high values of ${\displaystyle yf({\vec {x}})}$ will be penalized severely, regardless of whether the signs of ${\displaystyle y}$ and ${\displaystyle f({\vec {x}})}$ match.

A benefit of the square loss function is that its structure lends itself to easy cross validation of regularization parameters. Specifically for Tikhonov regularization, one can solve for the regularization parameter using leave-one-out cross-validation in the same time as it would take to solve a single problem.[10]

The minimizer of ${\displaystyle I[f]}$ for the square loss function can be directly found from equation (1) as

${\displaystyle f_{\text{Square}}^{*}=2\eta -1=2p(1\mid x)-1.}$

Logistic loss

The logistic loss function can be generated using (2) and Table-I as follows

{\displaystyle {\begin{aligned}\phi (v)&=C[f^{-1}(v)]+\left(1-f^{-1}(v)\right)\,C'\left[f^{-1}(v)\right]\\&={\frac {1}{\log(2)}}\left[{\frac {-e^{v}}{1+e^{v}}}\log {\frac {e^{v}}{1+e^{v}}}-\left(1-{\frac {e^{v}}{1+e^{v}}}\right)\log \left(1-{\frac {e^{v}}{1+e^{v}}}\right)\right]+\left(1-{\frac {e^{v}}{1+e^{v}}}\right)\left[{\frac {-1}{\log(2)}}\log \left({\frac {\frac {e^{v}}{1+e^{v}}}{1-{\frac {e^{v}}{1+e^{v}}}}}\right)\right]\\&={\frac {1}{\log(2)}}\log(1+e^{-v}).\end{aligned}}}

The logistic loss is convex and grows linearly for negative values which make it less sensitive to outliers. The logistic loss is used in the LogitBoost algorithm.

The minimizer of ${\displaystyle I[f]}$ for the logistic loss function can be directly found from equation (1) as

${\displaystyle f_{\text{Logistic}}^{*}=\log \left({\frac {\eta }{1-\eta }}\right)=\log \left({\frac {p(1\mid x)}{1-p(1\mid x)}}\right).}$

This function is undefined when ${\displaystyle p(1\mid x)=1}$ or ${\displaystyle p(1\mid x)=0}$ (tending toward ∞ and −∞ respectively), but predicts a smooth curve which grows when ${\displaystyle p(1\mid x)}$ increases and equals 0 when ${\displaystyle p(1\mid x)=0.5}$.[3]

It's easy to check that the logistic loss and binary cross entropy loss (Log loss) are in fact the same (up to a multiplicative constant ${\displaystyle {\frac {1}{\log(2)}}}$).The cross entropy loss is closely related to the Kullback–Leibler divergence between the empirical distribution and the predicted distribution. The cross entropy loss is ubiquitous in modern deep neural networks.

Exponential loss

The exponential loss function can be generated using (2) and Table-I as follows

${\displaystyle \phi (v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)]=2{\sqrt {({\frac {e^{2v}}{1+e^{2v}}})(1-{\frac {e^{2v}}{1+e^{2v}}})}}+(1-{\frac {e^{2v}}{1+e^{2v}}})({\frac {1-{\frac {2e^{2v}}{1+e^{2v}}}}{\sqrt {{\frac {e^{2v}}{1+e^{2v}}}(1-{\frac {e^{2v}}{1+e^{2v}}})}}})=e^{-v}}$

The exponential loss is convex and grows exponentially for negative values which makes it more sensitive to outliers. The exponential loss is used in the AdaBoost algorithm.

The minimizer of ${\displaystyle I[f]}$ for the exponential loss function can be directly found from equation (1) as

${\displaystyle f_{\text{Exp}}^{*}={\frac {1}{2}}\log \left({\frac {\eta }{1-\eta }}\right)={\frac {1}{2}}\log \left({\frac {p(1\mid x)}{1-p(1\mid x)}}\right).}$

Savage loss

The Savage loss[7] can be generated using (2) and Table-I as follows

${\displaystyle \phi (v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)]=({\frac {e^{v}}{1+e^{v}}})(1-{\frac {e^{v}}{1+e^{v}}})+(1-{\frac {e^{v}}{1+e^{v}}})(1-{\frac {2e^{v}}{1+e^{v}}})={\frac {1}{(1+e^{v})^{2}}}.}$

The Savage loss is quasi-convex and is bounded for large negative values which makes it less sensitive to outliers. The Savage loss has been used in gradient boosting and the SavageBoost algorithm.

The minimizer of ${\displaystyle I[f]}$ for the Savage loss function can be directly found from equation (1) as

${\displaystyle f_{\text{Savage}}^{*}=\log \left({\frac {\eta }{1-\eta }}\right)=\log \left({\frac {p(1\mid x)}{1-p(1\mid x)}}\right).}$

Tangent loss

The Tangent loss[11] can be generated using (2) and Table-I as follows

{\displaystyle {\begin{aligned}\phi (v)&=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)]=4(\arctan(v)+{\frac {1}{2}})(1-(\arctan(v)+{\frac {1}{2}}))+(1-(\arctan(v)+{\frac {1}{2}}))(4-8(\arctan(v)+{\frac {1}{2}}))\\&=(2\arctan(v)-1)^{2}.\end{aligned}}}

The Tangent loss is quasi-convex and is bounded for large negative values which makes it less sensitive to outliers. Interestingly, the Tangent loss also assigns a bounded penalty to data points that have been classified "too correctly". This can help prevent over-training on the data set. The Tangent loss has been used in gradient boosting, the TangentBoost algorithm and Alternating Decision Forests.[12]

The minimizer of ${\displaystyle I[f]}$ for the Tangent loss function can be directly found from equation (1) as

${\displaystyle f_{\text{Tangent}}^{*}=\tan(\eta -{\frac {1}{2}})=\tan(p(1\mid x)-{\frac {1}{2}}).}$

Hinge loss

The hinge loss function is defined as

${\displaystyle V(f({\vec {x}}),y)=\max(0,1-yf({\vec {x}}))=|1-yf({\vec {x}})|_{+}.}$

The hinge loss provides a relatively tight, convex upper bound on the 0–1 indicator function. Specifically, the hinge loss equals the 0–1 indicator function when ${\displaystyle \operatorname {sgn} (f({\vec {x}}))=y}$ and ${\displaystyle |yf({\vec {x}})|\geq 1}$. In addition, the empirical risk minimization of this loss is equivalent to the classical formulation for support vector machines (SVMs). Correctly classified points lying outside the margin boundaries of the support vectors are not penalized, whereas points within the margin boundaries or on the wrong side of the hyperplane are penalized in a linear fashion compared to their distance from the correct boundary.[4]

While the hinge loss function is both convex and continuous, it is not smooth (is not differentiable) at ${\displaystyle yf({\vec {x}})=1}$. Consequently, the hinge loss function cannot be used with gradient descent methods or stochastic gradient descent methods which rely on differentiability over the entire domain. However, the hinge loss does have a subgradient at ${\displaystyle yf({\vec {x}})=1}$, which allows for the utilization of subgradient descent methods.[4] SVMs utilizing the hinge loss function can also be solved using quadratic programming.

The minimizer of ${\displaystyle I[f]}$ for the hinge loss function is

${\displaystyle f_{\text{Hinge}}^{*}({\vec {x}})\;=\;{\begin{cases}1&{\text{if }}p(1\mid {\vec {x}})>p(-1\mid {\vec {x}})\\-1&{\text{if }}p(1\mid {\vec {x}})

when ${\displaystyle p(1\mid x)\neq 0.5}$, which matches that of the 0–1 indicator function. This conclusion makes the hinge loss quite attractive, as bounds can be placed on the difference between expected risk and the sign of hinge loss function.[1] The Hinge loss cannot be derived from (2) since ${\displaystyle f_{\text{Hinge}}^{*}}$ is not invertible.

Generalized smooth hinge loss

The generalized smooth hinge loss function with parameter ${\displaystyle \alpha }$ is defined as

${\displaystyle f_{\alpha }^{*}(z)\;=\;{\begin{cases}{\frac {\alpha }{\alpha +1}}-z&{\text{if }}z\leq 0\\{\frac {1}{\alpha +1}}z^{\alpha +1}-z+{\frac {\alpha }{\alpha +1}}&{\text{if }}0

where

${\displaystyle z=yf({\vec {x}}).}$

It is monotonically increasing and reaches 0 when ${\displaystyle z=1}$.

References

1. Rosasco, L.; De Vito, E. D.; Caponnetto, A.; Piana, M.; Verri, A. (2004). "Are Loss Functions All the Same?" (PDF). Neural Computation. 16 (5): 1063–1076. CiteSeerX 10.1.1.109.6786. doi:10.1162/089976604773135104. PMID 15070510.
2. Shen, Yi (2005), Loss Functions For Binary Classification and Class Probability Estimation (PDF), University of Pennsylvania, retrieved 6 December 2014
3. Rosasco, Lorenzo; Poggio, Tomaso (2014), A Regularization Tour of Machine Learning, MIT-9.520 Lectures Notes, Manuscript
4. Piyush, Rai (13 September 2011), Support Vector Machines (Contd.), Classification Loss Functions and Regularizers (PDF), Utah CS5350/6350: Machine Learning, retrieved 6 December 2014
5. Ramanan, Deva (27 February 2008), Lecture 14 (PDF), UCI ICS273A: Machine Learning, retrieved 6 December 2014
6. Bartlett, Peter L.; Jordan, Michael I.; Mcauliffe, Jon D. (2006). "Convexity, Classification, and Risk Bounds". Journal of the American Statistical Association. 101 (473): 138–156. doi:10.1198/016214505000000907. ISSN 0162-1459. JSTOR 30047445.
7. Masnadi-Shirazi, Hamed; Vasconcelos, Nuno (2008). "On the Design of Loss Functions for Classification: Theory, Robustness to Outliers, and SavageBoost". Proceedings of the 21st International Conference on Neural Information Processing Systems. NIPS'08. USA: Curran Associates Inc.: 1049–1056. ISBN 9781605609492.
8. Leistner, C.; Saffari, A.; Roth, P. M.; Bischof, H. (September 2009). "On robustness of on-line boosting - a competitive study". 2009 IEEE 12th International Conference on Computer Vision Workshops, ICCV Workshops: 1362–1369. doi:10.1109/ICCVW.2009.5457451. ISBN 978-1-4244-4442-7.
9. Vasconcelos, Nuno; Masnadi-Shirazi, Hamed (2015). "A View of Margin Losses as Regularizers of Probability Estimates". Journal of Machine Learning Research. 16 (85): 2751–2795. ISSN 1533-7928.
10. Rifkin, Ryan M.; Lippert, Ross A. (1 May 2007), Notes on Regularized Least Squares (PDF), MIT Computer Science and Artificial Intelligence Laboratory
11. Masnadi-Shirazi, H.; Mahadevan, V.; Vasconcelos, N. (June 2010). "On the design of robust classifiers for computer vision". 2010 IEEE Computer Society Conference on Computer Vision and Pattern Recognition: 779–786. doi:10.1109/CVPR.2010.5540136. ISBN 978-1-4244-6984-0.
12. Schulter, S.; Wohlhart, P.; Leistner, C.; Saffari, A.; Roth, P. M.; Bischof, H. (June 2013). "Alternating Decision Forests". 2013 IEEE Conference on Computer Vision and Pattern Recognition: 508–515. doi:10.1109/CVPR.2013.72. ISBN 978-0-7695-4989-7.