# Uncertainty Calibration in Bayesian Neural Networks via Distance-Aware
Priors

Gianluca Detommaso
AWS
<EMAIL_ADDRESS>&Alberto Gasparin11footnotemark: 1
Amazon
<EMAIL_ADDRESS>&Andrew Wilson
Amazon
<EMAIL_ADDRESS>&Cedric Archambeau
AWS
<EMAIL_ADDRESS>equal contribution

###### Abstract

As we move away from the data, the predictive uncertainty should increase,
since a great variety of explanations are consistent with the little available
information. We introduce Distance-Aware Prior (DAP) calibration, a method to
correct overconfidence of Bayesian deep learning models outside of the
training domain. We define DAPs as prior distributions over the model
parameters that depend on the inputs through a measure of their distance from
the training set. DAP calibration is agnostic to the posterior inference
method, and it can be performed as a post-processing step. We demonstrate its
effectiveness against several baselines in a variety of classification and
regression problems, including benchmarks designed to test the quality of
predictive distributions away from the data.

##  1 Introduction

Uncertainty quantification is crucial in a variety of real-world applications,
such as human decision making in medicine, risk assessment in automatic
vehicles, anomaly detection in fraud and cybersecurity. Uncertainty is often
compartmentalized into aleatoric and epistemic components [17, 29, 13].
Aleatoric uncertainty characterizes the intrinsic stochasticity in the data
given the data generating process. Epistemic uncertainty describes our
inability to identify the data generating process because of lack of
information [13]. Aleatoric uncertainty is irreducible because, given a data
generating process, the noise in the data cannot be decreased. Epistemic
uncertainty can be reduced by either better modelling or by collecting more
data. In general, we expect epistemic uncertainty to be small close to the
training set, where data is available, and large outside of the training
domain, when data is either not available or very sparse. We refer to these
two situations as in-distribution and out-of-distribution (OOD). Deep learning
models tend to overfit the training data, erroneously providing the same
uncertainty regardless of where we wish to make a prediction [11].

In Bayesian neural networks, one places a prior over parameters, inducing a
prior over functions that represents epistemic uncertainty. The most commonly
used prior is an independent Gaussian prior over parameters [43]. In practice
issues with approximate inference can have a significant effect on the ability
of these approaches to represent growing uncertainty away from the data [28,
8, 48]. Moreover, even with high quality inference, these priors lead to poor
predictive distribution in OOD settings [14].

In the classic framework, model parameters and input data are assumed to be
independent a priori [43], failing to represent our honest beliefs that the
uncertainty should grow as we move away from the data. However, representing
these beliefs is particularly desirable in OOD settings, where the data
provides none or scarce information about the model parameters, and the prior
distribution is the main determinant of the epistemic uncertainty.

For this reason, we drop the independence assumption between model parameters
and inputs, and propose uncertainty calibration via distance-aware priors
(DAPs). These priors over the model parameters induce larger model uncertainty
at inputs further from the training data. Since the prior distribution has a
strong influence on the predictive distribution in parts of the input space
where data is sparse or not available, we can calibrate the input-dependent
increase in prior uncertainty to remedy overconfidence OOD. As we do not
impose any restriction on the target data or likelihood function, DAP
calibration can be performed in both classification and regression problems.
Furthermore, DAP calibration is agnostic to the posterior inference method
[22, 9, 45, 28, 34, 50], and it can be performed as a post-processing step.

We remark that although DAP calibration only targets OOD uncertainty, it can
be seamlessly combined with methods better tailored to calibrate in-domain
uncertainty, such as temperature scaling [35, 11, 21, 23, 41, 16].
Nevertheless, throughout this paper we will exclusively focus on OOD
uncertainty calibration, in order to avoid confounding results.

We can summarize the contributions of this work as follows. (1) We propose a
novel method to calibrate OOD uncertainty estimates of Bayesian neural network
models. (2) We introduce a simple distance-aware Gaussian prior, capturing the
dependence of the model parameters to the inputs through a parametrized
distance. (3) We consider a distance measure based on neural network feature
mapping and (4) introduce a calibration loss function to achieve a desired
level of uncertainty OOD, in both classification and regression problems. (5)
We propose an importance sampling procedure that allows to perform calibration
as a post-processing step. (6) We study the effectiveness of DAP calibration
over several experiments, and against several methods for OOD uncertainty
estimation.

##  2 Related Work

The concept of distant-aware uncertainty is native in Gaussian Processes (GPs)
[42], where a kernel captures a measure of distance between pairs of inputs.
Modern approaches combine Radial Basis Function (RBF) kernels with deep
feature extractors, i.e. deep neural networks that transform the input space
in order to obtain a better fit of the data [4]. This approach is commonly
referred to as Deep Kernel Learning (DKL) [46, 44], and has recently inspired
a variety of methods for deterministic uncertainty estimation. SNGP [26] is
one such method, which builds upon DKL proposing to approximate a GP via
Random Fourier Features (RFFs) [36]. In the same work the authors propose a
theoretical analysis of the uncertainty estimation problem, identifying input
distance awareness as a key property for the task. In practice, they show how
requiring deep learning models to satisfy a bi-Lipschitz condition (enforced
via Spectral Normalization (SN) [31]) can actually make the model’s predictive
uncertainty input distance aware. [1] adopts a similar approach to the one
described in SNGP, where SN is used along with residual networks to enforce
the bi-Lipschitz condition, while a variational inducing point GP
approximation is used instead of RFFs.

In Deep Uncertainty Quantification (DUQ) [40], another single-forward pass
method, the bi-Lipschitz constraint is enforced via a two-sided gradient
penalty [10], while predictions are made via a RBF network which measures the
distance between the transformed test input and some class centroids. This
limits its scope to classification only. These methods are competitive with
Deep Ensembles on multiple OOD benchmarks, but still requires changes in the
training procedure and, as such, cannot be applied as a post-processing step.
A simpler alternative that addresses this issue is Deep Deterministic
Uncertainty (DDU) [32], where Gaussian Discriminant Analysis is fit after
model training and later used for characterizing epistemic uncertainty, while
the softmax predictive entropy is used for aleatoric uncertainty. Similarly to
[40, 26, 1], this method performs best when SN is used in the feature
extractor, outperforming both SNGP and DUQ on popular OOD classification
benchmarks.

In this work, rather than introducing a distance-aware output function, we
define distance-aware prior distributions in order to obtain uncertainty
estimates that grow as we move away from the training data. Like DDU, DAP
calibration does not require any change to neither the deep learning model,
nor the posterior inference procedure.

##  3 Distance-aware priors

We assume to be given a training data set $\\{(x_{i},y_{i})\\}_{i=1}^{N}$ of
inputs $x_{i}$ and targets $y_{i}$ We denote collections of training inputs
and targets by $\boldsymbol{x}$ and $\boldsymbol{y}$, respectively. After the
training data is observed, we are interested in quantifying the uncertainty of
validation targets $y_{j}^{*}$, given validation inputs $x_{j}^{*}$, for
$j=1,\dots,N^{*}$. We do so by looking at statistics defined over the
predictive density $p(y_{j}^{*}|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$,
which is obtained by integrating out the parameters
$\theta\in\mathbb{R}^{n_{\theta}}$ of the deep learning model:

$p(y_{j}^{*}|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})=\int_{\mathbb{R}^{n_{\theta}}}p(y_{j}^{*}|\theta,x_{j}^{*})\,p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})\,d\theta,$
(1)

where $p(y_{j}^{*}|\theta,x_{j}^{*})$ denotes the test likelihood function and
$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ the posterior
distribution.

