betaFeedback

WorksComputer Science

When Has a Bayesian Neural Network Sampled Enough? Adaptive Inference Time with Statistical Guarantees

Abstract

Bayesian neural network predictions are commonly approximated using a fixed number of Monte Carlo samples per input, without controlling the resulting error that comes from this finite sample. We propose the use of confidence sequences to dynamically determine how many samples are needed while maintaining statistical guarantees. We consider several ways in which predictive probabilities are used, including identifying the most likely class, approximating the full predictive distribution, and resolving probability-threshold decisions. Sampling stops once the corresponding decision can be made with the desired guarantee. Experiments show that the method allocates the computational budget efficiently, assigning more samples to ambiguous inputs than to easy inputs while preserving reliable decisions and reducing overall latency relative to a fixed Monte Carlo budget.

Introduction

Bayesian neural networks (BNNs) provide a principled framework for representing predictive uncertainty, which is particularly valuable in high-stakes applications. They typically approximate the posterior predictive distribution p(y⋆∣x⋆,x1:n,y1:n)p(y^\star\mid x^\star, x_{1:n}, y_{1:n}) by averaging predictions over posterior parameter samples Eθ∼qλ[p(y⋆∣x⋆,θ)]\mathbb{E}_{\theta\sim q_\lambda}[p(y^\star\mid x^\star, \theta)] [3, 11, 6, 4, 9]. In practice, this average is usually estimated using the same fixed number of stochastic forward passes for every input. Although simple, this ad hoc choice provides no guarantee that the Monte Carlo estimate is sufficiently accurate for the prediction. It may also waste computation on easy inputs while using too few samples for difficult ones.

We instead ask an input- and task-specific question:

When have enough posterior samples been drawn to make the required decision?

The answer depends on how the prediction will be used. If only the most likely label is needed, it suffices to separate the leading class from its competitors. If the full probability vector is required, every coordinate must be estimated accurately enough. Meanwhile, for threshold-based decisions, it is enough to determine whether any class probability exceeds a specified level qq. Adapting the number of samples to these objectives would allow the method to concentrate computation on the inputs that need it most.

To make these adaptive decisions in a principled manner, we need uncertainty bounds that remain statistically valid as posterior samples accumulate. We therefore use the multivariate confidence sequence (CS) of Denoodt et al. [5] to track the predicted probability vector and stop once the required decision is certified. Unlike repeatedly checking an ordinary confidence interval, CSs remain valid under repeated inspection and data-dependent stopping [8]. This also distinguishes our approach from the approach of Bethell et al. [1], which stops when changes in the estimated variance remain below a preset threshold and provides neither statistical guarantees nor an indication of how accurate the final prediction is.

Overall, we (i) formulate anytime-valid stopping rules for various prediction use cases that control the Monte Carlo estimation error under repeated inspection and adaptive stopping, and (ii) show empirically that adaptive sampling uses fewer posterior draws for easy inputs while preserving reliable decisions, reducing overall inference time.

Anytime-valid adaptive prediction

Preliminaries. Fix an input xx and a classification problem with CC classes, indexed by c∈[C]:={1,…,C}c \in[C] := \{1, \ldots, C\}. A BNN represents uncertainty about its parameters through an approximate posterior qλ(θ)q_\lambda(\theta). Drawing θ∼qλ\theta\sim q_\lambda and evaluating the network gives a class-probability vector fθ(x)f_\theta(x), and averaging these vectors gives the posterior predictive class probabilities

μ:=Eθ∼qλ[fθ(x)]∈ΔC−1,(1)\mu:= \mathbb{E}_{\theta\sim q_\lambda}[f_\theta(x)] \in\Delta^{C-1}, \tag*{(1)}

