P latt-Bin: Efficient Posterior Calibrated Training for NLP Classifiers (2022)

Abstract: Modern NLP classifiers are known to return uncalibrated estimations of class posteriors. Existing methods for posterior calibration rescale the predicted probabilities but often have an adverse impact on final classification accuracy, thus leading to poorer generalization. We propose an end-to-end trained calibrator, Platt-Binning, that directly optimizes the objective while minimizing the difference between the predicted and empirical posterior probabilities. Our method leverages the sample efficiency of Platt scaling and the verification guarantees of histogram binning, thus not only reducing the calibration error but also improving task performance. In contrast to existing calibrators, we perform this efficient calibration during training. Empirical evaluation of benchmark NLP classification tasks echoes the efficacy of our proposal.

## introduction deep learning has proven to be tremendously attractive for researchers in fields such as physics, biology, and manufacturing, to name a few @xcite @xcite . however, these are fields in which representing model uncertainty is of crucial importance @xcite . a common way to incorporate dnns in other fields is to use the predictions of a trained classifier for decision making in a downstream task. in some cases the effectiveness of the decisions depends on a utility function and it is not enough to simply predict the most likely label for each example. what is needed instead is to quantify model uncertainty about the predictions. despite promising performance in supervised learning benchmarks in terms of accuracy, dnns are poor at quantifying predictive uncertainty, and tend to produce overconfident predictions. overconfident incorrect predictions can be harmful or offensive in nlp applications @xcite , hence proper uncertainty quantification is crucial in practice. probabilistic uncertainty in machine learning translates to estimation of the probability mass function p(y|x) by the model, where x is the input sample and y is a class label. recent works have shown that stateof-the art structured prediction models are poorly calibrated. therefore, blindly using the output of the softmax function output as the model uncertainty is misleading @xcite @xcite . we are interested in calibrating the posterior estimates, i.e. we wish to get posterior probability estimations that reflect the true probability of the classes. the probability that a system outputs for an event should reflect the true frequency of that event: if an automated diagnosis system says 1,000 patients have cancer with probability 0.1, approximately 100 of them should indeed have cancer @xcite . even if the actual mechanism might be difficult to interpret, a calibrated model at least gives us a signal that it "knows what it doesn't know," thereby making these models easier to deploy in practice @xcite . we define perfect calibration as follows. irrespective of the choice of the calibration method, existing works generally calibrate the posterior distribution predicted from the classifier after training. these post-processing calibration methods re-learn an appropriate distribution from a held-out validation set and then apply it to an unseen test set. the fixed split of the data sets and insufficient number of samples for training the calibration function adversely affects the generalization of post-hoc calibrated classifiers and reduce their accuracy. in this paper we try to address some of the existing challenges in achieving apt calibration. in particular our contributions are: ## related works model uncertainty estimation and posterior calibration is a topic of continued interest not only in the fields of machine learning and statistics, but also in meteorology @xcite , fairness @xcite , healthcare @xcite , reinforcement learning @xcite , natural language processing @xcite , speech recognition @xcite and economics @xcite . in probabilistic models, the principal goal of estimation of the posterior p(y|x) given a sample @xmath0, is to assign low confidence to samples that were not explained well by the training data. one common way to calibrate multi-class posteriors after training the classifier f : @xmath1. in this case, model uncertainty is quantified by normalizing the estimation of p(@xmath2. generalization of calibration tests with kernel methods can be found in @xcite . various binary calibration methods can be used to estimate the marginal posterior over a calibration dataset, ranging from parametric approaches (e.g. platt scaling, temperature scaling, vector scaling @xcite ), to non-parametric methods (e.g. quantile or bayesian binning @xcite , and isotonic regression @xcite . another way to reduce the problem to binary calibration is by estimating model accuracy conditioned on its confidence, p(@xmath3. multi-class calibration aims to estimate the distribution of class labels conditioned on the estimated probability vector, p(y|f (x)). in this case the sample complexity is exponential in the number of classes and therefore with large number of classes, the main challenge is to constrain the hypothesis space with regularization. some of the proposed methods for this purpose are matrix scaling and dirichlet scaling which both use linear models for estimation of p(@xmath4, and mlp and order preserving functions @xcite . another approach is to account for model uncertainty via bayesian models. in bayesian neural networks (bnns) the predictive uncertainty will naturally be high in regions where training data is scarce @xcite . however, the marginalization of the weights in bnn is intractable in general. consequently, following papers propose various approximations such as variational inference (vi) @xcite . although bnns are theoretically proven to control the overconfidence of the model in unseen regions of data space @xcite , they require expensive approximations which limit their application in most modern nlp architectures. for instance, in @xcite the authors model the distribution on posterior probability using a dirichlet prior distribution and variational inference. mcdropout is a variational approximation of gaussian processes that avoids explicit modeling of the posterior distribution @xcite . both of these methods require modification of training of the network. in nlp, tasks with structured outputs posterior calibration are particularly challenging. this is because the number of classes are exponentially large and estimation of every posterior density or marginal posterior density is not possible. previous works such as @xcite propose to use the downstream task with small number of classes to perform calibration and estimation of the calibration error. in structured prediction models, calibration is also important for the generation of the structured outputs as the decoding algorithm relies on the posterior estimates to efficiently search through the space of sequences. however, estimation of the sequence calibration error and its correction is intractable. to cope with this problem, approximate calibration methods using a set of interesting events and feature based calibration are proposed in @xcite and an alternative calibration error estimator was proposed using sequence precision scoring function bleu in @xcite . we are considering the first class of problems and leave the structured calibration to future work. ## method in general, nlp classifiers work by first predicting a posterior probability distribution over all classes and then selecting the class with the largest estimated probability. however, these models are often poorly calibrated. existing calibration methods re-learn an appropriate distribution from a heldout validation set and then apply it to an unseen test set which degrades the model performance. alternatively, we can dynamically estimate the required statistics for calibration from the train set during training iterations, thereby minimizing cross-entropy as well as the calibration error as a multi-task setup @xcite . given a training set @xmath5, y 1 )..(x n , y n )}, where x i is an n-dimensional vector of input features and y i is a k-dimensional one-hot vector corresponding to its true label (with k classes), we minimize the loss l train : here l class is the classification loss (for eg. crossentropy) based on the predicted probability p ik updated during training for sample i and class k: l cal is the calibration loss which acts as a regularizer. it essentially tries to minimize the difference between the updated probability p and true posterior probabilities q via a distance function d (eg. mean squared error, kl-divergence, etc.): one crucial step here is to estimate the empirical probability q, which can be done by histogram binning method. here, we measure the ratio of true labels for each bin split by the predicted posterior p from each update. this refers to calempprob() function in algorithm 1. we store the results in empirical probability matrix @xmath6, where b is the number of bins used for each posterior dimension. histogram binning outputs probabilities from a finite set. unlike scaling methods, it can produce a model that is calibrated and measure its calibration error. however, the number of samples required to calibrate scales linearly with the number of distinct probabilities b the model can output which can be large in the multi-class setting @xcite . in this work, we propose an adaptive binning method that circumvents this bottleneck. we leverage the sample efficiency of platt scaling @xcite and the verification guarantees of histogram binning @xcite ) by defining the platt-binning calibrator. the problem with scaling methods is we cannot estimate their calibration error. the upside of scaling methods is that if the function family has at least one function that can achieve calibration error ϵ they require o(1/ϵ 2 ) samples to reach calibration error ϵ, while histogram binning requires o(b/ϵ 2 ) samples. platt-binning calibrator facilitates estimation of calibration error while being sampleefficient at the same time. platt scaling calibrator: since most modern deep learning classifiers do not output calibrated probabilities out of the box, recalibration methods take the output of an uncalibrated model, and transform it into a calibrated probability. that is, given a trained model f : @xmath7, 1], let @xmath8. we are given recalibration data @xmath9, y i )} n @xmath10, and we wish to learn a calibrator g : [0, 1] → [0, 1] such that g • f is well-calibrated. conventional scaling methods, for example platt scaling, output a function g: platt-binning calibrator builds at the intersection of the above two methods. given a recalibration data t of size n, platt-binning calibrator outputs ĝβ such that ĝβ • f has a low calibration error by using the following procedure: step 2: choose the bins so that an equal number of g(z i ) in t land in each bin b j for each @xmath11, ..., b step 3: discretize g, by outputting the average g value in each bin. let µ(s) = 1 |s| @xmath12. we set ĝβ (z) = µ(β(g(z)))we output the mean value of the bins that g(z) falls in. the motivation behind our method is that the g values in each bin are in a narrower range than the label values y, so when we take the average we incur lower estimation error. if g is well chosen, our method requires o( 1 ϵ 2 + b)samples to achieve calibration error ϵ instead of o( b ϵ 2 ) samples for histogram binning. all these steps are performed during training as explained in the pseudo-code in algorithm 1. to the best of our knowledge, such a formulation is novel among existing calibrators that tackle the problem during training. also, the whole approach is the first to be utilised to calibrate classifiers in the nlp domain. in the following section we prove the efficacy of our method by carrying out extensive evaluation of the performance of pretrained transformer models such as bert @xcite on simple multi-class text classification tasks. our motivation comes from the analysis in @xcite ) which shows that pretrained models are significantly better calibrated when used out-of-the-box. ## experiments in the experiments we fine-tune the parameters on pre-trained bert classifier using the regularized loss in equation ( 1 ). we compare our method to the following baselines: here, nn refers to a neural network with the component-wise logistic function. model is fit using one-vs-all binarization of the classification task. instead of the estimated posterior- we test the baselines and our method on the benchmark on nlp classification tasks: xslue @xcite . xslue contains classification benchmark on different types of styles such as a level of humor, formality and even demographics of authors. we train our method with two types of calibrators: in the first calibration task we train a calibrator for the most confident prediction of the classifier and call this version plattbintop (pbtop). the pseudocode of this version is illustrated in algorithm (1). in the second version we train a separate platt scaler and histogram binning for each class in a one-vs-all manner and we call this version of calibration plattbin (pb). while this version is exactly the same as plattbintop for binary tasks, it results in a very different solution for tasks with @xmath13. the pseudocode of this version is omitted due to being mainly similar to the other version with one additional loop over the classes at line 7 of algorithm (1) and conversion of label y and ŷ to one-vs-all binary labels. we report task accuracy, f1 score and ece as the evaluation metrics. we now analyse how our method behaves in comparison to mle at sample level during test time. to the total size of the test set for the reverse scenario. this quantitative analysis reinstates that our method, pb, has better model performance at test time, thereby establishing that it generalizes well while reducing calibration error. we extend the discussion above by analysing qualitative results in in the first two cases from stanfordpoliteness dataset, the level of politeness (e.g., "hey!" in s1) or arrogance (e.g., "what?" in s2) indicated on phrases is not captured well by mle, so it predicts the incorrect label while pb gives a correct prediction. however, for the rest two cases, mle gives confident correct predictions taking into account phrases such as "like" in s1 or a slightly difficult example in s2 but pb fails (only slightly in s2 though) to give correct predictions. arguing on similar lines for the multi-class case, we witness cases where mle fails to classify correctly (eg. s1 and s2 in hateoffensive) but pb gives highly confident predictions and vice-versa. from our manual investigation above, we find that statistical knowledge about posterior probability helps cor-rect p while training pb, so making p switch its prediction. for further analysis, we provide more examples in appendix ??. in figure 1 we show the calibration plots for three datasets: dailydialog, hateoffensive, and stan-fordpoliteness. we divide test samples according to the most confident estimated posterior into 10 bins. we plot the accuracy of the classifier versus the average classification confidence in each one of the bins in the top row. we also plot the number of samples in each calibration bin versus the classification confidence in the bottom row. ideally, a calibrated classifier would assign a probability to the top class that is equivalent to its accuracy. therefore, the accuracy-confidence curve of a calibrated classifier is close to the dashed grey curve in the top row. when platt-bin and platt-bin-top are further away from the calibration line it is because the number of samples in corresponding bins are low or even 0 in some cases. the bins with 0 samples in them can be ignored as they don't play a role in the classifier predictions. however, the distance of the curves is not enough to determine model calibration as most of the samples are assigned to the bin with highest estimated posterior. thus, correcting the calibration error in the bins with more samples is more effective in improving the expected calibration error. platt-binning and platt-binning-top algorithms increase the number of samples with lower classification confidence in all three of the illustrated tasks, while in comparison to mle with no regularization they only reduce classification accuracy by a negligible amount and even increase the accuracy for hateoffensive task. although, the classifier become visibily underconfident in hateoffensive task where post-hoc platt scaling has a more calibrated output. while the ece doesn't improve in stanfordpoliteness, platt-binning algorithm doesn't increase the ece as much as poscal regularization. we conjecture that such a behavior is demonstrated due to better sample efficiency of our algorithm. we conclude our analysis by observing the effect of two important parameters to this discussion-b: number of histogram bins used for calibration, and λ: strength of the regularization. figure 2 shows how calibration error (ece) vary when the number of bins b is varied as {10, ...100}. we see that the calibration error of all the methods have an increasing trend as b is increased. one plausible ## conclusion in this work we proposed a simple yet effective method called platt-binning calibrator for better posterior calibration. our method has theoretically lower sample complexity than histogram binning, giving us the best of scaling and binning methods. and unlike the existing post-processing calibration methods, platt-binning directly penalizes the difference between the predicted and the true (empirical) posterior probabilities dynamically over the training steps. our empirical analysis corroborates that platt-binning can not only reduce the calibration error but also increase the task performance on the classification benchmarks. for tasks where the reduction in calibration error is low, our method maintains the performance of the model instead of degrading it as seen for other existing calibrators. moreover, our method can be extended to any classification model as an additional component in the loss function, thus jointly optimised during training. there are many exciting avenues for future works in this regard. it will be interesting to assess how our method can provide advantages in the scenarios of domain adaptation and transfer learning. moreover, exploring alternatives to the model family g from which estimate ĝ is considered can be a direction of improvement. lastly, optimizing the overall method for huge datasets can be an essential extension. our method may also assist in analysing the bias and fairness aspects of the predictions made by nlp classifiers. this can facilitate ethical deployment of nlp models for real-world applications. true label @xmath15.32 0.70 our pleasure . please fill out this form , leaving your address and telephone number . noemotion @xmath16.30 0.55 sounds good . what are you going to have for your main course ? surprise @xmath17.17 0.60 really ? what did you get one for ? happiness @xmath18.13 0.82 i'm glad to help you . what's wrong ? anger @xmath19.12 0.36 damn it ! i'm injured here . we could wait all day for the police . anger @xmath20.14 0.41 to hell with you . the accident was your fault . anger @xmath21.11 0.39 to hell with you . noemotion @xmath22.73 0.43 no problem . noemotion @xmath23.99 0.31 of course . the fitting room is right over there . happiness @xmath24.61 0.46 great , thanks . noemotion @xmath25.78 0.34 hello ! happiness @xmath26.64 0.15 sure thing , follow me . this here is the . noemotion @xmath27.90 0.36 well , if you ever want to visit korea , i would be happy to show you around . anger @xmath28.41 0.36 i might just ! enjoy your stupid game ! noemotion @xmath29.81 0.40 but he seems to be very happy with rose . happiness @xmath30.53 0.08 so sorry . next time we'll go , thanks anyway . disgust @xmath31.49 0.28 i dislike it most . noemotion @xmath32.98 0.42 it was a real red letter day for you . noemotion @xmath33.96 0.37 yeah . we rolled out the red carpet to welcome him home .