In (1), we followed the typical assumption that the test target is independent
of the training data given the test input and the model parameters [2, 12],
and hence we can write
$p(y_{j}^{*}|\theta,x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ as
$p(y_{j}^{*}|\theta,x_{j}^{*})$. The idea behind this assumption is that the
parameters $\theta$ capture all the information in the training set necessary
to characterize $y_{j}^{*}$.

A second common assumption is that the prior distribution of the model
parameters $\theta$ is independent of the inputs $\boldsymbol{x}$ and
$x_{j}^{*}$. In scenarios where the interpretation of the model parameters is
explainable, this assumption may be justified by the lack of a clear causal
relation from inputs to parameters. In deep learning models, on the other
hand, the model parameters have no clear interpretation, hence the
independence assumption is dictated by practical reasons rather than a
principled understanding of the dependency structure. In fact, in order to
compensate for the consequences of this independence assumption, input data
are often normalized and outliers removed, with the goal of making parameters
“compatible” with all the inputs passed to the model, instead of letting the
distribution of the model parameters depend on the actual inputs. While
practical, from an epistemic (model) uncertainty point of view this assumption
may be harmful, as it is taking away from the model parameters the possibility
to express more or less uncertainty depending on the inputs. For example, it
is reasonable to assume that the model should be able to express a priori more
uncertainty far away from the training inputs, and less close by.

For these reasons, we do not assume that the model parameters are independent
of the inputs, and will construct prior distributions
$p(\theta|\boldsymbol{\xi})$ that express more uncertainty when arbitrary
collections of inputs $\boldsymbol{\xi}$ are far from the training inputs, and
less otherwise. We will refer to this type of distributions as distance-aware
priors. Figure 1 shows the directed graphical model, where the blue arrows
correspond to the dependencies that we account for, in contrast with the
standard assumption described earlier.

$\boldsymbol{x}$$x_{j}^{*}$$\theta$$\boldsymbol{y}$$y_{j}^{*}$ Figure 1:
Unlike standard priors in deep learning, we assume not only the targets $y$
but also the model parameters $\theta$ explicitly depend on the data inputs
$x$, as indicated by the blue arrows.

It also makes clear that in this framework there is a difference between the
posterior density $p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ that
appears in (1) and $p(\theta|\boldsymbol{x},\boldsymbol{y})$, since $\theta$
and $x_{j}^{*}$ are not independent given the training data $\boldsymbol{x}$
and $\boldsymbol{y}$.

###  3.1 Independent and joint predictions are not the same

We note that because of the blue arrows in Figure 1, in this framework there
is a difference between predicting independently and jointly: the joint
predictive
$p(\boldsymbol{y}^{*}|\boldsymbol{x}^{*},\boldsymbol{x},\boldsymbol{y})$ and
the product of the independent predictive distributions
$p(y_{j}^{*}|\boldsymbol{x}_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ are not
the same, since $p(\theta|x_{j}^{*},\boldsymbol{x})$ and
$p(\theta|\boldsymbol{x}^{*},\boldsymbol{x})$ are different.

The right distribution to use depends on the application. If we are interested
in a certain prediction given that we are observing a certain set of inputs
(e.g. predicting the score of a student, given that scores are normalized over
the results of the whole class), the joint predictive may be the right
distribution to use. Otherwise, if we are interested in simultaneous
independent predictions, we would rather use the independent predictive
distribution. Throughout this paper we use the individual predictive, since we
would argue that the latter situation is more commonly desired in machine
learning applications.

###  3.2 Distance-aware priors

We define distance-aware priors as distributions over the model parameters
that depend on the inputs through a measure of their distance from the
training inputs. In this work we use the term distance to indicate set-to-set
distance, typically from an arbitrary set of inputs $\boldsymbol{\xi}$ to the
training set $\boldsymbol{x}$. We require a distance function to be always
non-negative, and to satisfy the triangular inequality. Notice that we drop
the symmetry assumption since it is not needed for the purpose of this work.
We use the notation $d_{\phi}(\boldsymbol{\xi})$ to indicate a distance from
$\boldsymbol{\xi}$ to $\boldsymbol{x}$ parametrized by
$\phi\in\mathbb{R}^{n_{\phi}}$, where for sake of notation we leave the
argument $\boldsymbol{x}$ implicit.

While there are many possible ways to define a distance-aware prior,
throughout this paper we will consider the following distance-aware Gaussian
prior:

$p(\theta|\boldsymbol{\xi})=\text{Normal}(\theta\,|0,\,(\sigma_{0}+d_{\phi}(\boldsymbol{\xi}))^{2}I_{n_{\theta}}),$
(2)

where $\boldsymbol{\xi}$ is an arbitrary collection of inputs,
$\sigma_{0}\in\mathbb{R}_{+}$ is a standard deviation parameter,
$I_{n_{\theta}}$ is an identity matrix of size $n_{\theta}$ and
$d_{\phi}(\boldsymbol{x}^{*})\in\mathbb{R}_{+}$ is a distance metric
parametrized by $\phi\in\mathbb{R}^{n_{\phi}}$.

The definition of distance-aware Gaussian in (2) could be easily extended by
considering an array of distance
$d_{\phi}(\boldsymbol{\xi})\in\mathbb{R}_{+}^{n_{\theta}}$ instead of a scalar
quantity, which would allow to rescale the prior variance independently for
each component of $\theta$. However, throughout this paper we will not
consider this option, since a scalar distance is practically easier to
calibrate over a low-dimensional parameter $\phi$.

###  3.3 Requiring the distance to be zero at training inputs

We further require that $d_{\phi}(\boldsymbol{x})=0$, i.e. the distance
evaluated at the collection of training inputs must be 0. It follows that, at
training time, the distance-aware Gaussian prior introduced in 3.2 boils down
to a classic Gaussian prior, i.e.
$p(\theta|\boldsymbol{x})=\text{Normal}(\theta|0,\sigma_{0}^{2})$, which does
not depend on the distance nor on its parameters $\phi$. Furthermore, by
Bayes’ theorem we can write

$p(\theta|\boldsymbol{x},\boldsymbol{y})=p(\boldsymbol{y}|\theta,\boldsymbol{x})p(\theta|\boldsymbol{x})/p(\boldsymbol{y}|\boldsymbol{x}).$

The posterior $p(\theta|\boldsymbol{x},\boldsymbol{y})$ is not distance
dependent since none of the terms on the right hand side depend on distance.
As such, the latter corresponds to the posterior of standard frameworks, and
it can be inferred via classic posterior inference methods [33, 3, 27, 22, 9].
The difference between classic priors and distance-aware priors arises at
prediction time, when we need to evaluate the posterior
$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$. In fact, by Bayes’
theorem this posterior can be expressed in terms of the prior
$p(\theta|x_{j}^{*},\boldsymbol{x})$, which in turn depends on the distance
$d_{\phi}([x_{j}^{*},\boldsymbol{x}])$, that is generally non-zero.

###  3.4 Choice of distance in distance-aware priors

In this paper we consider a distance

$d_{\phi}(\boldsymbol{\xi}):=g(\phi)d_{0}(\boldsymbol{\xi}),$ (3)

where $g(\cdot)$ is a scalar positive function of $\phi\in\mathbb{R}$, while
$d_{0}(\boldsymbol{\xi})$ is a distance independent of $\phi$. We address
$d_{0}(\cdot)$ as pre-distance, since it corresponds to the distance
$d_{\phi}(\cdot)$ before the scaling $g(\phi)$ has been applied. The choice in
(3) is practically compelling for calibration over $\phi$, since after the
pre-distance $d_{0}(\boldsymbol{\xi})$ is pre-computed, the distance
$d_{\phi}(\boldsymbol{\xi})$ can be cheaply evaluated for different values of
$\phi$. The function $g(\phi)$ acts as a scaling factor, which regulates the
strength of the distance in the variance of the distance-aware prior, for the
specific collection of inputs $\boldsymbol{\xi}$.

We require the scaling function $g(\phi)$ to be a smooth function with
positive image defined on the reals. This condition allows us to optimize with
respect to $\phi$ in an unbounded domain. In this paper we simply choose
$g(\phi)=e^{\phi}$, but other functions, e.g. a softplus or a scaled sigmoid,
are also valid choices. We take the parameter $\phi$ to be a scalar to promote
scalability and robustness; other choices are possible.

As pre-distance, we consider a projected nearest neighbor asymmetric distance
given by

$d_{0}(\boldsymbol{\xi}):=\max_{k}\min_{i=1\dots,N}\|\mathcal{P}(\xi_{k})-\mathcal{P}(x_{i})\|_{2},$
(4)

where $\xi_{k}$ refers to the inputs in the collection $\boldsymbol{\xi}$ and
$\mathcal{P}(\cdot)$ is a projection operator that transforms the input space
into a low-dimensional latent space. The projector $\mathcal{P}$ can be
defined in several ways, for example via a variational autoencoder (VAE) [19],
random kitchen sinks [36], or as a deep feature extractor, i.e. as the forward
pass of a trained deep learning model, up to the last hidden layer [4].
Because at posterior inference time we already train a deep learning model
from input to output, throughout this paper we exploit the latter solution as
a projector. We remark that the pre-distance in (4) is strongly related to the
Hausdorff distance [38], which, unlike (4), is symmetric. Since the symmetry
property is not needed for the purpose of this paper, we use (4) because
cheaper to compute.

It is important to notice that, since we are interested in independent
predictions, as discussed in Section 3.1, we only ever need to evaluate the
distance at collections $\boldsymbol{\xi}=\boldsymbol{x}$ and
$\boldsymbol{\xi}=[x_{j}^{*},\boldsymbol{x}]$. From (4), it is immediate to
see that $d_{\phi}(\boldsymbol{x})=0$, satisfying the property required in
Section 3.2, which makes the posterior
$p(\theta|\boldsymbol{x},\boldsymbol{y})$ independent of the distance. On the
other hand, we observe that

$d_{\phi}([x_{j}^{*},\boldsymbol{x}])=d_{\phi}(x_{j}^{*})=\min_{i=1\dots,N}\|\mathcal{P}(x_{j}^{*})-\mathcal{P}(x_{i})\|_{2}.$

Hence the maximum operator in (4) is never needed, and it could be formally
replaced by any other function over the index $k$, e.g. a sum operator.

##  4 Estimation and calibration of predictive uncertainty

In this section we introduce importance sampling as a convenient method to
estimate statistics with respect to the predictive
$p(y_{j}^{*}|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$. Furthermore, we
introduce a calibration strategy to correct overconfidence OOD as a post-
processing step.

###  4.1 Importance sampling to compute statistics over the posterior
predictive distribution

As discussed in Section 3.1, we are interested in generating independent
predictions for each of the inputs $x_{j}^{*}$. In order to do so, one may
write the predictive distribution in (1) as an expectation with respect to the
posterior $p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$, and estimate it
via a Monte Carlo integration. However, since for each $j$ we require an
individual posterior inference approximation, inference can become quickly
infeasible. Furthermore, as we will see in Section 4.2, in order to calibrate
the distance $d_{\phi}([x_{j}^{*},\boldsymbol{x}])$ in the distance-aware
priors, we might want predictive estimates for several values of $\phi$,
making this approach even more computationally intensive.

In order to circumvent all these problems at once, we propose to use
importance sampling, where we adopt the posterior
$p(\theta|\boldsymbol{x},\boldsymbol{y})$ as importance distribution. We note
that the latter posterior does not depend on $x_{j}^{*}$, nor does it depend
on $\phi$: as in Section 3.3, we required $d_{\phi}(\boldsymbol{x})=0$. We
then rewrite the predictive in (1) as

$p(y_{j}^{*}|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})=\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}[p(y_{j}^{*}|\theta,x_{j}^{*})w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})],$
(5)

where we refer to $w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ as the
posterior weight defined by

$w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y}):=\frac{p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})}{p(\theta|\boldsymbol{x},\boldsymbol{y})}=\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}.$
(6)

We provide details in Appendix A. Hence, we can infer the posterior
$p(\theta|\boldsymbol{x},\boldsymbol{y})$ a single time, generate samples from
it, and estimate the predictive in (5) via Monte Carlo for each $x_{j}^{*}$
and each $\phi$.

More generally, we can use this approach to estimate the posterior expectation
of any statistics $f(\theta)$:

$\mathbb{E}_{\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y}}[f(\theta)]=\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}[f(\theta)w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})].$
(7)

If $f(\theta):=p(y_{j}^{*}|\theta,x_{j}^{*})$, we get the predictive
distribution in (5), while if
$f(\theta):=\mathbb{E}_{Y_{j}^{*}|\theta,x_{j}^{*}}[Y_{j}^{*}]$, we get the
predictive mean. Other statistics such as predictive variance and entropy, as
well as their decomposition into aleatoric and epistemic components, can be
derived analogously.

We remark once more that while it might appear unfamiliar that the posterior
$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ depends on the validation
input $x_{j}^{*}$, this dependence is due to the model dependencies
highlighted in blue in Figure 1, for which $x_{j}^{*}$ and $\theta$ are not
independent even when $\boldsymbol{x}$ and $\boldsymbol{y}$ are observed.
Similarly, the marginal $p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})$ also
depends on $x_{j}^{*}$, since $\boldsymbol{y}$ and $x_{j}^{*}$ are not
independent when $\theta$ is not observed. This phenomenon has a natural
analogy with the observer effect in physics [7], stating that the outcome of a
system can depend on the point of view of who is observing it, and be affected
by the act of observing itself. In our framework, the system is the
probabilistic model, and the point of view corresponds to the index $j$, for
which an input $x_{j}$ is observed.

####  4.1.1 Marginal ratio estimation and the Gaussian posterior scenario

The computation of the posterior weight in (6) involves the estimation of the
marginal ratio, i.e. the ratio between $p(\boldsymbol{y}|\boldsymbol{x})$ and
$p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})$. Each of these marginals can be
estimated via Monte Carlo integration:
$p(\boldsymbol{y}|\boldsymbol{x})=\mathbb{E}_{\theta|\boldsymbol{x}}[p(\boldsymbol{y}|\theta,\boldsymbol{x})]$
and
$p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})=\mathbb{E}_{\theta|x_{j}^{*},\boldsymbol{x}}[p(\boldsymbol{y}|\theta,\boldsymbol{x})]$.
One alternative, for which a derivation is provided in Appendix B, is to
observe that

$\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}=\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}\left[\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}\right]^{-1},$

which can be again approximated via Monte Carlo with samples from the
posterior $p(\theta|\boldsymbol{x},\boldsymbol{y})$. If we furthermore
approximate the posterior as a Gaussian distribution, which is often the case
in several popular Bayesian inference methods, e.g. [3, 27], then the marginal
can be estimated in closed-form. Indeed, given the Gaussian distance-aware
prior definition in (2), suppose that the posterior distribution
$p(\theta|\boldsymbol{x},\boldsymbol{y})$ is approximated as a diagonal
Gaussian with mean $\mu=[\mu_{1},\dots,\mu_{n_{\theta}}]$ and variance
$\sigma^{2}=[\sigma_{1}^{2},\dots,\sigma^{2}_{n_{\theta}}]$. In addition, let
us assume that $\max_{i}{\sigma_{i}^{2}}<\sigma_{0}^{2}$, meaning that the
data information contracts the prior variance over all directions of the
parameter space. Then we have

$\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}=\left(\frac{\sigma_{0}+d_{j}}{\sigma_{0}}\right)^{n_{\theta}}\prod_{i=1}^{n_{\theta}}\frac{\sigma_{i}}{\beta_{i,j}}\exp\left(\frac{1}{2}\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\left(1-\frac{\beta_{i,j}^{2}}{\sigma_{i}^{2}}\right)\right),$

