/gpcam:multi-task-advancedfrom gpcam
Designs multi-output gpCAM experiments with GPOptimizer when a single measurement returns multiple correlated quantities (spectra, multi-channel detectors). Explains multi_task_mode choices (independent, cartesian, kronecker) and migration from legacy fvGPOptimizer.
Ships in gpcam. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Verified against gpCAM 8.7.0 / fvgp 4.11.1 — last checked 2026-10-28 (gpCAM a21385a).
Design experiments with vector-valued or function-valued outputs using gpCAM's multi-task GP.
Since gpCAM 8.7 there is one class. GPOptimizer is a multi-task GP whenever y_data
has more than one column. fvGPOptimizer is deprecated (FutureWarning);
GPOptimizer(..., multi_task_mode="cartesian") is exactly what it did. Never generate new
code with fvGPOptimizer; when you meet it in a user's script, it still runs.
multi_task_modey_data of shape (V, No) with No > 1 makes a multi-task GP; multi_task_mode decides how
the outputs relate. Name your choice and its tradeoff to the scientist:
| mode | when | kernel sees | noise | missing outputs |
|---|---|---|---|---|
"independent" (default) | outputs unrelated, but sharing one kernel and its hyperparameters; cheapest | inputs only, (N, D) | (V,), one per point | no |
"cartesian" | correlated outputs, any kernel; the classic fvGPOptimizer | [x, task] points, (N, D+1) | (V, No), NaN where y is NaN | yes (NaN) |
"kronecker" | every output measured at every point (spectra, images) and many outputs; learns the output covariance by default | inputs only; returns a list of factors | (V,) | no |
"independent" cannot exploit correlation between channels — if that is the reason for a
multi-task GP, use "cartesian" (or "kronecker" for full grids with many outputs).
"cartesian")In "cartesian" mode, a multi-task GP is a single-task GP over the Cartesian product of input space × output space. The output dimension is appended as an extra column to the input.
For example, with 2D input and 3 output channels, a point looks like:
[x0, x1, task_id] where task_id ∈ {0, 1, 2}
This means your kernel must handle D+1 dimensional inputs, where the last dimension is the task index.
The data are stored in that form too: gpo.x_data is (V·No, D+1), gpo.y_data (V·No, 1),
less the missing outputs, and get_data() returns them so.
import numpy as np
from gpcam import GPOptimizer
# 100 input points, 5 output channels
x_data = np.random.uniform(0, 1, (100, 2)) # 2D input
y_data = np.random.randn(100, 5) # 5 outputs per point
# Independent outputs, default kernel on the inputs:
gpo = GPOptimizer(x_data, y_data)
gpo.train()
# Kronecker: a learned 5 x 5 output covariance (coregionalization), defaults from the data:
gpo = GPOptimizer(x_data, y_data, multi_task_mode="kronecker")
gpo.train()
# For multi_task_mode="cartesian" with a custom task-correlation kernel, see
# "Multi-Task Kernel Design" below for the complete definition and bounds.
x_out argumentMulti-task prediction methods take x_out, the outputs you want, all by default: output
indices for "independent" and "kronecker", output coordinates (the task column's values)
for "cartesian" — the integers 0 … No-1 unless you built the task column yourself.
Results have the same shapes in every mode:
# Predict all 5 task outputs at a grid of input points:
mean = gpo.posterior_mean(x_grid)["m(x)"] # shape (N, 5)
std = np.sqrt(gpo.posterior_covariance(x_grid, variance_only=True)["v(x)"]) # (N, 5)
S = gpo.posterior_covariance(x_grid, x_out=np.array([0, 3]))["S"] # (N, N, 2, 2)
# S[i, j, t, u] = Cov(f(x_i, t), f(x_j, u)); "S_flat" is (2N, 2N), pair (i, t) at i + N*t
# Ask for the next best input point across all tasks:
gpo.ask(parameter_bounds, x_out=np.array([0, 1, 2, 3, 4]), n=1)
# Ask for a batch of 4 points, chosen together (greedily, any acquisition function):
gpo.ask(parameter_bounds, x_out=np.array([0, 1]), n=4,
acquisition_function="relative information entropy set", vectorized=True)
Plain "expected improvement" with x_out assumes the tasks are uncorrelated. The
multi-task branch of evaluate_gp_acquisition_function (surrogate_model.py:375-389, 415-427)
scalarizes to the task-summed objective and takes the spread as sqrt(Σ_t Var[f(x,t)]), which
drops the cross-task covariance. Its incumbent is the best task-summed posterior mean at the
data points (_incumbent_task_sum; before 8.7, a missing output made every EI value NaN, and on
gpCAM ≤ 8.4.2 it summed standard deviations instead of variances — fixed in #55). For correlated
tasks — the usual reason to fit a multi-task GP at all — prefer KG/NEI below.
Choose by what the scientist is doing:
"relative information entropy set" or "variance"
("total correlation" is not available for "kronecker"; ask(n > 1, batch_mode="joint")
uses "relative information entropy" there)"knowledge gradient" or "noisy expected improvement". Both act on
the same task-summed objective g(x) = Σ_t f(x, t) as multi-task
"expected improvement" / "ucb", but build the joint posterior with the exact
cross-task covariance via _scalarized_blocks. Both are maximization-only and are the
most expensive built-ins; see the caveats in the acquisition-functions skill.gpo.ask(parameter_bounds, x_out=np.array([0, 1]),
acquisition_function="knowledge gradient")
If the scientist wants to optimize one specific channel rather than the task sum,
that's a single-task problem on a derived scalar — extract it in the measurement
function and use GPOptimizer.
For simple black-box vector-valued optimization, optimize() replaces the manual loop.
The shape contract is asymmetric and is the easiest thing to get wrong.
optimize() calls func with a single point of shape (D,) during initial
sampling (via map), then with shape (1, D) inside the loop. For multi-task the
return shape must follow suit: a 1-D vector of length T for a single point, and
(n, T) for a batch. Returning (1, T) for the single-point case fails with
AssertionError: updated x and y do not have the same lengths.
def f(x):
"""
Single point (D,) -> returns (T,) values and (T,) noise
Batch (n, D) -> returns (n,T) values and (n,T) noise
"""
single = np.ndim(x) == 1
xa = np.atleast_2d(x)
y = np.column_stack([np.sin(xa[:, 0]), np.cos(xa[:, 0])]) # (n, T)
v = np.full(y.shape, 0.01)
return (y[0], v[0]) if single else (y, v)
result = GPOptimizer(multi_task_mode="cartesian").optimize(
func=f,
x_out=np.array([0, 1]), # which task indices to treat as outputs
search_space=np.array([[0., 1.]]),
max_iter=50,
)
If you would rather not reason about this, use the explicit ask/tell loop instead —
there you control every call and tell() takes a plain (n, D) / (n, T) pair.
The kernel receives inputs with the task index as the last column. You need to model both within-task and between-task correlations:
from gpcam.kernels import matern_kernel_diff1, get_anisotropic_distance_matrix
def multi_task_kernel(x1, x2, hps):
"""
ARD over the input dimensions, plus a task-correlation term.
x1, x2: shape (N, D+1), where the last column is the task index.
hps[0] : signal variance
hps[1:D+1] : input-space length scales (one per input dimension)
hps[D+1] : task correlation strength, in [0, 1]
Total: D + 2 hyperparameters.
"""
D = x1.shape[1] - 1 # strip the task column
# Spatial kernel — ARD over the input dimensions only
d_spatial = get_anisotropic_distance_matrix(x1[:, :D], x2[:, :D], hps[1:D+1])
k_spatial = matern_kernel_diff1(d_spatial, 1.0)
# Task kernel — same task = 1, different task = hps[D+1]
same_task = np.equal.outer(x1[:, -1], x2[:, -1]).astype(float)
k_task = same_task + hps[D+1] * (1.0 - same_task)
return hps[0] * k_spatial * k_task
The task-correlation hyperparameter must be bounded to [0, 1]. Values above 1 make
the task block non-PSD; negative values are representable but rarely what you want
and make the matrix indefinite for more than two tasks.
Matching bounds for the D = 2, 5-task example above:
D = 2
hp_bounds = np.array(
[[0.01, 100.0]] + # hps[0] signal variance
[[0.01, 10.0]] * D + # hps[1:3] length scales, one per input dim
[[0.0, 1.0]] # hps[3] task correlation <- index D+1
)
gpo = GPOptimizer(x_data, y_data,
init_hyperparameters=np.ones(D + 2),
kernel_function=multi_task_kernel,
multi_task_mode="cartesian")
gpo.train(hyperparameter_bounds=hp_bounds)
assert len(gpo.hyperparameters) == len(hp_bounds)
Note that hps[D+1] is the task index only because the kernel reads D length
scales. If you simplify to a single isotropic length scale, the task correlation moves
to hps[2] and the bounds array shrinks accordingly — keep the docstring, the code,
and the bounds in agreement, or you will train a hyperparameter that does nothing
while reading a length scale as a task correlation.
ask(n > 1) batches any acquisition function greedily, in every mode (a greedy pick conditions the GP for all outputs; in "cartesian" mode only for those in x_out). For exploring all channels use "relative information entropy set" / "relative information entropy" / "variance" / "total correlation" ("total correlation" is unavailable in "kronecker" mode). For optimization of the task-summed objective sum_t f(x, t), prefer "knowledge gradient" or "noisy expected improvement", which use the exact cross-task covariance (whereas multi-task "expected improvement" assumes independent tasks). Pass x_out=... to ask(), or write a custom callable if you care about a weighted combination or a single task."cartesian" only): y_data can have np.nan entries (e.g., task 1 wasn't measured at some x); the corresponding noise_variances entry must also be np.nan. The GP just ignores those entries — no imputation needed. "independent" and "kronecker" reject NaN."independent" uses the anisotropic Matérn on the inputs; "kronecker" the coregionalization kernel (a Matérn on the inputs times a learned output covariance), with data-driven defaults and bounds; "cartesian" treats the task index as one more input dimension, which is rarely what a scientist wants — write a kernel (below). If you supply a custom kernel_function, you become responsible for the full init_hyperparameters + hyperparameter_bounds layout.from gpcam.deep_kernel_network import Network gives you an MLP you parametrize from hps — see the kernel-designer skill for the pattern."cartesian"): the kernel sees D+1 columns, not D. A kernel written for [x, task] points under the default "independent" mode gets D columns and fails or silently misreads them — pass multi_task_mode="cartesian".noise_variances is (N, No) for "cartesian", (N,) for "independent" and "kronecker" (one variance per point, shared by the outputs)."cartesian" makes N data points × No outputs = N×No rows in the internal GP. Gets large fast; "kronecker" is the way out on full grids.y_data of shape (V, 1) is a single-task GP in every mode (fvGPOptimizer before 8.7 made it a one-task Cartesian GP).What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub lbl-camera/gpcam --plugin gpcam11 skills in this plugin
/acquisition-functionsUse when choosing or writing an acquisition function for gpCAM — selecting a built-in criterion for exploration, optimization, batch, or multi-task runs, or implementing a custom callable for UCB/LCB, minimization EI, constraints, radical gradient, or threshold finding./cost-functionsUse when modeling the real expense of moving between gpCAM measurement points — motor travel time, settling, directional costs, sample damage, beam time, or zone-based penalties./experiment-designerUse for end-to-end autonomous experiment design with gpCAM. Translates a scientist's description of their measurement into a complete, runnable gpCAM script — useful for replacing raster scans with adaptive sampling, peak-finding, or parameter optimization./gp2scale-advancedUse for large-scale gpCAM experiments (>10k points up to millions) using sparse compactly-supported kernels and Dask distributed computing for exact GP computation at scale./kernel-designerUse when designing or composing custom kernel (covariance) functions for gpCAM — smoothness, periodicity, symmetry, anisotropy, non-stationary amplitude/length-scale kernels (`non_stat_kernel`, Gibbs), non-Euclidean inputs, or neural-network-warped deep kernels (`Network`)./noise-functionsUse when modeling position-dependent, heteroscedastic, or otherwise structured noise in gpCAM — e.g., detector characteristics, count-rate-dependent variance, or non-uniform measurement uncertainty.Other plugins' skills for the same job