Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC017 · Model
Latent neural process (NP)
Add a shared random latent variable to a context-conditioned neural predictor so sampled outputs can vary coherently across target locations.
In CodeSplash
Running this needs images, audio, video, graphs or another data type CodeSplash doesn't load yet.
- Availability
- Needs a data type CodeSplash doesn't load yet
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- 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
Add a shared random latent variable to a context-conditioned neural predictor so sampled outputs can vary coherently across target locations. The latent represents function-level uncertainty rather than independent per-target noise. This mechanism distinguishes an NP from the deterministic-summary CNP.
Question / hypotheses
What distribution over task-specific functions is supported by the observed context? Evaluate both pointwise prediction and joint sample behavior; function-level variation requires sharing the same sampled latent across the targets of a sampled function.
Architecture / computational structure
A permutation-invariant context encoder parameterizes q(z|C). During training, a target-informed encoder q(z|C∪T) provides a variational posterior, and a decoder maps each target input plus shared z to likelihood parameters. Some implementations include an additional deterministic context path; identify whether that path is present.
Assumptions
Tasks and context sampling at deployment should be reasonably represented during meta-training. The chosen latent dimension, variational Gaussian family and decoder likelihood restrict uncertainty. The training objective uses target labels only in the training posterior, never in test-time conditioning.
Formula / computational definition
A typical objective is . At prediction, sample and reuse that z for all queried targets in one function sample. Conditional observation noise can then be sampled separately.
Parameters and notation
Learned: context/posterior encoders, latent mean/scale heads, decoder and any deterministic path. Hyperparameters: latent size, hidden widths, context/target distribution, likelihood, sample count and KL weighting. A KL multiplier other than one changes the stated variational objective.
Fitting, fine-tuning, and adaptation
Context conditioning is the default fast adaptation mechanism. Fine-tuning global weights on target tasks is optional; freeze the encoder or adapt the decoder first if labeled target data are scarce. Re-evaluate uncertainty after tuning and never use held-out target responses to construct a test-time latent posterior.
How to use and calibrate
Assess target log likelihood, reconstruction error and coherent sample behavior on unseen tasks, using a validation design that varies context size. Inspect posterior collapse via KL and sensitivity of predictions to z. For multi-target scoring, distinguish joint marginal likelihood from a sum of individually marginalized log densities.
Example application
Generate plausible complete curves from a handful of context measurements, preserving uncertainty about global slope or shape. Plot several trajectories from shared latent samples rather than independent noise at each coordinate.
Interpretation / pitfalls
Watch out
A Gaussian latent bottleneck can blur multimodal task families. The context encoder is an amortized inference approximation, not exact Bayes. Target leakage into q(z|C) can make evaluation look unrealistically strong; separate training posterior construction from deployment inference.
Python and R code examples
Scope: One-sample variational objective component with a scalar latent and analytic Gaussian KL. The target-informed distribution is used only to demonstrate training; prediction would sample the context distribution instead.
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.
Component demo, not a trained model — One-sample variational objective component with a scalar latent and analytic Gaussian KL. The target-informed distribution is used only to demonstrate training; prediction would sample the context distribution instead.
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))
mu_c,sd_c,mu_t,sd_t = 0.,1.,.4,.6
z=mu_t+sd_t*.25; x=np.array([-1.,0.,1.]); y=np.array([-.5,.2,.9])
mean=.2+z*x; noise=.3
loglik=-.5*np.sum(np.log(2*np.pi*noise**2)+(y-mean)**2/noise**2)
kl=np.log(sd_c/sd_t)+(sd_t**2+(mu_t-mu_c)**2)/(2*sd_c**2)-.5
elbo=loglik-kl
assert kl>=0 and np.isfinite(elbo)
print({'shared_latent':z,'target_log_likelihood':float(loglik),'kl':float(kl),'one_sample_elbo':float(elbo)})
Component demo, not a trained model — One-sample variational objective component with a scalar latent and analytic Gaussian KL. The target-informed distribution is used only to demonstrate training; prediction would sample the context distribution instead.
mu_c <- 0; sd_c <- 1; mu_t <- .4; sd_t <- .6
z <- mu_t+sd_t*.25; x <- c(-1,0,1); y <- c(-.5,.2,.9)
m <- .2+z*x; noise <- .3
loglik <- -.5*sum(log(2*pi*noise^2)+(y-m)^2/noise^2)
kl <- log(sd_c/sd_t)+(sd_t^2+(mu_t-mu_c)^2)/(2*sd_c^2)-.5
elbo <- loglik-kl
stopifnot(kl>=0,is.finite(elbo))
print(c(shared_latent=z,target_log_likelihood=loglik,kl=kl,one_sample_elbo=elbo))
Sources
- Garnelo et al. latent neural processesarxiv.org
- Authors’ neural-process implementationsgithub.com
From the CodeSplash reference, entry UNC017.