with $d_{j}:=d([x_{j}^{*},\boldsymbol{x}])$,
$\beta_{i,j}^{-2}:=\tfrac{1}{\sigma_{i}^{2}}-\tfrac{1}{\gamma_{j}^{2}}$ and
$\gamma_{j}^{-2}:=\tfrac{1}{\sigma_{0}^{2}}-\tfrac{1}{(\sigma_{0}+d_{j}^{2})}$.
See Appendix (B).

###  4.2 Calibrating the distance as a post-processing step of training

We have introduced distance-aware priors in order to correct overconfidence of
predictions OOD. We do so by increasing the prior variance according to the
distance of the validation inputs from the training set. However, for the
total predictive uncertainty to be reasonable, the distance needs to be
calibrated. Since we use a parametric distance $d_{\phi}(\cdot)$, specifically
as in (3), we calibrate the distance by picking $\phi$ such that we reach the
minimum of a certain calibration loss.

There are many plausible choices for the calibration loss. For example, given
a validation data set, one could maximize a sum over $j$ of the test log-
marginal likelihood, i.e. $\sum_{j}\log p(y_{j}^{*}|x_{j}^{*})$, in order to
find the parameter $\phi$ for which the model best fits the validation data. A
similar choice may involve maximizing the summed log-predictive $\sum_{j}\log
p(y_{j}^{*}|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$, where the
marginalization is with respect to the posterior rather than the prior.
Alternatively, one could choose an empirical Bayes approach [30]. In this
paper we choose the following loss function, which appeared to work best
across a variety of experiments:

$\mathcal{L}(\phi)=\frac{1}{N^{*}}\sum_{j=1}^{N^{*}}\|\mathcal{U}_{\phi}(x_{j}^{*})-\gamma\|^{2}.$
(8)

The loss in (8) forces a certain measure of uncertainty
$\mathcal{U}_{\phi}(x_{j}^{*})$ to be as close as possible to a certain
$\gamma$ at a set of validation inputs $x_{j}^{*}$, for $j=1,\dots,N^{*}$. For
example, in a classification case, we may choose
$\mathcal{U}_{\phi}(x_{j}^{*})$ to be the predictive mean evaluated at some
OOD inputs $x_{j}^{*}$, and
$\gamma=\left[\tfrac{1}{m},\dots,\tfrac{1}{m}\right]\in\mathbb{R}^{m}$, where
$m$ is the number of classes. This loss steers the model towards a state of
total uncertainty at the given inputs, counteracting overconfidence OOD. In
regression, we may choose $\mathcal{U}_{\phi}(x_{j}^{*})$ to be the epistemic
variance evaluated at some OOD inputs $x_{j}^{*}$, and $\gamma$ to be some
value based on a quantile of the epistemic variances estimated at the training
inputs. This strategy drives the epistemic variance to be large OOD compared
to the variance in-domain, which again counteracts the overconfidence issue.

The OOD inputs $x_{j}^{*}$ might be available for a certain test case, in
which case they might as well be directly used. If these are not available, a
possible strategy is to generate them from an arbitrary distribution, with a
domain just outside of the training domain. With low-dimensional inputs, this
is convenient, as one may be able to visualize where the training domain is
located, and simulate OOD inputs accordingly. When this is less clear, a
possible drawback is that if the generated inputs are very far from the
training data, even after calibrating with (8) the predictive uncertainty
might still be too small in the nearest of the training domain. In order to
avoid this issue, in high-dimensions we prefer to take a set of validation
inputs on which we either misclassify, or we misregress above a certain error
threshold. Intuitively, inputs on which we predict badly are often the
furthest from the training domain, which we will confirm experimentally in
Section 5.

##  5 Experiments

In this section, we first demonstrate the effect of DAP calibration on low-
dimensional toy examples, in order to visualize its effect on OOD uncertainty
estimates. Then we test DAP calibration on UCI Gap [8], a set of benchmarks
designed to test the quality of predictive distributions away from the data.
Finally, we study the ability of our method to discriminate in-distribution
and OOD data, on MNIST, Fashion-MNIST and CIFAR10. We compare its performance
against several baselines.

###  5.1 Toy examples

We start with two low-dimensional toy examples. The first example is a
standard two-moons two-dimensional data set for classification, extensively
studied in several works [26, 40]. Details about the experimental setup are
given in Appendix C. We focus on the epistemic variance, as it best
characterizes uncertainty OOD. The left panel in Figure 2 shows the epistemic
component of the predictive variance produced via Automatic Differentiation
Variational Inference (ADVI) [37]. The right panel shows the same after DAP
calibration. As we move away from the data, the epistemic uncertainty
increase, counteracting the issue of overconfidence OOD.

Figure 2: Epistemic uncertainty estimations for two-moon dataset. The left and
right columns respectively show estimates produced via ADVI before and after
DAP calibration. We can see that DAP calibration helps counteracting ADVI’s
overconfidence OOD.

The second example that we consider is a one-dimensional sinusoidal regression
problem with inputs generated either from a Gaussian centered at -5, or from
one centered at 5. See Appendix C for setup details. Figure 3 compares
predictions and $95\%$ credibility interval estimated via a Laplace
approximation [6], before (left) and after (right) DAP calibration. Since we
focus on OOD uncertainty, the credibility intervals are produced using
epistemic variance only. We can see that DAP calibration reasonably improves
uncertainty OOD, remedying the overconfidence issue visible before
calibration.

Figure 3: Prediction and 95% credible interval estimation (via epistemic
variance only) using a Laplace approximation, before (left) and after (right)
DAP calibration. After DAP calibration, the uncertainty estimates grow as me
move away from the data.

###  5.2 UCI-Gap

Figure 4: Average test log-likelihood for five UCI "gap" datasets. Results are
averaged across different gap splits and displayed within one standard error.
The benefit of using DAP calibration is higher when VI is strongly
overconfident, like in naval.

We evaluated our method on five UCI "gap" datasets [8]. The UCI "gap"
benchmark has been specifically designed to test for in-between uncertainty,
as the test set is obtained by sampling the "middle" region for each input
feature component. In Figure 4 we report the average log-likelihood of our
method compared MAP estimation and VI. The average is taken among different
train-test split. Notice that in this setting the number of splits equals the
number of input features. As it is common in this regression task, we employ a
two-layer MLP as our base architecture, and consider its output as the mean of
a Gaussian distribution with homoscedastic variance. More details on the
experimental setup are provided in the Appendix C. On Boston, concrete, energy
and red wine, DAP did not improve much upon black-box VI, indeed it often
falls back to the very same solution. On the other hand, as already noticed in
[8], VI and MAP fail completely on naval due to the large differences between
training and test set. Here DAP significantly improves upon the VI solution,
correcting its overconfident behaviour.

###  5.3 MNIST, Fashion-MNIST and CIFAR-10

In this section we study the effect of DAP calibration over popular
classification datasets, such as MNIST [24], Fashion-MNIST [47] and CIFAR-10
[20]. We compare its performance on wildely used OOD classification benchmarks
and compare against: MAP estimation, variational inference (VI) [37], ODIN
[25], Deep Deterministic Uncertainty (DDU) [40] and Deep Ensembles (DE) [22].

We model target variables via a categorical distribution, with vector of
probabilities given by a softmax function evaluated at the outputs of a deep
learning model. We employ a simple MLP architecture for MNIST, a LeNet5[24]
for Fashion-MNIST and a a Wide Residual Network 28-10 [49] for CIFAR-10; more
details on the experimental setups are provided in the Appendix C. After
training the models to estimate the MAP, for each model we define the
projection $\mathcal{P}$ of an input as the forward pass evaluated at that
input up to the last hidden layer. In order to find the optimal calibration
parameter for DAP, we optimize the calibration loss in (8) on the
misclassified samples on a validation set, as already discussed in Section
4.2. In Appendix D, Figure 5 confirms the intuition that misclassified inputs
tend to be far from the training set, hence it is reasonable to use them for
DAP calibration. We highlight that in the following experiments our method is
applied on top of the VI solution, but other approximate Bayesian inference
methods can alternatively be used.

We measure the area under the receiving operator characteristic curve (AUROC)
and the area under the precision recall curve (AUCPR) [5] to quantify how well
we discriminate between in-distribution data OOD data sets. This procedure has
been employed in several previous works to evaluate the quality of uncertainty
estimates OOD [28, 26, 40, 15, 32]. To compute AUROC and AUCPR, we used the
predictive mean (confidence score) for DAP, while we rely on predictive
entropy for MAP, VI and DE. For DDU we used the Gaussian Mixture Model density
as described in [40].

MNIST and Fashion-MNIST. In Table 1 we summarize the results when MNIST is
used as the in-distribution dataset and Fashion-MNIST, SVHN and Omniglot are
used as out-of-distribution datasets. We observe that DAP performs
particularly well on Omniglot and Fashion-MNIST while no major improvement
upon VI is noticed for MNIST vs SVHN. We argue that the benefit of DAP
calibration is strongest when the posterior inference method is overconfident.
Indeed, this is the case for MNIST vs Fashion-MNIST, MNIST vs Omniglot and
also for Fashion-MNIST vs MNIST (Table 3) where the performance of VI is close
to the one of MAP, which is the weakest among the baselines. We observe that
DAP calibration performs either better or competitively with the other
methods. In particular, DAP and DDU, which are both distance-aware methods,
perform consistently among the best, and better than DE, which is usually
considered to be a strong baseline.

Table 1: Results on MNIST

Model |  Accuracy ($\uparrow$) | F-MNIST | SVHN | Omniglot
---|---|---|---|---
|  |  AUROC ($\uparrow$) |  AUCPR ($\uparrow$) |  AUROC ($\uparrow$) |  AUCPR ($\uparrow$) |  AUROC ($\uparrow$) |  AUCPR ($\uparrow$)
MAP | 96.40 | 87.65 | 87.77 | 92.38 | 95.26 | 89.58 | 90.65
ODIN | - | 89.50 | 86.42 | 97.10 | 94.85 | 90.15 | 87.49
DDU | - | 94.35 | 95.53 | 91.41 | 90.07 | 92.85 | 92.66
DE | 97.24 | 90.56 | 88.82 | 93.09 | 96.54 | 94.70 | 95.23
VI | 96.62 | 87.90 | 89.76 | 98.46 | 98.96 | 89.96 | 91.06
DAP | 96.57 | 96.25 | 96.44 | 98.85 | 97.99 | 95.24 | 95.14

Table 2: Results on Fashion-MNIST Model Accuracy ($\uparrow$) MNIST AUROC
($\uparrow$) AUCPR ($\uparrow$) MAP 89.39 67.65 67.49 ODIN - 71.22 64.79 DDU -
99.66 99.73 DE 91.34 91.69 91.92 VI 89.66 70.31 71.98 DAP 89.66 99.70 99.76
Table 3: Results on CIFAR-10 Model Accuracy ($\uparrow$) SVHN CIFAR-100 AUROC
($\uparrow$) AUCPR ($\uparrow$) AUROC ($\uparrow$) AUCPR ($\uparrow$) MAP
95.40 94.91 97.16 88.41 86.70 ODIN - 94.55 92.12 88.26 88.20 DDU - 97.04 94.91
90.60 91.13 DE 96.14 95.79 97.72 90.94 91.01 VI 95.24 96.73 98.33 90.70 88.68
DAP 95.24 95.24 94.23 90.95 91.87

Cifar 10. We train the model on CIFAR-10 and then consider a standard OOD task
using SVHN (far-OOD detection) and a more complex OOD task using CIFAR-100
(near-OOD detection). Table 3 shows that our model is competitive with all the
baselines, including DDU and DE, on both tasks. Our model is the top performer
on the harder task of far-OOD detection (CIFAR-100) while it falls shortly
behind other methods on CIFAR-10 vs SVHN. As observed earlier, in the latter
task the VI model, whose posterior samples DAP builds upon in this experiment,
is already achieving remarkable results, and it is possible that it does not
exist a calibration parameter $\phi$ that allows to improve upon the VI
solution. We remark that, in order to improve DAP calibration scores, we could
have calibrated individually for each of the OOD data sets. However, we
preferred to stay agnostic to the OOD data set and calibrate $\phi$ only once
over the misclassified validation data, as described above.

##  6 Conclusion and future directions

In this work we introduced a type of OOD uncertainty calibration based on
distance-aware priors (DAPs). These prior distributions assume that model
parameters depend on the inputs through a measure of their distance from the
training data. DAP calibration can be done as a post-processing step of
posterior inference, and it can be easily integrated with other popular
calibration techniques. We performed extensive evaluation on both
classification and regression tasks, demonstrating competitive performance
against several other methods that aim to estimate OOD uncertainty. We believe
DAP can be used as a practical option to decrease the issue of overconfidence
in out-of-distribution settings. We expect this approach to have broad impact,
since in virtually any real-world problem our test points are not drawn from
exactly the same distribution as our training points, and we must have
carefully calibrated uncertainty for decision making in these settings.

A natural extension of this work is to couple DAP calibration with other
methods for in-domain calibration, such as Platt scaling [35]. Furthermore, it
would be interesting to investigate the impact of feature-collapse [40, 26,
32, 1] when using neural networks as the projector in the distance computation
in DAP. Another important direction is to improve the importance sampling
approach by employing variance reduction strategies that can mitigate its
known instability issues [39].

## References

  * [1] Joost Amersfoort et al.  “On Feature Collapse and Deep Kernel Learning for Single Forward Pass Uncertainty”  In _arXiv preprint arXiv:2102.11409_ , 2021
  * [2] Christopher M Bishop  “Pattern recognition”  In _Machine learning_ 128.9, 2006
  * [3] David M Blei, Alp Kucukelbir and Jon D McAuliffe  “Variational inference: A review for statisticians”  In _Journal of the American statistical Association_ 112.518 Taylor & Francis, 2017, pp. 859–877
  * [4] Yushi Chen et al.  “Deep feature extraction and classification of hyperspectral images based on convolutional neural networks”  In _IEEE Transactions on Geoscience and Remote Sensing_ 54.10 IEEE, 2016, pp. 6232–6251
  * [5] Jesse Davis and Mark Goadrich  “The relationship between Precision-Recall and ROC curves”  In _Proceedings of the 23rd international conference on Machine learning_ , 2006, pp. 233–240
  * [6] Erik Daxberger et al.  “Laplace Redux-Effortless Bayesian Deep Learning”  In _Advances in Neural Information Processing Systems_ 34, 2021
  * [7] Paul Adrien Maurice Dirac  “The principles of quantum mechanics”  Oxford university press, 1981
  * [8] Andrew YK Foong, Yingzhen Li, José Miguel Hernández-Lobato and Richard E Turner  “’In-Between’Uncertainty in Bayesian Neural Networks”  In _arXiv preprint arXiv:1906.11537_ , 2019
  * [9] Yarin Gal and Zoubin Ghahramani  “Dropout as a bayesian approximation: Representing model uncertainty in deep learning”  In _international conference on machine learning_ , 2016, pp. 1050–1059  PMLR
  * [10] Ishaan Gulrajani et al.  “Improved training of wasserstein gans”  In _Advances in neural information processing systems_ 30, 2017
  * [11] Chuan Guo, Geoff Pleiss, Yu Sun and Kilian Q Weinberger  “On calibration of modern neural networks”  In _International Conference on Machine Learning_ , 2017, pp. 1321–1330  PMLR
  * [12] Trevor Hastie, Robert Tibshirani, Jerome H Friedman and Jerome H Friedman  “The elements of statistical learning: data mining, inference, and prediction”  Springer, 2009
  * [13] Eyke Hüllermeier and Willem Waegeman  “Aleatoric and epistemic uncertainty in machine learning: An introduction to concepts and methods”  In _Machine Learning_ 110.3 Springer, 2021, pp. 457–506
  * [14] Pavel Izmailov, Patrick Nicholson, Sanae Lotfi and Andrew G Wilson  “Dangers of Bayesian model averaging under covariate shift”  In _Advances in Neural Information Processing Systems_ 34, 2021
  * [15] Pavel Izmailov, Sharad Vikram, Matthew D Hoffman and Andrew Gordon Gordon Wilson  “What are Bayesian neural network posteriors really like?”  In _International Conference on Machine Learning_ , 2021, pp. 4629–4640  PMLR
  * [16] Sanyam Kapoor, Wesley J Maddox, Pavel Izmailov and Andrew Gordon Wilson  “On Uncertainty, Tempering, and Data Augmentation in Bayesian Classification”  In _arXiv preprint arXiv:2203.16481_ , 2022
  * [17] Alex Kendall and Yarin Gal  “What uncertainties do we need in bayesian deep learning for computer vision?”  In _Advances in neural information processing systems_ 30, 2017
  * [18] Diederik P Kingma and Jimmy Ba  “Adam: A Method for Stochastic Optimization”  In _ICLR (Poster)_ , 2015
  * [19] Diederik P Kingma and Max Welling  “Auto-encoding variational bayes”  In _arXiv preprint arXiv:1312.6114_ , 2013
  * [20] Alex Krizhevsky and Geoffrey Hinton  “Learning multiple layers of features from tiny images”  Citeseer, 2009
  * [21] Volodymyr Kuleshov, Nathan Fenner and Stefano Ermon  “Accurate uncertainties for deep learning using calibrated regression”  In _International Conference on Machine Learning_ , 2018, pp. 2796–2804  PMLR
  * [22] Balaji Lakshminarayanan, Alexander Pritzel and Charles Blundell  “Simple and Scalable Predictive Uncertainty Estimation using Deep Ensembles”  In _Advances in Neural Information Processing Systems_ 30, 2017
  * [23] Max-Heinrich Laves et al.  “Well-calibrated regression uncertainty in medical imaging with deep learning”  In _Medical Imaging with Deep Learning_ , 2020, pp. 393–412  PMLR
  * [24] Yann LeCun, Léon Bottou, Yoshua Bengio and Patrick Haffner  “Gradient-based learning applied to document recognition”  In _Proceedings of the IEEE_ 86.11 Ieee, 1998, pp. 2278–2324
  * [25] Shiyu Liang, Yixuan Li and R. Srikant  “Enhancing The Reliability of Out-of-distribution Image Detection in Neural Networks”  In _International Conference on Learning Representations_ , 2018  URL: https://openreview.net/forum?id=H1VGkIxRZ
  * [26] Jeremiah Liu et al.  “Simple and principled uncertainty estimation with deterministic deep learning via distance awareness”  In _Advances in Neural Information Processing Systems_ 33, 2020, pp. 7498–7512
  * [27] David JC MacKay  “Bayesian interpolation”  In _Neural computation_ 4.3 MIT Press One Rogers Street, Cambridge MA 02142-1209, USA journals-info …, 1992, pp. 415–447
  * [28] Wesley J Maddox et al.  “A simple baseline for bayesian uncertainty in deep learning”  In _Advances in Neural Information Processing Systems_ 32, 2019, pp. 13153–13164
  * [29] Andrey Malinin and Mark Gales  “Predictive uncertainty estimation via prior networks”  In _Advances in neural information processing systems_ 31, 2018
  * [30] Johannes S Maritz and T Lwin  “Empirical bayes methods”  ChapmanHall/CRC, 2018
  * [31] Takeru Miyato, Toshiki Kataoka, Masanori Koyama and Yuichi Yoshida  “Spectral normalization for generative adversarial networks”  In _International Conference on Learning Representations_ , 2018
  * [32] Jishnu Mukhoti et al.  “Deterministic Neural Networks with Appropriate Inductive Biases Capture Epistemic and Aleatoric Uncertainty”  In _arXiv preprint arXiv:2102.11582_ , 2021
  * [33] Radford M Neal  “MCMC using Hamiltonian dynamics”  In _Handbook of markov chain monte carlo_ 2.11, 2011, pp. 2
  * [34] Kazuki Osawa et al.  “Practical deep learning with Bayesian principles”  In _Advances in neural information processing systems_ 32, 2019
  * [35] John Platt  “Probabilistic outputs for support vector machines and comparisons to regularized likelihood methods”  In _Advances in large margin classifiers_ 10.3 Cambridge, MA, 1999, pp. 61–74
  * [36] Ali Rahimi and Benjamin Recht  “Random features for large-scale kernel machines”  In _Advances in neural information processing systems_ 20, 2007
  * [37] Danilo Rezende and Shakir Mohamed  “Variational inference with normalizing flows”  In _International conference on machine learning_ , 2015, pp. 1530–1538  PMLR
  * [38] R Tyrrell Rockafellar and Roger J-B Wets  “Variational analysis”  Springer Science & Business Media, 2009
  * [39] Richard S Sutton and Andrew G Barto  “Reinforcement learning: An introduction”  MIT press, 2018
  * [40] Joost Van Amersfoort, Lewis Smith, Yee Whye Teh and Yarin Gal  “Uncertainty estimation using a single deep deterministic neural network”  In _International conference on machine learning_ , 2020, pp. 9690–9700  PMLR
  * [41] Vladimir Vovk et al.  “Conformal calibrators”  In _Conformal and Probabilistic Prediction and Applications_ , 2020, pp. 84–99  PMLR
  * [42] Christopher K Williams and Carl Edward Rasmussen  “Gaussian processes for machine learning”  MIT press Cambridge, MA, 2006
  * [43] Andrew G Wilson and Pavel Izmailov  “Bayesian deep learning and a probabilistic perspective of generalization”  In _Advances in neural information processing systems_ 33, 2020, pp. 4697–4708
  * [44] Andrew G Wilson, Zhiting Hu, Russ R Salakhutdinov and Eric P Xing  “Stochastic variational deep kernel learning”  In _Advances in Neural Information Processing Systems_ 29, 2016
  * [45] Andrew Gordon Wilson, Zhiting Hu, Ruslan Salakhutdinov and Eric P Xing  “Deep kernel learning”  In _Artificial intelligence and statistics_ , 2016, pp. 370–378  PMLR
  * [46] Andrew Gordon Wilson, Zhiting Hu, Ruslan Salakhutdinov and Eric P. Xing  “Deep Kernel Learning”  In _Proceedings of the 19th International Conference on Artificial Intelligence and Statistics_ 51, Proceedings of Machine Learning Research  Cadiz, Spain: PMLR, 2016, pp. 370–378  URL: https://proceedings.mlr.press/v51/wilson16.html
  * [47] Han Xiao, Kashif Rasul and Roland Vollgraf  “Fashion-mnist: a novel image dataset for benchmarking machine learning algorithms”  In _arXiv preprint arXiv:1708.07747_ , 2017
  * [48] Jiayu Yao, Weiwei Pan, Soumya Ghosh and Finale Doshi-Velez  “Quality of uncertainty quantification for Bayesian neural network inference”  In _arXiv preprint arXiv:1906.09686_ , 2019
  * [49] Sergey Zagoruyko and Nikos Komodakis  “Wide Residual Networks”  In _BMVC_ , 2016
  * [50] Ruqi Zhang et al.  “Cyclical Stochastic Gradient MCMC for Bayesian Deep Learning”  In _International Conference on Learning Representations_ , 2020  URL: https://openreview.net/forum?id=rkeS1RVtPS

## Checklist

  1. 1.

For all authors…

    1. (a)

Do the main claims made in the abstract and introduction accurately reflect
the paper’s contributions and scope? [Yes]

    2. (b)

Did you describe the limitations of your work? [Yes] We discussed that the
scope of our work is limited to OOD uncertainty calibration.

    3. (c)

Did you discuss any potential negative societal impacts of your work? [No] We
do not see any.

    4. (d)

Have you read the ethics review guidelines and ensured that your paper
conforms to them? [Yes]

  2. 2.

If you are including theoretical results…

    1. (a)

Did you state the full set of assumptions of all theoretical results? [Yes]

    2. (b)

Did you include complete proofs of all theoretical results? [Yes]

  3. 3.

If you ran experiments…

    1. (a)

Did you include the code, data, and instructions needed to reproduce the main
experimental results (either in the supplemental material or as a URL)? [No]
We do not yet provide the code, but we will after the papers is accepted.

    2. (b)

Did you specify all the training details (e.g., data splits, hyperparameters,
how they were chosen)? [Yes]

    3. (c)

Did you report error bars (e.g., with respect to the random seed after running
experiments multiple times)? [No]

    4. (d)

Did you include the total amount of compute and the type of resources used
(e.g., type of GPUs, internal cluster, or cloud provider)? [No] Not relevant
for this paper, as we do not compare computational times.

  4. 4.

If you are using existing assets (e.g., code, data, models) or
curating/releasing new assets…

    1. (a)

If your work uses existing assets, did you cite the creators? [N/A]

    2. (b)

Did you mention the license of the assets? [N/A]

    3. (c)

Did you include any new assets either in the supplemental material or as a
URL? [N/A]

    4. (d)

Did you discuss whether and how consent was obtained from people whose data
you’re using/curating? [N/A]

    5. (e)

Did you discuss whether the data you are using/curating contains personally
identifiable information or offensive content? [N/A]

  5. 5.

If you used crowdsourcing or conducted research with human subjects…

    1. (a)

Did you include the full text of instructions given to participants and
screenshots, if applicable? [N/A]

    2. (b)

Did you describe any potential participant risks, with links to Institutional
Review Board (IRB) approvals, if applicable? [N/A]

    3. (c)

Did you include the estimated hourly wage paid to participants and the total
amount spent on participant compensation? [N/A]

##  Appendix A The posterior weight

The relation between the distance-aware prior distribution
$p(\theta|x_{j}^{*},\boldsymbol{x})$ and the posterior distribution
$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ in (1) is given by Bayes’
theorem:

$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})=\frac{p(\boldsymbol{y}|\theta,\boldsymbol{x})p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})},$

where $p(\boldsymbol{y}|\theta,\boldsymbol{x})$ is the training likelihood,
$p(\theta|x_{j}^{*},\boldsymbol{x})$ is the distance-aware prior and
$p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})$ is the marginal training
likelihood, i.e. the evidence. Then, by using Bayes’ theorem on both
$p(\theta|\boldsymbol{x},\boldsymbol{y})$ and
$p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$, we get

$\displaystyle w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$
$\displaystyle:=\frac{p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})}{p(\theta|\boldsymbol{x},\boldsymbol{y})}=\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}.$

##  Appendix B The marginal ratio in the Gaussian scenario

We first note that the expected value of
$w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})$ with respect to
$p(\theta|x_{j}^{*},\boldsymbol{x})$ is one:

$\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}[w(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})]=\int\frac{p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})}{p(\theta|\boldsymbol{x},\boldsymbol{y})}p(\theta|\boldsymbol{x},\boldsymbol{y})\,d\theta=\int
p(\theta|x_{j}^{*},\boldsymbol{x},\boldsymbol{y})\,d\theta=1.$

Given the expression of posterior weight (see Appendix A), this implies

$\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}=\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}\left[\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}\right]^{-1}.$

Secondly, let us assume

$\displaystyle p(\theta|x_{j}^{*},\boldsymbol{x})$
$\displaystyle:=N(\theta|0,(\sigma_{0}+d_{j})^{2}I_{n_{\theta}}),$
$\displaystyle p(\theta|\boldsymbol{x})$
$\displaystyle:=N(\theta|0,\sigma_{0}^{2}I_{n_{\theta}}),$ $\displaystyle
p(\theta|\boldsymbol{x},\boldsymbol{y})$
$\displaystyle:=N(\theta|\mu,\text{diag}(\sigma^{2})),$

where $d_{j}:=d([x_{j}^{*},\boldsymbol{x}])$, and $\text{diag}(\sigma^{2})$ is
a diagonal matrix with $\sigma^{2}\in\mathbb{R}_{+}^{n_{\theta}}$ on the
diagonal. We have

$\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}=\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\exp\left(-\frac{\|\theta\|^{2}}{2}\left(\frac{1}{(\sigma_{0}+d_{j})^{2}}-\frac{1}{\sigma_{0}^{2}}\right)\right)=\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\exp\left(\frac{\|\theta\|^{2}}{2\gamma_{j}^{2}}\right),$

with

$\frac{1}{\gamma_{j}^{2}}:=\frac{1}{\sigma_{0}^{2}}-\frac{1}{(\sigma_{0}+d_{j})^{2}}.$

Let us define
$\beta_{i,j}^{-2}:=\frac{1}{\sigma_{i}^{2}}-\frac{1}{\gamma_{j}^{2}}$, and
assume $\beta_{i,j}^{2}>0$. We will show later when this condition is true.
Then

$\displaystyle\mathbb{E}_{\theta|\boldsymbol{x},\boldsymbol{y}}\left[\frac{p(\theta|x_{j}^{*},\boldsymbol{x})}{p(\theta|\boldsymbol{x})}\right]=\frac{1}{(2\pi)^{n_{\theta}/2}}\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\prod_{i}\frac{1}{\sigma_{i}}\int\exp\left(-\frac{1}{2}\left(\frac{\theta_{i}^{2}-2\theta_{i}\mu_{i}}{\sigma_{i}^{2}}-\frac{\theta_{i}^{2}}{\gamma_{j}^{2}}+\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\right)\right)\,d\theta_{i}$
$\displaystyle=\frac{1}{(2\pi)^{n_{\theta}/2}}\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\prod_{i}\frac{1}{\sigma_{i}}\int\exp\left(-\frac{1}{2}\left(\left(\frac{1}{\sigma_{i}^{2}}-\frac{1}{\gamma_{j}^{2}}\right)\theta_{i}^{2}-2\theta_{i}\frac{\mu_{i}}{\sigma_{i}^{2}}+\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\right)\right)\,d\theta_{i}$
$\displaystyle=\frac{1}{(2\pi)^{n_{\theta}/2}}\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\prod_{i}\frac{1}{\sigma_{i}}\int\exp\left(-\frac{1}{2}\left(\beta_{i,j}^{-2}\left(\theta_{i}^{2}-2\theta_{i}\beta_{i,j}^{2}\frac{\mu_{i}}{\sigma_{i}^{2}}+\beta_{i,j}^{4}\frac{\mu_{i}^{2}}{\sigma_{i}^{4}}\right)-\beta_{i,j}^{2}\frac{\mu_{i}^{2}}{\sigma_{i}^{4}}+\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\right)\right)\,d\theta_{i}$
$\displaystyle=\frac{1}{(2\pi)^{n_{\theta}/2}}\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\prod_{i}\frac{1}{\sigma_{i}}\exp\left(-\frac{1}{2}\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\left(1-\frac{\beta_{i,j}^{2}}{\sigma_{i}^{2}}\right)\right)\int\exp\left(-\frac{1}{2\beta_{i,j}^{2}}\left(\theta_{i}-\beta_{i,j}^{2}\frac{\mu_{i}}{\sigma_{i}^{2}}\right)^{2}\right)\,d\theta_{i}$
$\displaystyle=\left(\frac{\sigma_{0}}{\sigma_{0}+d_{j}}\right)^{n_{\theta}}\prod_{i}\frac{\beta_{i,j}}{\sigma_{i}}\exp\left(-\frac{1}{2}\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\left(1-\frac{\beta_{i,j}^{2}}{\sigma_{i}^{2}}\right)\right).$

