Skip to content

Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC016 · Model

Conditional neural process (CNP)

Predict a target function’s outputs after observing a variable-size set of context input/output pairs, without refitting weights for every new context set.

Needs a data type CodeSplash doesn't load yet Component demo, not a trained model CodeSplash's run and the reference's record

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

Predict a target function’s outputs after observing a variable-size set of context input/output pairs, without refitting weights for every new context set. A learned permutation-invariant encoder summarizes context and a decoder predicts target distributions. This is function-level amortization, not an unsupervised clustering method.

Question / hypotheses

What target responses are plausible given this context set and a learned distribution of related tasks? The prediction rule inherits its behavior from meta-training tasks; it is not automatically the exact posterior of a specified Gaussian process.

Architecture / computational structure

Encode every context pair with a shared network h, aggregate with a symmetric operation such as a mean, and decode each target input together with the aggregated representation. A typical output is a mean and positive standard deviation for each target, factorized conditionally on the deterministic context representation.

Assumptions

Meta-training functions must cover the structures and context patterns expected at evaluation. Context ordering should be irrelevant, while input coordinates and target identities retain meaning. A fixed-dimensional global summary can lose local details, and a factorized target likelihood limits coherent joint function sampling.

Formula / computational definition

For C={(xi,yi)}i=1nCC=\{(x_i,y_i)\}_{i=1}^{n_C}, rC=nC−1∑ihθ(xi,yi)r_C=n_C^{-1}\sum_i h_\theta(x_i,y_i) and pθ(yT∣xT,C)=∏j∈Tpθ(yj∣xj,rC)p_\theta(y_T\mid x_T,C)=\prod_{j\in T}p_\theta(y_j\mid x_j,r_C). Train by expected negative target log likelihood over tasks and sampled context/target splits. The empty-context case needs an explicit default representation.

Parameters and notation

Learned: pair encoder and target-decoder weights, including likelihood heads. Hyperparameters: representation dimension, aggregation rule, context/target sampling distributions, network widths, likelihood scale floor and meta-training optimization. Context size is an input condition, not a new set of neural weights.

Fitting, fine-tuning, and adaptation

Ordinary adaptation supplies additional context and performs a new forward pass with weights frozen. Gradient fine-tuning is optional and changes the protocol; if used, adapt with training tasks or permitted context labels and keep evaluation targets hidden. Fine-tuning only the decoder is a useful restricted baseline.

How to use and calibrate

Split at the function/task level to test transfer rather than reusing points from the same function across evaluation. Plot target log likelihood and error against context size, distance from nearest context, and task shift. Check permutation invariance numerically and distinguish interpolation from extrapolation.

Example application

Predict an irregularly observed response curve from a few measured context points. Show uncertainty bands as context points are added and report whether observation noise is learned or fixed.

Interpretation / pitfalls

Watch out

Independent target draws can form incoherent functions even when pointwise predictions look plausible. Mean aggregation can underfit dense local context. Predictive variance alone does not guarantee calibrated uncertainty or distribution-free coverage.

Python and R code examples

Scope: Tiny fixed-weight CNP forward computation, including a permutation-invariance check. It demonstrates a learned-model form with illustrative weights but does not meta-train it.

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 — Tiny fixed-weight CNP forward computation, including a permutation-invariance check. It demonstrates a learned-model form with illustrative weights but does not meta-train it.

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))
context=np.array([[-1.,-.7],[0.,.1],[1.,.8]])
W=np.array([[.7,-.2],[.4,.6]])
encode=lambda c: np.tanh(c@W).mean(axis=0)
r=encode(context); xt=np.array([-.5,.5])
mu=.8*xt+r[0]-.3*r[1]; sd=np.logaddexp(0,.2+np.abs(xt)*.1)+.01
assert np.allclose(r,encode(context[[2,0,1]])) and np.all(sd>0)
print({'context_representation':r.tolist(),'target_means':mu.tolist(),'target_sd':sd.tolist()})
R

Component demo, not a trained model — Tiny fixed-weight CNP forward computation, including a permutation-invariance check. It demonstrates a learned-model form with illustrative weights but does not meta-train it.

context <- matrix(c(-1,-.7,0,.1,1,.8),3,2,byrow=TRUE)
W <- matrix(c(.7,-.2,.4,.6),2,2,byrow=TRUE)
encode <- function(c) colMeans(tanh(c%*%W))
r <- encode(context); xt <- c(-.5,.5)
mu <- .8*xt+r[1]-.3*r[2]; sd <- log1p(exp(.2+abs(xt)*.1))+.01
stopifnot(max(abs(r-encode(context[c(3,1,2),])))<1e-12,all(sd>0))
print(list(context_representation=r,target_means=mu,target_sd=sd))

Sources

From the CodeSplash reference, entry UNC016.