Calibration Regularized Training of Deep Neural Networks using Kernel Density Estimation

1 INTRODUCTION . Deep neural networks have shown tremendous success in classification tasks , being regularly the best performing models in terms of accuracy . However , they are also known to make overconfident predictions ( Guo et al. , 2017 ) , which is particularly problematic in safety-critical applications such as medical diagnosis or autonomous driving . Therefore , in many real world applications we do not just care about the predictive performance , but also about the trustworthiness of that prediction , that is , we are interested in accurate predictions with robust uncertainty estimates . To this end , we want our models to be uncertainty calibrated which means that , for instance , among all cells that have been predicted with a probability of 0.8 to be cancerous , in fact a fraction of 80 % belong to a malignant tumor . Being calibrated , however , does not imply that the classifier achieves good accuracy . For instance , a classifier that always predicts the marginal distribution of the target class is calibrated , but will not be very useful in practice . Likewise , a good predictive performance does not ensure calibration . In particular , for a broad class of loss functions , risk minimization leads to asymptotically Bayes optimal classifiers ( Bartlett et al. , 2006 ) . However , there is no guarantee that they are calibrated , even in the aysmptotic limit . Therefore , we consider minimizing the risk plus a term that penalizes miscalibration , i.e. , Risk +λ · CalibrationError . For parameter values λ > 0 , this will push the classifier towards a calibrated model , while maintaining similar accuracy . The existence of such a λ > 0 is suggested by the fact that there always exists at least one Bayes optimal classifier that is calibrated , namely P ( y|x ) . To optimize the risk and the calibration error jointly , we propose a differentiable and consistent estimator of the expected Lp calibration error based on kernel density estimates ( KDEs ) . In particular , we use a Beta kernel in binary classification tasks and a Dirichlet kernel in the multiclass setting , as these kernels are the natural choices to model density estimation over a probability simplex . Our Dirichlet kernel based estimator allows for the estimation of canonical calibration , which is the strongest notion of multiclass calibration as it implies the calibration of the whole probability vector ( Bröcker , 2009 ; Appice et al. , 2015 ; Vaicenavicius et al. , 2019 ) . By contrast , most other state-ofthe-art methods only achieve weaker versions of multiclass calibration , namely top-label ( Guo et al. , 2017 ) and marginal or class-wise calibration ( Kull et al. , 2019 ) . The top-label calibration only considers the scores for the predictied class , while for marginal calibration the multiclass problem is split up into K one-vs-all binary ones , each of which is required to be calibrated according to the definition of binary calibration . In many applications marginal and canonical calibration are preferable to top-label calibration , since we often care about having reliable uncertainty estimates for more than just one class per prediction . For instance , in medical diagnosis we do not just care about the most likely disease a certain patient might have but also about the probabilities of other diseases . Our contributions can be summarized as follows : 1 . We develop a trainable calibration error objective using Dirichlet kernel density estimates , which can be minimized alongside any loss function in the existing batch stochastic gradient descent framework . 2 . We propose to use our estimator to evaluate canonical calibration . Due to the scaling properties of Dirichlet kernel density estimation , and the tendency for probabilities to be concentrated in a relatively small number of classes , this becomes feasible in cases that can not be estimated using a binned estimator . 3 . We show on a variety of network architectures and two datasets that DNNs trained alongside an estimator of the calibration error achieve competitive results both on existing metrics and on the proposed measure of canonical calibration . 2 RELATED WORK . Calibration of probabilistic predictors has long been studied in many fields . This topic gained attention in the deep learning community following the observation in Guo et al . ( 2017 ) that modern neural networks are poorly calibrated and tend to give overconfident predictions due to overfitting on the NLL loss . The surge of interest resulted in many calibration strategies that can be split in two general categories , which we discuss subsequently . Post-hoc calibration strategies learn a calibration map of the predictions from a trained predictor in a post-hoc manner . For instance , Platt scaling ( Platt , 1999 ) fits a logistic regression model on top of the logit outputs of the model . A special case of Platt scaling that fits a single scalar , called temperature , has been popularized by Guo et al . ( 2017 ) as an accuracy-preserving , easy to implement and effective method to improve calibration . However , it has the undesired consequence that it clamps the high confidence scores of accurate predictions ( Kumar et al. , 2018 ) . Other approaches for post-hoc calibration include : histogram binning ( Zadrozny & Elkan , 2001 ) , isotonic regression ( Zadrozny & Elkan , 2002 ) , and Bayesian binning into quantiles ( Naeini & Cooper , 2015 ) . Trainable calibration strategies integrate a differentiable calibration measure into the training objective . One of the earliest approaches is regularization by penalizing low entropy predictions ( Pereyra et al. , 2017 ) . Similarly to temperature scaling , it has been shown that entropy regularization needlessly suppresses high confidence scores of correct predictions ( Kumar et al. , 2018 ) . Another popular strategy is MMCE ( Maxmimum Mean Calibration Error ) ( Kumar et al. , 2018 ) , where the entropy regularizer is replaced by a kernel-based surrogate for the calibration error that can be optimized alongside NLL . It has been shown that label smoothing ( Szegedy et al. , 2015 ; Müller et al. , 2020 ) , i.e . training models with a weighted mixture of the labels instead of one-hot vectors , also improves model calibration . Liang et al . ( 2020 ) propose to add the difference between predicted confidence and accuracy as auxiliary term to the cross-entropy loss . Focal loss ( Mukhoti et al. , 2020 ; Lin et al. , 2018 ) has recently been empirically shown to produce better calibrated models than many of the alternatives , but does not estimate a clear quantity related to calibration error . Kernel density estimation ( Parzen , 1962 ; Rosenblatt , 1956 ) is a non-parametric method to estimate a probability density function from a finite sample . Zhang et al . ( 2020 ) propose a KDE-based estimator of the calibration error for measuring calibration performance . However , they use the triweight kernel , which has a limited support interval and is therefore applicable to binary classification , but does not have a natural extension to higher dimensional simplexes , in contrast to the Dirichlet kernel that we consider here . As a result , they consider an unnatural proxy to marginal calibration error , which does not result in a consistent estimator . 3 METHODS . The most commonly used loss functions are designed to achieve consistency in the sense of Bayes optimality under risk minimization , however , they do not guarantee calibration - neither for finite samples nor in the asymptotic limit . Since we are interested in models f that are both accurate and calibrated , we consider the following optimization problem bounding the calibration error CE ( f ) : f = arg min f∈F Risk ( f ) , s.t . CE ( f ) ≤ B ( 1 ) for some B > 0 , and its associated Lagrangian f = arg min f∈F ( Risk ( f ) + λ · CE ( f ) ) . ( 2 ) We measure the ( mis- ) calibration in terms of the Lp calibration error . To this end , let ( Ω , A , P ) be a probability space , let X = Rd , Y = { 0 , 1 , ... , K } . Let x : Ω → X and y : Ω → Y be random variables while realizations are denoted with subscripts . Furthermore , let f : X → 4K be a decision function , where 4K denotes the K dimensional simplex as is achieved e.g . from the output of a final softmax layer in a neural network . Definition 3.1 ( Calibration error , ( Naeini et al. , 2015 ; Kumar et al. , 2019 ; Wenger et al. , 2020 ) ) . The Lp calibration error of f is : CEp ( f ) = ( E [ ∥∥∥E [ y | f ( x ) ] − f ( x ) ∥∥∥p p ] ) 1 p . ( 3 ) We note that we consider multiclass calibration , and that f ( x ) and the conditional expectation in Equation 3 therefore map to points on a probability simplex . We say that a classifier f is perfectly calibrated if CEp ( f ) = 0 . Kumar et al . ( 2018 ) have also considered a minimization problem similar to Equation 2 . Instead of using the CEp they use a metric called maximum mean calibration error ( MMCE ) that is 0 if and only if CEp = 0 . However , it is unclear how MMCE relates to the canonical multiclass setting or to the norm parameter p for non-zero CEp . In order to optimize Definition 3.1 directly , we need to perform density estimation over the probability simplex in order to empirically compute the conditional expectation . In a binary setting , this has traditionally been done with binned estimates ( Naeini et al. , 2015 ; Guo et al. , 2017 ; Kumar et al. , 2019 ) . However , this is not differentiable w.r.t . the function f , and can not be incorporated into a gradient based training procedure . Furthermore , binned estimates suffer from the curse of dimensionality and do not have a practical extension to multiclass settings . A natural choice for a differentiable kernel density estimator in the binary case is a kernel based on the Beta distribution and the extension to the multiclass case is given by the Dirichlet distribution . Hence , we consider an estimator for the CEp based on Beta and Dirichlet kernel density estimates in the binary and multiclass setting , respectively . We require that this estimator is consistent and differentiable such that we can train it according to Equation 2 . This estimator is given by : ̂CEp ( f ) p = 1 n n∑ h=1 [ ∥∥∥ ̂E [ y | f ( x ) ] ∣∣∣ f ( xh ) − f ( xh ) ∥∥∥p p ] , ( 4 ) where ̂E [ y | f ( x ) ] ∣∣∣ f ( xh ) denotes ̂E [ y | f ( x ) ] evaluated at f ( x ) = f ( xh ) . If Px , y has a probability density px , y with respect to the product of the Lebesgue and counting measure , we can define : px , y ( xi , yi ) = py|x=xi ( yi ) px ( xi ) . Then we define the estimator of the conditional expectation as follows : E [ y | f ( x ) ] = ∑ yk∈Y yk py|x=f ( x ) ( yk ) = ∑ yk∈Y yk px , y ( f ( x ) , yk ) px ( f ( x ) ) ( 5 ) ≈ ∑n i=1 k ( f ( x ) ; f ( xi ) ) yi∑n i=1 k ( f ( x ) ; f ( xi ) ) = : ̂E [ y | f ( x ) ] ( 6 ) where k is the kernel of a kernel density estimate evaluated at point xi . Proposition 3.2 . ̂E [ y | f ( x ) ] is a pointwise consistent estimator of E [ y | f ( x ) ] , that is : lim n→∞ ∑n i=1 k ( f ( x ) ; f ( xi ) ) yi∑n i=1 k ( f ( x ) ; f ( xi ) ) = ∑ yk∈Y yk px , y ( f ( x ) , yk ) px ( f ( x ) ) . ( 7 ) Proof . By the consistency of kernel density estimators ( Silverman , 1986 ; Chen , 1999 ; Ouimet & Tolosana-Delgado , 2021 ) , for all f ( x ) ∈ ( 0 , 1 ) , 1n ∑n i=1 k ( f ( x ) ; f ( xi ) ) yi n→∞−−−−→∑ yk∈Y yk px , y ( f ( x ) , yk ) and 1 n ∑n i=1 k ( f ( x ) ; f ( xi ) ) n→∞−−−−→ px ( f ( x ) ) . The fact that the ratio of two convergent sequences converges against the ratio of their limits shows the result . Mean squared error in binary classification As a first instantiation of our framework we consider a binary classification setting , with the mean squared error MSE ( f ) = E [ ( f ( x ) − y ) 2 ] as the risk function , jointly optimized with the L2 calibration error CE2 . Following Murphy ( 1973 ) ; Degroot & Fienberg ( 1983 ) ; Kuleshov & Liang ( 2015 ) ; Nguyen & O ’ Connor ( 2015 ) we decompose ( full derivation in Appendix A ) the MSE as : MSE ( f ) − CE2 ( f ) 2 = E [ ( 1− E [ y | f ( x ) ] ) E [ y | f ( x ) ] ] ≥ 0 . ( 8 ) Similar to Equation 2 , we consider the optimization problem for some λ > 0 : f = arg min f∈F ( MSE ( f ) + λCE2 ( f ) 2 ) . ( 9 ) Using Equation 8 we rewrite : MSE ( f ) + λCE2 ( f ) 2 = ( 1 + λ ) MSE ( f ) − λ ( MSE ( f ) − CE2 ( f ) 2 ) ( 10 ) = ( 1 + λ ) MSE ( f ) − λE [ ( 1− E [ y | f ( x ) ] ) E [ y | f ( x ) ] ] . ( 11 ) Rescaling Equation 11 by a factor of ( 1 + λ ) −1 and a variable substitution γ = λ1+λ ∈ [ 0 , 1 ) f = arg min f∈F ( MSE ( f ) + λCE2 ( f ) 2 ) = arg min f∈F ( MSE ( f ) − γE [ ( 1− E [ y | f ( x ) ] ) E [ y | f ( x ) ] ] ) ( 12 ) = arg min f∈F ( MSE ( f ) + γE [ E [ y | f ( x ) ] 2 ] ) . ( 13 ) For optimization we wish to find an estimator for E [ E [ y | f ( x ) ] 2 ] . Building upon Equation 6 , a partially debiased estimator can be written as:1 ̂ E [ E [ y | f ( x ) ] 2 ] ≈ 1 n n∑ h=1 ( ∑ i 6=h k ( f ( xh ) ; f ( xi ) ) yi ) 2 − ∑ i 6=h ( k ( f ( xh ) ; f ( xi ) ) yi ) 2 ( ∑ i 6=h k ( f ( xh ) ; f ( xi ) ) ) 2 − ∑ i6=h ( k ( f ( xh ) ; f ( xi ) ) ) 2 . ( 14 ) In a binary setting , the kernels k ( · , · ) are Beta distributions , i.e . denoting zi : = f ( xi ) for short , then : kBeta ( z , zi ) : = z αi−1 ( 1− z ) βi−1 Γ ( αi + βi ) Γ ( αi ) Γ ( βi ) , ( 15 ) with αi = zih +1 and βi = 1−zi h +1 ( Chen , 1999 ; Bouezmarni & Rolin , 2003 ; Zhang & Karunamuni , 2010 ) , where h is a bandwidth parameter in the kernel density estimate that goes to 0 as n → ∞ . We note that the computational complexity of this estimator is O ( n2 ) . Within the gradient descent training procedure , the density is estimated using a mini-batch and therefore the O ( n2 ) complexity is w.r.t . a mini-batch , not the entire dataset . The estimator in Equation 14 is a ratio of two second order U-statistics that converge as n−1/2 ( Ferguson , 2005 ) . Therefore , the overall convergence will be n−1/2 . Empirical covergence rates are calculated in Appendix D.3 and shown to be close to the theoretically expected value . 1We have debiased the numerator and denominator individually ( Ferguson , 2005 , Section 2 ) , but for simplicity have not corrected for the fact that we are estimating a ratio ( Scott & Wu , 1981 ) . Multiclass calibration with Dirichlet kernel density estimates There are multiple definitions regarding multiclass calibration that differ in the strictness regarding the calibration of the probability vector f ( x ) . The weakest notion is top label calibration , which , as the name suggests , only cares about calibrating the entry with the highest predicted probability , which reduces to a binary calibration problem again ( Guo et al. , 2017 ) . Marginal or class-wise calibration ( Kull et al. , 2019 ) is the most commonly used definition of multiclass calibration and a stronger version of top label calibration . Here , the problem is split into K one-vs-all binary calibration setting , such that each class has to be calibrated against the other K − 1 classes : MCEp ( f ) p = K∑ k=1 E [ ∣∣∣E [ y = k | f ( x ) k ] − f ( x ) k∣∣∣p ] . ( 16 ) An estimator for this calibration error is : ̂MCEp ( f ) p = K∑ k=1 1 n n∑ j=1 ∣∣∣∣∣ ∑ i 6=j kBeta ( f ( xj ) k ; f ( xi ) k ) [ yi ] k∑ i 6=j kBeta ( f ( xj ) k ; f ( xi ) k ) − f ( xj ) k ∣∣∣∣∣ p . ( 17 ) The strongest notion of multiclass calibration , and the one that we want to consider in this paper , is called canonical calibration ( Bröcker , 2009 ; Appice et al. , 2015 ; Vaicenavicius et al. , 2019 ) . Here it is required that the whole probability vector f ( x ) is calibrated . The definition is exactly the one from Definition 3.1 . Its estimator is : ̂CEp ( f ) p = 1 n n∑ j=1 ∥∥∥∥∥ ∑ i6=j kDir ( f ( xj ) ; f ( xi ) ) yi∑ i 6=j kDir ( f ( xj ) ; f ( xi ) ) − f ( xj ) ∥∥∥∥∥ p p ( 18 ) where kDir is a Dirichlet kernel defined as : kDir ( z , zi ) : = Γ ( ∑K i=1 αi ) ∏K i=1 Γ ( αi ) K∏ j=1 z αij−1 j ( 19 ) with αi = zi/h+ 1 ( Ouimet & Tolosana-Delgado , 2021 ) . As before , the computational complexity is O ( n2 ) irrespective of p. This estimator is differentiable and furthermore , the following proposition holds : Proposition 3.3 . The Dirichlet kernel based CE estimator is consistent , that is lim n→∞ 1 n n∑ j=1 ∥∥∥∥∥ ∑n i 6=j kDir ( f ( xj ) ; f ( xi ) ) yi∑n i6=j kDir ( f ( xj ) ; f ( xi ) ) − f ( xj ) ∥∥∥∥∥ p p = E [ ∥∥∥E [ y | f ( x ) ] − f ( x ) ∥∥∥p p ] p . ( 20 ) Proof . Dirichlet kernel estimators are consistent ( Ouimet & Tolosana-Delgado , 2021 ) , consequently , by Proposition 3.2 the term inside the norm is consistent for any fixed f ( xj ) ( note , that summing over i 6= j ensures that the ratio of the KDE ’ s does not depend on the outer summation ) . Moreover , for any convergent sequence also the norm of that sequence converges against the norm of its limit . Ultimately , the outer sum is merely the sample mean of consistent summands , which again is consistent .

Summary: The paper proposes a new approach for calibrating neural network outputs. The idea is to train the neural network with a regularized loss function that is a linear combination of prediction and calibration errors. The calibration error is measured as the Lp norm of the difference between predicted class probabilities and the expected true class probabilities given the predicted class probabilities where the latter term is computed using a kernel density estimate with a Dirichlet Kernel. The approach is evaluated in terms of accuracy and expected calibration error on CIFAR-10 and CIFAR-100.