Because of the identity shown above, we have

$\frac{p(\boldsymbol{y}|\boldsymbol{x})}{p(\boldsymbol{y}|x_{j}^{*},\boldsymbol{x})}=\left(\frac{\sigma_{0}+d_{j}}{\sigma_{0}}\right)^{n_{\theta}}\prod_{i}\frac{\sigma_{i}}{\beta_{i,j}}\exp\left(\frac{1}{2}\frac{\mu_{i}^{2}}{\sigma_{i}^{2}}\left(1-\frac{\beta_{i,j}^{2}}{\sigma_{i}^{2}}\right)\right).$

Let us now define $\bar{\sigma}^{2}:=\max(\sigma_{i}^{2})$. We show that if
$\bar{\sigma}^{2}<\sigma_{0}^{2}$, then $\beta_{i,j}^{2}>0$ for all
$i=1,\dots,n_{\theta}$. Let us start by the thesis:

$\frac{1}{\sigma_{i}^{2}}-\frac{1}{\gamma_{j}^{2}}>0\quad\quad\forall
i=1,\dots,n_{\theta},$

which is verified if and only if

$\gamma_{j}^{2}>\max(\sigma_{i}^{2})=\bar{\sigma}^{2}.$

By definition of $\gamma_{j}^{2}$, this is true if and only if