where ΔC−1={m∈[0,1]C:∑c=1Cmc=1}\Delta^{C-1} = \{\mathbf{m} \in[0,1]^C : \sum_{c=1}^{C} m_c = 1\} is the probability simplex. Each independent draw θi∼qλ\theta_i \sim q_\lambda leads to a prediction yi=fθi(x)y_i = f_{\theta_i}(x), and after tt draws, the usual Monte Carlo estimate of μ\mu is

μ^t=t−1∑i=1tyi.\hat{\mu}_t = t^{-1}\sum_{i=1}^{t} y_i.

A CS for μ\mu is a sequence of data-dependent sets (Ct)t≥1(C_t)_{t \ge1}, where Ct=Ct(y1,…,yt)C_t = C_t(y_1, \ldots, y_t) is constructed from the first tt predictions, such that

P(∀t≥1:μ∈Ct)≥1−α,(2)\mathbb{P}(\forall t \ge1 : \mu\in C_t) \ge1 - \alpha, \tag*{(2)}

for a prescribed error level α\alpha [12]. Equation (2) immediately implies that, for any stopping time τ\tau determined from the observed samples y1,…,yτy_1, \ldots, y_\tau, P(μ∈Cτ)≥1−α\mathbb{P}(\mu\in C_\tau) \ge1 - \alpha.

Stopping rules for various prediction tasks. Suppose that the CS supplies coordinate-wise bounds [Lt,c,Ut,c][L_{t,c}, U_{t,c}] satisfying Ct⊆∏c=1C[Lt,c,Ut,c]C_t \subseteq\prod_{c=1}^{C}[L_{t,c}, U_{t,c}]. We propose three task-specific stopping rules based on these bounds: one for selecting a class, one for approximating the full predictive distribution, and one for making a threshold decision. Proofs of all three guarantees are deferred to Appendix § 6.

Class selection. If we want to return a single class while ensuring that it has the largest posterior predictive probability, let c^t=arg⁡max⁡c∈[C]μ^t,c\hat{c}_t = \arg\max_{c \in[C]} \hat{\mu}_{t,c} denote the current Monte Carlo prediction and let c∗=arg⁡max⁡c∈[C]μcc^* = \arg\max_{c \in[C]} \mu_c denote the leading class of μ\mu. We stop once the lower bound of c^t\hat{c}_t exceeds every competing upper bound:

τcls:=min⁡{t:Lt,c^t>max⁡c≠c^tUt,c}.(3)\tau_{\mathrm{cls}} := \min\{t : L_{t,\hat{c}_t} > \max_{c \ne\hat{c}_t} U_{t,c}\}. \tag*{(3)}

If the condition is not met after a predefined Tmax⁡T_{\max} samples, set τcls:=∞\tau_{\mathrm{cls}} := \infty and abstain from prediction.

Lemma 1. With probability at least 1−α1 - \alpha, whenever τcls<∞\tau_{\mathrm{cls}} < \infty, the returned class is the leading class: c^τcls=c∗\hat{c}_{\tau_{\mathrm{cls}}} = c^*.

Predictive-distribution accuracy. To approximate the complete predictive vector, the coordinate-wise error can be controlled instead. One can define

rt=max⁡cmax⁡{Ut,c−μ^t,c,μ^t,c−Lt,c},(4)r_t = \max_{c}\max\{U_{t,c} - \hat{\mu}_{t,c}, \hat{\mu}_{t,c} - L_{t,c}\}, \tag*{(4)}

such that stopping at

τdist:=min⁡{t:rt≤ε}(5)\tau_{\mathrm{dist}} := \min\{t : r_t \le\varepsilon\} \tag*{(5)}

Measuring the distance from μ^t,c\hat{\mu}_{t,c} to both Ut,cU_{t,c} and Lt,cL_{t,c} in Eq. (4), rather than using the interval width, also accounts for potentially asymmetric confidence intervals.

