Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC006 · Model
Deep kernel learning with Gaussian processes
Use a neural network to transform inputs before a Gaussian-process kernel evaluates similarity. The GP supplies a probabilistic prediction layer while the network learns a task-specific geometry.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page.
- Gaussian process Verified in Python and R
Do it in CodeSplash Run it on your own data, in Python or R.
- Availability
- Runs in CodeSplash
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- Verified in Python and R through learn.gp. 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
Use a neural network to transform inputs before a Gaussian-process kernel evaluates similarity. The GP supplies a probabilistic prediction layer while the network learns a task-specific geometry. It differs from a deep Gaussian process, which composes stochastic function layers.
Question / hypotheses
Can a learned feature geometry improve predictive fit without losing useful uncertainty? Evaluate marginal/predictive likelihood and calibration, not just neural-feature accuracy.
Architecture / computational structure
A feature extractor maps x to a low-dimensional embedding, a positive-semidefinite base kernel compares embeddings, and a GP likelihood connects latent values to targets. Exact small Gaussian regression uses Cholesky factorization; scalable variants use inducing points, structured interpolation or variational approximations.
Assumptions
The transformed kernel must remain positive semidefinite and the likelihood must suit the observations. Standard exact formulas assume Gaussian observation noise. Learning a flexible embedding on limited data can distort uncertainty despite a valid GP covariance.
Formula / computational definition
. With , maximize . Latent predictive variance is ; add observation noise for an observed future response.
Parameters and notation
Learned: neural weights, kernel lengthscales/amplitudes, noise level and, in sparse variants, inducing locations/distributions. Hyperparameters: feature width, kernel family, inducing budget, optimizer schedules, priors and jitter. Jitter is numerical stabilization and should not be confused with measurement-noise variance.
Fitting, fine-tuning, and adaptation
Warm-start the feature extractor, train the GP head, then jointly optimize at controlled learning rates. On a new domain, a frozen feature map can be a baseline before unfreezing. Updating GP targets alone is conditional on a fixed representation; changing the encoder requires recomputing kernel matrices and recalibrating predictions.
How to use and calibrate
Standardize inputs and targets using training data. Validate proper predictive scores and interval coverage; compare a plain GP and a neural regression baseline. Inspect learned distances and posterior variance in gaps and beyond the input support. Use linear solves rather than explicitly inverting C.
Example application
Predict a measured response from high-dimensional sensor features while plotting uncertainty in a two-variable slice. Compare slices only within plausible sensor combinations.
Interpretation / pitfalls
Watch out
Feature collapse can make distant raw inputs appear close and falsely confident. GP-head uncertainty typically conditions on point-estimated network weights and kernel parameters; it does not integrate every uncertainty source. Claimed scalability depends on the chosen approximation.
Python and R code examples
Scope: Exact GP marginal likelihood and prediction with a fixed nonlinear feature map. This is the GP computation inside DKL; neural/kernel parameters are not optimized.
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.
Complete calculation — Exact GP marginal likelihood and prediction with a fixed nonlinear feature map. This is the GP computation inside DKL; neural/kernel parameters are not optimized.
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))
x = np.linspace(-1,1,6)[:,None]; y = np.sin(2*x[:,0])
feature = lambda a: np.tanh(np.column_stack([a[:,0],2*a[:,0]+.2]))
z = feature(x); C = rbf(z,z,.7)+.05*np.eye(len(x))
L = np.linalg.cholesky(C); alpha = np.linalg.solve(C,y)
nll = .5*y@alpha+np.log(np.diag(L)).sum()+len(y)/2*np.log(2*np.pi)
zstar = feature(np.array([[.25]])); k = rbf(z,zstar,.7)[:,0]
mean = k@alpha; latent_var = 1.-k@np.linalg.solve(C,k)
assert latent_var>=-1e-10 and np.isfinite(nll)
print({'nll':float(nll),'mean':float(mean),'latent_variance':float(latent_var)})
Complete calculation — Exact GP marginal likelihood and prediction with a fixed nonlinear feature map. This is the GP computation inside DKL; neural/kernel parameters are not optimized.
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))
}
x <- matrix(seq(-1,1,length.out=6),ncol=1); y <- sin(2*x[,1])
feature <- function(a) tanh(cbind(a[,1],2*a[,1]+.2))
z <- feature(x); C <- rbf(z,z,.7)+.05*diag(nrow(x))
U <- chol(C); alpha <- solve(C,y)
nll <- .5*sum(y*alpha)+sum(log(diag(U)))+length(y)/2*log(2*pi)
k <- as.vector(rbf(z,feature(matrix(.25,1,1)),.7))
m <- sum(k*alpha); latent_var <- 1-sum(k*solve(C,k))
stopifnot(latent_var>=-1e-10,is.finite(nll))
print(c(nll=nll,mean=m,latent_variance=latent_var))
Sources
- Wilson et al. deep kernel learningproceedings.mlr.press
From the CodeSplash reference, entry UNC006.