Skip to content

Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC003 · Training method

Deep ensembles for predictive uncertainty

Combine independently trained probabilistic networks to improve prediction and quantify disagreement. The defining mechanism is an ensemble of fitted predictors and a mixture predictive distribution, not a single…

Runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through learn.deep_ensemble. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Combine independently trained probabilistic networks to improve prediction and quantify disagreement. The defining mechanism is an ensemble of fitted predictors and a mixture predictive distribution, not a single network’s softmax confidence. It provides a strong practical uncertainty baseline without claiming exact Bayesian posterior sampling.

Question / hypotheses

How much do plausible trained predictors disagree, and does their mixture improve held-out likelihood, reliability and robustness? These are empirical evaluation questions rather than automatic guarantees of uncertainty under every distribution shift.

Architecture / computational structure

Train M networks with independent initializations and minibatch order, normally using a proper predictive loss. A regression member may output a mean and positive observation variance. Average predictive distributions at inference; classification averages probabilities, and regression yields a Gaussian mixture rather than necessarily a Gaussian.

Assumptions

Members need meaningful diversity and representative labeled data. Their errors can remain highly correlated under a shared misspecification. Observation noise and between-member disagreement are useful operational summaries, but neither component is guaranteed to equal a unique physical or Bayesian uncertainty.

Formula / computational definition

p(y∣x)=M−1∑mpm(y∣x)p(y\mid x)=M^{-1}\sum_m p_m(y\mid x). For Gaussian members, μˉ=M−1∑mμm\bar\mu=M^{-1}\sum_m\mu_m and v=M−1∑mσm2+M−1∑m(μm−μˉ)2v=M^{-1}\sum_m\sigma_m^2+M^{-1}\sum_m(\mu_m-\bar\mu)^2. The mixture log score uses log-sum-exp of member log densities, not their arithmetic mean.

Parameters and notation

Learned: all member weights and per-input likelihood parameters. Hyperparameters: ensemble size M, network architecture, initialization and optimization scheme, diversity construction, noise floor and any post-hoc calibration. Bootstrapping is optional and is not the defining requirement of deep ensembles.

Fitting, fine-tuning, and adaptation

Fine-tune each member on the target domain with independent stochasticity and an appropriate source/target mixing policy. Identical fine-tuning trajectories can collapse diversity. If only heads are adapted, state that shared features limit epistemic variation; recalibrate on held-out target data after adaptation.

How to use and calibrate

Compare mixture NLL, Brier score or proper regression scores, coverage/interval width, and risk–coverage curves on both in-domain and predefined shifted sets. Increase M until prediction and disagreement stabilize within the available compute budget. Retain individual outputs for disagreement diagnostics.

Example application

Predict a continuous material property and flag compositions where members disagree. Plot member curves, mixture mean, observation-noise contribution, and between-member variance separately.

Interpretation / pitfalls

Watch out

A confident consensus can still be wrong far outside the training support. Treating mixture variance as a Gaussian interval can miss multimodality. Ensemble disagreement is a ranking signal requiring validation, not a distribution-free confidence statement.

Python and R code examples

Scope: Predictive aggregation for three hypothetical trained Gaussian members. The calculation is complete; it does not train the member networks.

Dependencies / validation: use the shared NumPy/SciPy or base-R setup above. Python executed successfully with assertions; R reviewed but not executed.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Predictive aggregation for three hypothetical trained Gaussian members. The calculation is complete; it does not train the member networks.

Shared setup for this chapter
import numpy as np
from scipy.special import logsumexp, gammaln
from scipy.linalg import expm
from itertools import product

def rbf(a, b, ell=1.0):
    a, b = np.atleast_2d(a), np.atleast_2d(b)
    return np.exp(-np.sum((a[:, None, :] - b[None, :, :])**2, axis=2)/(2*ell**2))
means = np.array([[.8,1.9],[1.0,2.4],[1.2,2.0]])
variances = np.array([[.1,.3],[.2,.2],[.1,.4]])
y = np.array([1.1,2.3]); mean = means.mean(axis=0)
aleatoric = variances.mean(axis=0)
disagreement = ((means-mean)**2).mean(axis=0)
logp = -.5*(np.log(2*np.pi*variances)+(y-means)**2/variances)
nll = -np.mean(logsumexp(logp,axis=0)-np.log(len(means)))
assert np.all(aleatoric+disagreement >= aleatoric)
print({'mean':mean.tolist(),'variance':(aleatoric+disagreement).tolist(),'mixture_nll':float(nll)})
R

Component demo, not a trained model — Predictive aggregation for three hypothetical trained Gaussian members. The calculation is complete; it does not train the member networks.

Shared setup for this chapter
lse <- function(z) { m <- max(z); m + log(sum(exp(z-m))) }
rbf <- function(a,b,ell=1) {
  a <- as.matrix(a); b <- as.matrix(b)
  D <- outer(rowSums(a*a),rowSums(b*b),"+")-2*a%*%t(b)
  exp(-pmax(D,0)/(2*ell^2))
}
mu <- matrix(c(.8,1.9,1,2.4,1.2,2),3,2,byrow=TRUE)
v <- matrix(c(.1,.3,.2,.2,.1,.4),3,2,byrow=TRUE)
y <- c(1.1,2.3); m <- colMeans(mu)
aleatoric <- colMeans(v)
disagreement <- colMeans(sweep(mu,2,m,"-")^2)
logp <- -.5*(log(2*pi*v)+sweep(mu,2,y,"-")^2/v)
nll <- -mean(apply(logp,2,lse)-log(nrow(mu)))
stopifnot(all(aleatoric+disagreement>=aleatoric))
print(list(mean=m,variance=aleatoric+disagreement,mixture_nll=nll))

Sources

From the CodeSplash reference, entry UNC003.