Lemma 2. With probability at least 1−α1 - \alpha, whenever τdist<∞\tau_{\mathrm{dist}} < \infty, the returned estimate satisfies ∥μ^τdist−μ∥∞≤ε\lVert\hat{\mu}_{\tau_{\mathrm{dist}}} - \mu\rVert_\infty\le\varepsilon.

Threshold decision. To determine whether any class has a mean probability of at least q∈[0,1]q \in[0,1], we can stop either when some lower bound reaches qq, indicating that the threshold is reached, or when all upper bounds fall below qq, establishing that it is not:

τq:=min⁡{t:max⁡cLt,c≥q or max⁡cUt,c<q}.(6)\tau_q := \min\{t : \max_c L_{t,c} \ge q \text{ or } \max_c U_{t,c} < q\}. \tag*{(6)}

Lemma 3. With probability at least 1−α1 - \alpha, whenever τq<∞\tau_q < \infty, the rule correctly determines whether max⁡cμc≥q\max_c \mu_c \ge q.

A simple but powerful multivariate CS. The stopping rules above can be applied with any multivariate CS that provides coordinate-wise bounds. In our experiments, we use the bounding-box CS of Denoodt et al. [5] because its regions are efficient to compute while producing empirically tight intervals that adapt to the variance of the observations.

At time tt, its region is

Ctbbox:={m∈[0,1]C∣mc∈[Lt,c,Ut,c], c=1,…,C},(7)\mathcal{C}^{\mathrm{bbox}}_t := \{\mathbf{m} \in[0,1]^C \mid m_c \in[L_{t,c},U_{t,c}],\ c=1,\ldots,C\}, \tag*{(7)}

where

