Loss functions for classification: Difference between revisions

Content deleted Content added
Fix cite date error
 
(46 intermediate revisions by 29 users not shown)
Line 1:
{{Short description|Concept in machine learning}}
{{Machine learning}}
[[File:BayesConsistentLosses2.jpg|thumb|Bayes consistent loss functions: Zero-one loss (gray), Savage loss (green), Logistic loss (orange), Exponential loss (purple), Tangent loss (brown), Square loss (blue)]]
{{Attention|reason=Discuss the difference compared to scoring rules|date=January 2024}}
 
In [[machine learning]] and [[mathematical optimization]], '''loss functions for classification''' are computationally feasible [[loss functions]] representing the price paid for inaccuracy of predictions in [[statistical classification|classification problem]]s (problems of identifying which category a particular observation belongs to).<ref name="mit">{{Cite journal | last1 = Rosasco | first1 = L. | last2 = De Vito | first2 = E. D. | last3 = Caponnetto | first3 = A. | last4 = Piana | first4 = M. | last5 = Verri | first5 = A. | url = http://web.mit.edu/lrosasco/www/publications/loss.pdf| title = Are Loss Functions All the Same? | doi = 10.1162/089976604773135104 | journal = Neural Computation | volume = 16 | issue = 5 | pages = 1063–1076 | year = 2004 | pmid = 15070510| pmc = | citeseerx = 10.1.1.109.6786 | s2cid = 11845688 }}</ref> Given <math>\mathcal{X}</math> as the vector space of all possible inputs (usually <math>\mathcal{X} \subset \mathbb{R}^d</math>), and ''<math>\mathcal{Y''&nbsp;} =&nbsp; \{–1 -1,1 \}</math> as the vector spaceset of alllabels (possible outputs), wea wishtypical goal of classification algorithms is to find a function <math>f: \mathcal{X} \mapstoto \mathbbmathcal{RY}</math> which best mapspredicts a label <math>\vec{x}y</math> tofor a given input <math>y\vec{x}</math>.<ref name="penn">{{Citation | last= Shen | first= Yi | title= Loss Functions For Binary Classification and Class Probability Estimation | publisher= University of Pennsylvania | year= 2005 | url= http://stat.wharton.upenn.edu/~buja/PAPERS/yi-shen-dissertation.pdf | accessdateaccess-date= 6 December 2014}}</ref> However, because of incomplete information, noise in the measurement, or probabilistic components in the underlying process, it is possible for the same <math>\vec{x}</math> to generate different <math>y</math>.<ref name="mitlec">{{Citation | lastlast1= Rosasco | firstfirst1= Lorenzo | last2= Poggio | first2= Tomaso | title= A Regularization Tour of Machine Learning | series= MIT-9.520 Lectures Notes | volume= Manuscript | year= 2014}}</ref> As a result, the goal of the learning problem is to minimize expected loss (also known as the risk), defined as
:<math>I[f] = \displaystyle \int_{\mathcal{X} \times \mathcal{Y}} V(f(\vec{x}),y) \, p(\vec{x},y) \, d\vec{x} \, dy</math>
where <math>V(f(\vec{x}),y)</math> is thea given loss function, and <math>p(\vec{x},y)</math> is the [[probability density function]] of the process that generated the data, which can equivalently be written as
 
:<math>p(\vec{x},y)=p(y\mid\vec{x}) p(\vec{x}).</math>
 
ForWithin computational easeclassification, itseveral is standard practice tocommonly writeused [[loss functions]] are written solely in terms of the product of the true label <math>y</math> and the predicted label <math>f(\vec{x})</math>. Therefore, they can be defined as functions of only one variable <math>\upsilon=y f(\vec{x})</math>., so Withinthat classification<math>V(f(\vec{x}),y) loss= functions\phi(yf(\vec{x})) are= generally\phi(\upsilon)</math> writtenwith solelya insuitably termschosen of the product of the true classifierfunction <math>y\phi:\mathbb{R}\to\mathbb{R}</math>. andThese theare predictedcalled value'''margin-based loss functions'''. Choosing a margin-based loss function amounts to choosing <math>f(\vec{x})phi</math>. Selection of a loss function within this framework impacts the optimal <math>f^{*}_\phi</math> which minimizes the expected risk, see [[empirical risk minimization]].
 
:<math>V(f(\vec{x}),y)=\phi(yf(\vec{x}))=\phi(\upsilon)</math>
 
impacts the optimal <math>f^{*}_\phi</math> 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,
Line 17 ⟶ 15:
:<math>
\begin{align}
I[f] & = \int_{\mathcal{X} \times \mathcal{Y}} V(f(\vec{x}),y) \, p(\vec{x},y) \,d\vec{x} \,dy \\[6pt]
& = \int_Xint_\mathcal{X} \int_Yint_\mathcal{Y} \phi(yf(\vec{x})) \, p(y\mid\vec{x}) \, p(\vec{x}) \,dy \,d\vec{x} \\[6pt]
& = \int_Xint_\mathcal{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_Xint_\mathcal{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{align}
</math>
Line 30 ⟶ 28:
One can solve for the minimizer of <math>I[f]</math> by taking the functional derivative of the last equality with respect to <math>f</math> and setting the derivative equal to 0. This will result in the following equation
 
:<math>\frac{\partial \phi(f)}{\partial f}\eta + \frac{\partial \phi(-f)}{\partial f}(1-\eta)=0, \;\;\;\;\;(1)</math>{{Citation needed|date=February 2023}}{{Clarify|reason=What is η?|date=February 2023}}
:<math>
\frac{\partial \phi(f)}{\partial f}\eta + \frac{\partial \phi(-f)}{\partial f}(1-\eta)=0 \;\;\;\;\;(1)
</math>
 
:where <math>
which is also equivalent to setting the derivative of the conditional risk equal to zero.
\eta=p(y=1|\vec{x})
</math>, 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
Line 40 ⟶ 38:
 
where <math>H</math> 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.<ref name="Utah">{{Citation | last= Piyush | first= Rai | title= Support Vector Machines (Contd.), Classification Loss Functions and Regularizers | publisher= Utah CS5350/6350: Machine Learning | date= 13 September 2011 | url= httphttps://wwwcis.cs.utahtemple.edu/~piyushlatecki/teachingCourses/13AI-9-printFall12/Lectures/SVM.pdf | accessdateaccess-date= 64 DecemberMay 20142021}}</ref> As a result, it is better to substitute continuous, convex '''loss function surrogates''' which are tractable for commonly used learning algorithms, as they have convenient properties such as being convex and smooth. 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.<ref name="uci">{{Citation | last= Ramanan | first= Deva | title= Lecture 14 | publisher= UCI ICS273A: Machine Learning | date= 27 February 2008 | url= http://www.ics.uci.edu/~dramanan/teaching/ics273a_winter08/lectures/lecture14.pdf | accessdateaccess-date= 6 December 2014}}</ref> Some of these surrogates are described below.
 
In practice, the probability distribution <math>p(\vec{x},y)</math> is unknown. Consequently, utilizing a training set of <math>n</math> [[iid|independently and identically distributed]] sample points
Line 59 ⟶ 57:
A loss function is said to be ''classification-calibrated or Bayes consistent'' if its optimal <math>f^*_{\phi}</math> is such that <math>f^*_{0/1}(\vec{x}) = \operatorname{sgn}(f^*_{\phi}(\vec{x}))</math>and is thus optimal under the Bayes decision rule. A Bayes consistent loss function allows us to find the Bayes optimal decision function <math>f^*_{\phi}</math> by directly minimizing the expected risk and without having to explicitly model the probability density functions.
 
For convex margin loss <math>\phi(\upsilon)</math>, it can be shown that <math>\phi(\upsilon)</math> is Bayes consistent if and only if it is differentiable at 0 and <math>\phi'(0)=<0</math>.<ref>{{Cite journal|lastlast1=Bartlett|firstfirst1=Peter L.|last2=Jordan|first2=Michael I.|last3=Mcauliffe|first3=Jon D.|date=2006|title=Convexity, Classification, and Risk Bounds|journal=Journal of the American Statistical Association|volume=101|issue=473|pages=138–156|issn=0162-1459|jstor=30047445|doi=10.1198/016214505000000907|s2cid=2833811}}</ref><ref name="mit" /> 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 <ref name=":0">{{Cite journal|lastlast1=Masnadi-Shirazi|firstfirst1=Hamed|last2=Vasconcelos|first2=Nuno|date=2008|title=On the Design of Loss Functions for Classification: Theory, Robustness to Outliers, and SavageBoost|url=httphttps://dlpapers.acmnips.orgcc/citation.cfm?id=2981780paper/3591-on-the-design-of-loss-functions-for-classification-theory-robustness-to-outliers-and-savageboost.2981911pdf|journal=Proceedings of the 21st International Conference on Neural Information Processing Systems|series=NIPS'08|___location=USA|publisher=Curran Associates Inc.|pages=1049–1056|isbn=9781605609492}}</ref>
 
:<math>\phi(v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)] \;\;\;\;\;(2)</math>,
 
where <math>f(\eta), (0\leq \eta \leq 1)</math> is any invertible function such that <math>f^{-1}(-v)=1-f^{-1}(v)</math> and <math>C(\eta)</math> is any differentiable strictly concave function such that <math>C(\eta)=C(1-\eta)</math>. Table-I shows the generated Bayes consistent loss functions for some example choices of <math>C(\eta)</math> and <math>f^{-1}(v)</math>. 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.<ref name=":0" /><ref>{{Cite journalbook|lastlast1=Leistner|firstfirst1=C.|last2=Saffari|first2=A.|last3=Roth|first3=P. M.|last4=Bischof|first4=H.|datetitle=September2009 IEEE 12th International Conference on Computer Vision Workshops, ICCV Workshops 2009|titlechapter=On robustness of on-line boosting - a competitive study |url=https://ieeexplore.ieee.org/document/5457451|journaldate=September 2009 IEEE 12th International Conference on Computer Vision Workshops, ICCV Workshops|pages=1362–1369|doi=10.1109/ICCVW.2009.5457451|isbn=978-1-4244-4442-7|s2cid=6032045}}</ref> For all loss functions generated from (2), the posterior probability <math>p(y=1|\vec{x})</math> can be found using the invertible ''link function'' as <math>p(y=1|\vec{x})=\eta=f^{-1}(v)</math>. Such loss functions where the posterior probability can be recovered using the invertible link are called ''proper loss functions''.
{| class="wikitable"
|+Table-I
Line 106 ⟶ 104:
 
==Proper loss functions, loss margin and regularization==
[[File:LogitLossMarginWithMu.jpg|alt=|thumb|(Red) standard Logistic loss (<math>\gamma=1, \mu=2</math>) and (Blue) increased margin Logistic loss (<math>\gamma=0.2</math>).]]
For proper loss functions, the ''loss margin'' can be defined as <math>\mu_{\phi}=-\frac{\phi'(0)}{\phi''(0)}</math> and shown to be directly related to the regularization properties of the classifier.<ref>{{Cite journal|lastlast1=Vasconcelos|firstfirst1=Nuno|last2=Masnadi-Shirazi|first2=Hamed|date=2015|title=A View of Margin Losses as Regularizers of Probability Estimates|url=http://jmlr.org/papers/v16/masnadi15a.html|journal=Journal of Machine Learning Research|volume=16|issue=85|pages=2751–2795|issn=1533-7928}}</ref> 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 <math>\gamma</math> parameter and writing the logistic loss as <math>\frac{1}{\gamma}\log(1+e^{-\gamma v})</math> where smaller <math>0<\gamma<1</math> increases the margin of the loss. It is shown that this is directly equivalent to decreasing the learning rate in [[gradient boosting]] <math>F_m(x) = F_{m-1}(x) + \gamma h_m(x),</math> where decreasing <math>\gamma</math> improves the regularization of the boosted classifier. It should be noted that theThe theory makes it clear that when a learning rate of <math>\gamma</math> is used, the correct formula for retrieving the posterior probability is now <math>\eta=f^{-1}(\gamma F(x))</math>.
 
In conclusion, by choosing a loss function with larger margin (smaller <math>\gamma</math>) we increase regularization and improve our estimates of the posterior probability which in turn improves the ROC curve of the final classifier.
Line 117 ⟶ 115:
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.<ref name="mit" /> In addition, functions which yield high values of <math>f(\vec{x})</math> for some <math>x \in X</math> will perform poorly with the square loss function, since high values of <math>yf(\vec{x})</math> will be penalized severely, regardless of whether the signs of <math>y</math> and <math>f(\vec{x})</math> 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 (statistics)|cross-validation]] in the same time as it would take to solve a single problem.<ref>{{Citation| lastlast1= Rifkin| firstfirst1= Ryan M.| last2= Lippert| first2= Ross A.| title= Notes on Regularized Least Squares| publisher= MIT Computer Science and Artificial Intelligence Laboratory| date= 1 May 2007|url=https://dspace.mit.edu/bitstream/handle/1721.1/37318/MIT-CSAIL-TR-2007-025.pdf?sequence=1}}</ref>
 
The minimizer of <math>I[f]</math> for the square loss function can be directly found from equation (1) as
Line 141 ⟶ 139:
This function is undefined when <math>p(1\mid x)=1</math> or <math>p(1\mid x)=0</math> (tending toward ∞ and −∞ respectively), but predicts a smooth curve which grows when <math>p(1\mid x)</math> increases and equals 0 when <math>p(1\mid x)= 0.5</math>.<ref name="mitlec" />
 
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 <math>\frac{1}{\log(2)}</math>). 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 learning|deep neural networks]].
 
== Exponential loss ==
The exponential loss function can be generated using (2) and Table-I as follows
 
:<math>\phi(v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)] = 2\sqrt{\left(\frac{e^{2v}}{1+e^{2v}}\right)\left(1-\frac{e^{2v}}{1+e^{2v}}\right)}+\left(1-\frac{e^{2v}}{1+e^{2v}}\right)\left(\frac{1-\frac{2e^{2v}}{1+e^{2v}}}{\sqrt{\frac{e^{2v}}{1+e^{2v}}(1-\frac{e^{2v}}{1+e^{2v}})}}\right) = e^{-v}</math>
 
The exponential loss is convex and grows exponentially for negative values which makes it more sensitive to outliers. The exponentialexponentially-weighted 0-1 loss is used in the [[AdaBoost|AdaBoost algorithm]] giving implicitly rise to the exponential loss.
 
The minimizer of <math>I[f]</math> for the exponential loss function can be directly found from equation (1) as
Line 157 ⟶ 155:
The Savage loss<ref name=":0" /> can be generated using (2) and Table-I as follows
 
:<math>\phi(v)=C[f^{-1}(v)]+(1-f^{-1}(v))C'[f^{-1}(v)] = \left(\frac{e^v}{1+e^v}\right)\left(1-\frac{e^v}{1+e^v}\right)+\left(1-\frac{e^v}{1+e^v}\right)\left(1-\frac{2e^v}{1+e^v}\right) = \frac{1}{(1+e^v)^2}.</math>
 
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.
Line 166 ⟶ 164:
 
== Tangent loss ==
The Tangent loss<ref>{{Cite journalbook|lastlast1=Masnadi-Shirazi|firstfirst1=H.|last2=Mahadevan|first2=V.|last3=Vasconcelos|first3=N.|datetitle=June 2010 IEEE Computer Society Conference on Computer Vision and Pattern Recognition |titlechapter=On the design of robust classifiers for computer vision |urldate=https://ieeexplore.ieee.org/document/5540136|journal=2010June IEEE Computer Society Conference on Computer Vision and Pattern Recognition2010|pages=779–786|doi=10.1109/CVPR.2010.5540136|citeseerx=10.1.1.172.6416|isbn=978-1-4244-6984-0|s2cid=632758}}</ref> can be generated using (2) and Table-I as follows
 
:<math>
\begin{align}
\phi(v) & = C[f^{-1}(v)]+\left( 1-f^{-1}(v)\right) 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}))\\
\\ & = 4 \left( \arctan(v)+\frac{1}{2} \right) \left( 1- \left( \arctan(v)+\frac{1}{2} \right) \right) + \left( 1- \left( \arctan(v)+\frac{1}{2} \right) \right) \left( 4-8 \left( \arctan(v)+\frac{1}{2} \right) \right) \\
& = \left( 2\arctan(v)-1 \right) ^2.
\end{align}
</math>
 
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.<ref>{{Cite journalbook|lastlast1=Schulter|firstfirst1=S.|last2=Wohlhart|first2=P.|last3=Leistner|first3=C.|last4=Saffari|first4=A.|last5=Roth|first5=P. M.|last6=Bischof|first6=H.|date=June 2013|title=Alternating Decision Forests|url=https://ieeexplore.ieee.org/document/6618916|journal=2013 IEEE Conference on Computer Vision and Pattern Recognition |chapter=Alternating Decision Forests |date=June 2013|pages=508–515|doi=10.1109/CVPR.2013.72|citeseerx=10.1.1.301.1305|isbn=978-0-7695-4989-7|s2cid=6557162}}</ref>
 
The minimizer of <math>I[f]</math> for the Tangent loss function can be directly found from equation (1) as
 
:<math>f^*_\text{Tangent}= \tan \left( \eta-\frac{1}{2} \right) =\tan \left( p \left( 1\mid x \right) -\frac{1}{2}\right) .</math>
 
== Hinge loss ==
{{main|Hinge loss}}
The hinge loss function is defined with <math>\phi(\upsilon) = \max(0, 1-\upsilon) = [1-\upsilon]_{+}</math>, where <math>[a]_{+} = \max(0,a)</math> is the [[positive part]] function.
The hinge loss function is defined as
 
:<math>V(f(\vec{x}),y) = \max(0, 1-yf(\vec{x})) = |[1 - yf(\vec{x}) |]_{+}.</math>
 
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 <math>\operatorname{sgn}(f(\vec{x})) = y</math> and <math>|yf(\vec{x})| \geq 1</math>. 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.<ref name="Utah" />
Line 200 ⟶ 199:
The generalized smooth hinge loss function with parameter <math>\alpha</math> is defined as
 
:<math>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<z<1 \\ 0 & \text{if } z \geq 1 \end{cases},</math>
 
where
Line 206 ⟶ 205:
 
It is monotonically increasing and reaches 0 when <math>z = 1</math>.
 
== See also ==
*[[Differentiable programming]]
*[[Scoring function]]
 
== References ==
{{Reflist}}
 
{{Artificial intelligence navbox}}
 
[[Category:Machine learning algorithms]]