$\sigma_{0}^{2}(\sigma_{0}+d_{j})^{2}>\bar{\sigma}^{2}(d_{j}^{2}+2d_{j}\sigma_{0}).$

By developing the products, we get

$\sigma_{0}^{4}+\sigma_{0}^{2}d_{j}^{2}+2\sigma_{0}^{3}d_{j}>\bar{\sigma}^{2}d_{j}^{2}+2\sigma_{0}\bar{\sigma}^{2}d_{j}.$

Finally, by factorization, we have

$(\sigma_{0}^{2}-\bar{\sigma}^{2})d_{j}^{2}+2\sigma_{0}(\sigma_{0}^{2}-\bar{\sigma}^{2})d_{j}+\sigma_{0}^{4}>0,$

which is clearly always positive given the assumption
$\bar{\sigma}^{2}<\sigma_{0}^{2}$.

##  Appendix C Experimental setups

##### Toy classification.

We generate 1000 samples with noise given by 0.07. We model inputs to logits
via a simple multilayer perceptron (MLP) and take a binomial distribution over
the targets, with probabilities given by the sigmoid function of the logits.
As a prior distribution over the model parameters, we take a standard
Gaussian. As validation inputs $x_{j}^{*}$, we used samples from Gaussian
distribution centered just outside the training domain, with standard
deviation given by $0.1$. We take the pre-distance in (4) and the calibration
loss in (8), with $\mathcal{U}_{\phi}(x_{j}^{*})$ being the predictive mean
and $\gamma=[0.5,0.5]$. Results are plotted for $\phi=-4.85$, that is a
minimum of the calibration loss.

##### Toy regression.

We model the target distribution via a Gaussian, where mean and standard
deviations are modelled via independent MLPs. We use a standard Gaussian over
all model parameters. Since only the parameters of the mean model directly
contribute to the epistemic uncertainty, we do DAP calibration only on them,
that is we define a DAP that increases the prior variance with a distance only
for the components of $\theta$ corresponding for the parameters of the mean
model. This helps better isolating the effect that we want to achieve, that is
increasing the epistemic uncertainty OOD without necessarily modifying the
aleatoric uncertainty. We employ the pre-distance in (4) and the calibration
loss in (8), where we take $\mathcal{U}_{\phi}(x_{j}^{*})$ to be the epistemic
variance, and $\gamma=10q_{95}$, with $q_{95}$ being the 95th of the predicted
epistemic variances at the training inputs. The validation inputs are
simulated from three Gaussians centered just outside of the training domain,
on the left, in between and on the right of the clusters.

##### UCI Gap.

We used a minibatch of size equal to 64, and optimize hyperparameters via grid
search on a validation set held out from the training set (90-10 split). We
highlight that this validation set is later used for DAP calibration and not
included in the final training of the model. For MAP and VI the
hyperparameters that we optimised were the learning rates [1e-2,1e-4, 1e-5]
and the number of epochs [20,40,100,200], while we kept the prior variance
fixed to 1. We used the Adam [18] optimizer. For VI we used an ensemble of 100
posterior samples at prediction time. The calibration parameter $\phi$ of DAP
was tuned independently for each split using a subsample of the validation set
used for hyperparameter tuning. This subset includes the samples used for
calibration (30% of the total), where the model has the higher RMSE. Like in
the toy regression example, we employ the pre-distance in (4) and the
calibration loss in (8), where we take $\mathcal{U}_{\phi}(x_{j}^{*})$ to be
the epistemic variance, and $\gamma=10q_{95}$, with $q_{95}$ being the 95th of
the predicted epistemic variances at the training inputs.

##### Classification experiments.

We employed a two-layer MLP with 30 hidden layers and tanh activations for the
MNIST experiments. We trained the model for 50 epochs with SGD with momentum
of 0.9, a learning rate of $10^{-6}$ and batch size of 128. For VI we used 50
posterior samples at inference time. The data are normalized between 0 and 1.
For SVHN and Omniglot, the images are turned to gray scale and resized to have
height and width of 28 before being normalized.

For Fashion-MNIST, we trained a LeNet5 for 80 epochs with SGD with momentum of
0.9, a learning rate of $10^{-6}$ and batch size of 512. For VI we used 50
posterior samples at inference time. The data are normalized between 0 and 1.

We employed a WideResNet with depth 28 and widen factor 10 for the CIFAR-10
experiments. The network was trained for 200 epochs without early stopping and
batch size of 128 decaying the learning rate by a factor of 0.2 every 60
epochs. The optimizer is SGD with momentum of 0.9 and an initial learning rate
of 0.1. We initialized the mean of VI with a pre-trained MAP network and then
used 20 posterior samples at evaluation time.

For ODIN and DDU we used MAP as the pretrained feature extractor. In DAP, we
also used the MAP model to transform the inputs before feeding them to the
distance function in order to ease the comparison with the other methods. For
ODIN we chose the temperature parameter $T$ among $[1,10,100,300,500,1000]$
and the noise parameter $\epsilon$ within $(0.001,0.5)$ with step size 0.017.
For deep ensembles, we used always 5 networks. We use an hold-out of 1000
samples from the in-distribution data for calibrating DAP. As described in
Section 5, among these samples only the misclassified ones are used in
practice in the calibration procedure.

##  Appendix D Distance plot on MNIST

Figure 5: Distances from the training set, for inputs correctly and wrongly
classified. The plots confirm the intuition that wrongly classified inputs
tend to be further from the training domain.

##  Appendix E Calibration losses for MNIST, Fashion-MNIST and CIFAR-10

Figure 6: Calibration losses over $\phi$ for MNIST, Fashion-MNIST and
CIFAR-10. The plots visualize where the losses achieve their minima.