[Lt,c,Ut,c]:={mc∈[0,1]∣ktc(mc)≤C(α−1−∑c′≠cwc′ktc′,⋆)}.(8)[L_{t,c},U_{t,c}] := \{m_c \in[0,1] \mid k_t^c(m_c) \le C(\alpha^{-1} - \sum_{c'\ne c} w_{c'}k_{t}^{c',\star})\}. \tag*{(8)}

Here, ktc(mc)k_t^c(m_c) denotes the scalar hedged-capital function of Waudby-Smith and Ramdas [13], evaluated at the candidate mean mcm_c using observations y1,c,…,yt,cy_{1,c},\ldots,y_{t,c}, and ktc,⋆:=min⁡u∈[0,1]ktc(u)k_t^{c,\star}:=\min_{u\in[0,1]}k_t^c(u). Because ktck_t^c is quasiconvex, its minimum and the interval endpoints can be computed with the conservative bisection procedure of Denoodt et al. [5].

We make two additional tightenings that are specific to our setting. First, we intersect the boxes over time, resulting in the endpoints

Lt,c:=max⁡1≤s≤tLs,c,Ut,c:=min⁡1≤s≤tUs,c.(9)L_{t,c}:=\max_{1\le s\le t}L_{s,c},\qquad U_{t,c}:=\min_{1\le s\le t}U_{s,c}. \tag*{(9)}

Second, since μ∈ΔC−1\boldsymbol{\mu}\in\Delta^{C-1}, we can use the probability-simplex constraint to further tighten each coordinate interval:

Lt,c:=max⁡{Lt,c,1−∑c′≠cUt,c′},Ut,c:=min⁡{Ut,c,1−∑c′≠cLt,c′}.(10)L_{t,c}:=\max\left\{L_{t,c},1-\sum_{c'\ne c}U_{t,c'}\right\},\qquad U_{t,c}:=\min\left\{U_{t,c},1-\sum_{c'\ne c}L_{t,c'}\right\}. \tag*{(10)}

These updates exploit both past regions and the simplex constraint while preserving coverage guarantees.

Experiments

We focus the evaluation on the class selection stopping rule, which corresponds to reporting the most likely class. We evaluate it in simulation and on CIFAR-100 with α=.05\alpha=.05. If the rule has not stopped by Tmax⁡T_{\max}, we return c^Tmax⁡\hat{c}_{T_{\max}} as unresolved, without a statistical guarantee.

Simulated three-class problem. For each of 500 three-class inputs, we model stochastic predictions as yt∣(μ,κ)∼Dirichlet⁡(κμ)\mathbf{y}_t\mid(\boldsymbol{\mu},\kappa)\sim\operatorname{Dirichlet}(\kappa\boldsymbol{\mu}). For 65% of the inputs, we use an “easy” setup with μ=(.75,.25r,.25(1−r))\boldsymbol{\mu}=(.75,.25r,.25(1-r)), where r∼Beta⁡(1,1)r\sim\operatorname{Beta}(1,1) and κ∼Unif⁡[80,300]\kappa\sim\operatorname{Unif}[80,300]. The remaining 35% simulate ambiguous predictions with μ=(.46,.44,.10)\boldsymbol{\mu}=(.46,.44,.10) and κ∼Unif⁡[30,120]\kappa\sim\operatorname{Unif}[30,120]. The true class c⋆c^\star is therefore quickly separated for easy inputs, whereas ambiguous inputs may require more samples to detect it. We set Tmax⁡=400T_{\max}=400.

Figure 1 illustrates the contrast between an easy input and a near tie. Across the simulation, the adaptive rule recovers c⋆c^\star on every input, while roughly one fifth reach the cap unresolved. Figure 2 shows that the adaptive rule spends the compute budget more wisely than a fixed budget per input.

Figure 2. Sample efficiency on simulated predictions. Adaptive stopping requires fewer samples per input to match accuracy.

Figure 1. Example of the class selection rule. (a) True means and samples for an easy and an ambiguous input. (b) The CS detects the easy input’s leading class at t=13t = 13. (c) The ambiguous input remains unresolved at Tmax⁡=400T_{\max} = 400.

CIFAR-100. We evaluate the rule on CIFAR-100 [10] using a public ResNet-18 [7] checkpoint with 79.26% test accuracy [2]. With Laplace-to-Gauss [3], we fit a diagonal generalized Gauss-Newton Laplace approximation over all network parameters:

q(θ)=N(θ^,(diag⁡(GGGN)+λI)−1).(11)q(\theta)=\mathcal{N}\left(\hat{\theta},(\operatorname{diag}(G_{\mathrm{GGN}})+\lambda I)^{-1}\right). \tag*{(11)}

We compute curvature from the 50,000 training images and select prior precision λ=3×105\lambda=3\times10^5 by predictive NLL on a disjoint 1,000-image calibration set. Each posterior sample requires a full-network forward pass. Since the exact predictive mean μ\boldsymbol{\mu} in Eq. (1) is unavailable, an independent 1,000-sample Bayesian model average (BMA) defines the reference class crefc^{\mathrm{ref}}. We evaluate 1,000 test images with Tmax⁡=200T_{\max}=200.

We inspect the stopping rule only at t∈{16,24,36,54,81,122,183,200}t \in\{16, 24, 36, 54, 81, 122, 183, 200\}, although every draw updates the CS. This reduces the computational overhead of constructing Cbbox\mathcal{C}_{\mathrm{bbox}}, which is otherwise costly relative to a single forward pass.

Figure 3 shows 99.8% agreement with the reference using 48.6 samples on average. The rule stops for 89.9% of inputs, and every resolved prediction agrees with the BMA. The vertical lines mark the Q1, Q2, and Q3 quartiles at 16, 24, and 183 samples, respectively, indicating that the majority of inputs require only a small number of samples. The smallest fixed budget reaching the same 99.8% agreement is T⋆=136T^\star= 136 samples per input.

Figure 3. Sample efficiency on CIFAR-100. Agreement with the reference BMA (top) and the allocated-sample distribution with Q1–Q3 quartiles (bottom).

Does sample efficiency translate into time efficiency? We benchmark mean latency of making predictions on an NVIDIA A100, including posterior sampling, forward passes, and CS computation. Table 1 shows that adaptive inference averages 0.253s per input, including 0.055s of CS overhead. It is nevertheless 3.21× faster than 200 posterior samples and 2.24× faster than the performance-matched T⋆=136T^\star= 136. Fixed 30 is faster but reaches only 98.4% agreement.

MethodInference (s)CS overhead (s)Total (s)Adaptive vs. rowPr⁡(c^=cref)\Pr(\hat{c} = c^{\mathrm{ref}})
Fixed 300.12500.1252.03× slower98.4%
Fixed 2000.81300.8133.21× faster99.8%
Fixed T⋆=136T^\star= 1360.56600.5662.24× faster99.8%
Adaptive (Tˉ=48.6\bar{T} = 48.6)0.1980.0550.253–99.8%

Table 1. Mean latency per CIFAR-100 input, measured over 1,000 inputs. Adaptive sampling uses Tˉ=48.6\bar{T} = 48.6 posterior samples on average and achieves the same 99.8% agreement as the smallest matching fixed Monte Carlo budget, T⋆=136T^\star= 136, while being 2.24× faster.

Conclusion

We introduce an anytime-valid approach for dynamically determining the number of Monte Carlo samples needed for each input in Bayesian neural network inference. Using confidence sequences, sampling stops once the prediction can be made with a statistical guarantee. This saves samples on easy inputs while using more computation for ambiguous ones, reducing overall inference time without sacrificing reliable decisions.

Acknowledgments and Disclosure of Funding

We thank Joaquin Vanschoren for their valuable input and discussions. This work was funded by the MOSAIC project under Grant Agreement No. 101194414.

Funded by the European Union. Views and opinions expressed are however those of the author(s) only and do not necessarily reflect those of the European Union or the European Commission. Neither the European Union nor the European Commission can be held responsible for them.

Proofs of the stopping-rule guarantees

Proof of Lemma 1.

Proof. Denote c^=c^τcls\widehat{c} = \widehat{c}_{\tau_{\mathrm{cls}}}. Then, on the simultaneous coverage event in Eq. (2), for every c≠c^c \ne\widehat{c}, the stopping condition gives

μc^≥Lτcls,c^>Uτcls,c≥μc.\mu_{\widehat{c}} \ge L_{\tau_{\mathrm{cls}},\widehat{c}} > U_{\tau_{\mathrm{cls}},c} \ge\mu_c.

Thus c^t\widehat{c}_t is the leading class. The coverage event has probability at least 1−α1-\alpha.

Proof of Lemma 2.

Proof. On the simultaneous coverage event, μc∈[Lt,c,Ut,c]\mu_c \in[L_{t,c}, U_{t,c}] for every tt and cc. Hence ∣μ^t,c−μc∣≤max⁡{Ut,c−μ^t,c,μ^t,c−Lt,c}≤rt\lvert\widehat{\mu}_{t,c}-\mu_c\rvert\le\max\{U_{t,c}-\widehat{\mu}_{t,c},\widehat{\mu}_{t,c}-L_{t,c}\} \le r_t. At t=τdistt=\tau_{\mathrm{dist}}, this is at most ε\varepsilon for every coordinate.

Proof of Lemma 3.

Proof. On the simultaneous coverage event, max⁡cLt,c≥q\max_c L_{t,c} \ge q implies that μc≥q\mu_c \ge q for at least one class, whereas max⁡cUt,c<q\max_c U_{t,c} < q implies that μc<q\mu_c < q for every class. Thus either stopping condition guarantees the corresponding decision.

References

  1. [1]Daniel Bethell, Simos Gerasimou, and Radu Calinescu. Robust uncertainty quantification using conformalized monte carlo prediction. In Proceedings of the AAAI Conference on Artificial Intelligence, volume 38, pages 20939–20948, 2024. doi: 10.1609/aaai.v38i19.30084.DOI
  2. [2]Eduardo Dadalto. edadalto/resnet18_cifar100 · Hugging Face — huggingface.co. https://huggingface.co/edadalto/resnet18_cifar100, 2024. Accessed 20 September 2026.
  3. [3]Erik Daxberger, Agustinus Kristiadi, Alexander Immer, Runa Eschenhagen, Matthias Bauer, and Philipp Hennig. Laplace redux—effortless bayesian deep learning. In Advances in Neural Information Processing Systems, volume 34, pages 20089–20103, 2021.arxiv.org/abs/2106.14806
  4. [4]Fabian Denoodt and José Oramas. Efficient post-hoc uncertainty calibration via variance-based smoothing. arXiv preprint arXiv:2503.15583, 2025.arxiv.org/abs/2503.15583
  5. [5]Fabian Denoodt, Sibylle Hess, Joaquin Vanschoren, and Christian A. Naesseth. On the tightness and computational tractability of higher-dimensional confidence sequences. arXiv preprint arXiv:2610.03727, 2026. URL https://arxiv.org/abs/2610.03727.
  6. [6]Yarin Gal and Zoubin Ghahramani. Dropout as a bayesian approximation: Representing model uncertainty in deep learning. In Maria-Florina Balcan and Kilian Q. Weinberger, editors, Proceedings of the 33nd International Conference on Machine Learning, ICML 2016, New York City, NY, USA, June 19-24, 2016, volume 48 of JMLR Workshop and Conference Proceedings, pages 1050–1059. JMLR.org, 2016. URL http://proceedings.mlr.press/v48/gal16.html.arxiv.org/abs/1506.02142
  7. [7]Kaiming He, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. Deep residual learning for image recognition. In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pages 770–778, 2016.arxiv.org/abs/1512.03385
  8. [8]Steven R. Howard, Aaditya Ramdas, Jon McAuliffe, and Jasmeet Sekhon. Time-uniform, nonparametric, nonasymptotic confidence sequences. The Annals of Statistics, 49(2):1055–1080, 2021. doi: 10.1214/20-AOS1991.DOI
  9. [9]Andreas Krause and Jonas Hübotter. Probabilistic artificial intelligence. arXiv preprint arXiv:2502.05244, 2025.arxiv.org/abs/2502.05244
  10. [10]Alex Krizhevsky. Learning multiple layers of features from tiny images. Technical report, University of Toronto, 2009.
  11. [11]Wesley J. Maddox, Pavel Izmailov, Timur Garipov, Dmitry P. Vetrov, and Andrew Gordon Wilson. A simple baseline for bayesian uncertainty in deep learning. In Hanna M. Wallach, Hugo Larochelle, Alina Beygelzimer, Florence d’Alché-Buc, Emily B. Fox, and Roman Garnett, editors, Advances in Neural Information Processing Systems 32: Annual Conference on Neural Information Processing Systems 2019, NeurIPS 2019, December 8-14, 2019, Vancouver, BC, Canada, pages 13132–13143, 2019. URL https://proceedings.neurips.cc/paper/2019/hash/118921efba23fcf329e6560b27861f0c2-Abstract.html.arxiv.org/abs/1902.02476
  12. [12]Jongha Jon Ryu and Gregory W. Wornell. Gambling-based confidence sequences for bounded random vectors. In Forty-first International Conference on Machine Learning, ICML 2024, Vienna, Austria, July 21-27, 2024. OpenReview.net, 2024. URL https://openreview.net/forum?id=mu7Er7f9NQ.arxiv.org/abs/2402.03683
  13. [13]Ian Waudby-Smith and Aaditya Ramdas. Estimating means of bounded random variables by betting. Journal of the Royal Statistical Society Series B: Statistical Methodology, 86(1):1–27, 2024. doi: 10.1093/jrsssb/qkad009.

Paper details

vlnc:W_dhqtth67
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 0

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents