gpflow.kernels

Kernel s form a core component of GPflow models and allow prior information to be encoded about a latent function of interest. For an introduction to kernels, see Kernels in our Getting Started guide. The effect of choosing different kernels, and how it is possible to combine multiple kernels is shown in the “Using kernels in GPflow” notebook.

Broadcasting over leading dimensions: kernel.K(X1, X2) returns the kernel evaluated on every pair in X1 and X2. E.g. if X1 has shape [S1, N1, D] and X2 has shape [S2, N2, D], kernel.K(X1, X2) will return a tensor of shape [S1, N1, S2, N2]. Similarly, kernel.K(X1, X1) returns a tensor of shape [S1, N1, S1, N1]. In contrast, the return shape of kernel.K(X1) is [S1, N1, N1]. (Without leading dimensions, the behaviour of kernel.K(X, None) is identical to kernel.K(X, X).)

Modules

Classes

gpflow.kernels.ActivityCondition

class gpflow.kernels.ActivityCondition(requirements=<factory>)[source]

Bases: object

Conjunction of indicator-equality requirements gating one feature column.

Parameters:

requirements (Mapping[int, int]) – mapping from indicator column index (in the sliced coordinate system, i.e. the same space as HierarchyNode.feature_dims) to its required integer value. An empty mapping denotes an unconditional column. Keys and values must be non-negative int.

gpflow.kernels.AnisotropicStationary

class gpflow.kernels.AnisotropicStationary(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: Stationary

Base class for anisotropic stationary kernels, i.e. kernels that only depend on

d = x - x’

Derived classes should implement K_d(self, d): Returns the kernel evaluated on d, which is the pairwise difference matrix, scaled by the lengthscale parameter ℓ (i.e. [(X - X2ᵀ) / ℓ]). The last axis corresponds to the input dimension.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

scaled_difference_matrix(X, X2=None)[source]

Returns [(X - X2ᵀ) / ℓ]. If X has shape […, N, D] and X2 has shape […, M, D], the output will have shape […, N, M, D].

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

Return type:

Tensor

Returns:

  • return has shape [batch…, N, N, D] if X2 is None.

  • return has shape [batch…, N, batch2…, N2, D] if X2 is not None.

gpflow.kernels.ArcCosine

class gpflow.kernels.ArcCosine(order=0, variance=1.0, weight_variances=1.0, bias_variance=1.0, *, active_dims=None, name=None)[source]

Bases: Kernel

The Arc-cosine family of kernels which mimics the computation in neural networks. The order parameter specifies the assumed activation function. The Multi Layer Perceptron (MLP) kernel is closely related to the ArcCosine kernel of order 0.

The key reference is Cho and Saul [CS09].

Parameters:
  • order (int)

  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • weight_variances (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • bias_variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

  • name (Optional[str])

property ard: bool

Whether ARD behaviour is active.

gpflow.kernels.ArcHierarchical

class gpflow.kernels.ArcHierarchical(hierarchy, base_kernel=None, *, active_dims, name=None)[source]

Bases: HierarchicalEmbeddingKernel

The Arc kernel of Swersky et al. (2014).

Each conditional column c (normalised value v_c in [0, 1], activity mask m_c) is mapped into the plane via

\[\phi_c(v_c, m_c) = \big( r_c \sin(\pi a_c v_c)\, m_c,\; r_c \cos(\pi a_c v_c)\, m_c \big),\]

so that inactive points sit at the origin and active points sit on a circle whose phase depends on v_c. A stationary base kernel then evaluates covariance in the joint embedded space.

Parameters:
  • hierarchy (Sequence[HierarchyNode])

  • base_kernel (Optional[Stationary])

  • active_dims (Union[slice, Sequence[int]])

  • name (Optional[str])

gpflow.kernels.AxiomCheck

class gpflow.kernels.AxiomCheck(node_name, feature_dim, axiom, passed, max_violation, detail)[source]

Bases: object

Outcome of one axiom check on one conditional feature column.

Parameters:
  • node_name (str) – name of the HierarchyNode whose conditional behaviour was tested.

  • feature_dim (int) – column index (sliced coordinate system) of the conditional feature dimension that was tested.

  • axiom (int) – which axiom — 1, 2, or 3.

  • passed (bool) – True iff the predicate held within tolerance over every sampled input.

  • max_violation (float) – largest violation observed across samples (always >= 0). For axioms 1 and 2 this is |K_a - K_b|; for axiom 3 this is max(0, K_cross - K_self) — i.e. by how much an incomparable pair was at least as similar as a self pair.

  • detail (str) – one-line human-readable explanation.

gpflow.kernels.AxiomReport

class gpflow.kernels.AxiomReport(checks)[source]

Bases: object

Aggregate report from validate_hierarchical_axioms().

Use passed for the boolean summary, for_axiom() to slice by axiom number, and str(report) for a human-readable table.

Parameters:

checks (Tuple[AxiomCheck, ...])

for_axiom(n)[source]

Return only the checks for axiom n (1, 2, or 3).

Parameters:

n (int)

Return type:

Tuple[AxiomCheck, ...]

property passed: bool

True iff every axiom check passed (vacuously True when the hierarchy has no conditional nodes).

gpflow.kernels.Bias

gpflow.kernels.Bias

alias of Constant

gpflow.kernels.ChangePoints

class gpflow.kernels.ChangePoints(kernels, locations, steepness=1.0, name=None)[source]

Bases: Combination

The ChangePoints kernel defines a fixed number of change-points along a 1d input space where different kernels govern different parts of the space.

The kernel is by multiplication and addition of the base kernels with sigmoid functions (σ). A single change-point kernel is defined as:

K₁(x, x') * (1 - σ(x)) * (1 - σ(x')) + K₂(x, x') * σ(x) * σ(x')

where K₁ is deactivated around the change-point and K₂ is activated. The single change-point version can be found in Lloyd [Llo14]. Each sigmoid is a logistic function defined as:

σ(x) = 1 / (1 + exp{-s(x - x₀)})

parameterized by location “x₀” and steepness “s”.

The key reference is Lloyd [Llo14].

Parameters:
  • kernels (Sequence[Kernel])

  • locations (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • steepness (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • name (Optional[str])

gpflow.kernels.Combination

class gpflow.kernels.Combination(kernels, name=None)[source]

Bases: Kernel

Combine a list of kernels, e.g. by adding or multiplying (see inheriting classes).

Note that kernel composition can be done easily by using the + and * operators defined in Kernel .

The names of the kernels to be combined are generated from their class names.

Parameters:
  • kernels (Sequence[Kernel])

  • name (Optional[str])

property on_separate_dimensions: bool

Checks whether the kernels in the combination act on disjoint subsets of dimensions. Currently, it is hard to asses whether two slice objects will overlap, so this will always return False.

Returns:

Boolean indicator.

gpflow.kernels.Constant

class gpflow.kernels.Constant(variance=1.0, active_dims=None)[source]

Bases: Static

The Constant (aka Bias) kernel. Functions drawn from a GP with this kernel are constant, i.e. f(x) = c, with c ~ N(0, σ^2). The kernel equation is

k(x, y) = σ²

where: σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

gpflow.kernels.Convolutional

class gpflow.kernels.Convolutional(base_kernel, image_shape, patch_shape, weights=None, colour_channels=1)[source]

Bases: Kernel

Plain convolutional kernel as described in van der Wilk et al. [vdWRH17]. Defines a GP \(f()\) that is constructed from a sum of responses of individual patches in an image:

\[f(x) = \sum_p x^{[p]}\]

where \(x^{[p]}\) is the \(p\)’th patch in the image.

The key reference is van der Wilk et al. [vdWRH17].

Parameters:
  • base_kernel (Kernel)

  • image_shape (Sequence[int])

  • patch_shape (Sequence[int])

  • weights (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None])

  • colour_channels (int)

get_patches(X)[source]

Extracts patches from the images X. Patches are extracted separately for each of the colour channels.

Parameters:

X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

  • X has shape [batch…, N, D].

Images.

Return type:

Tensor

Returns:

  • return has shape [batch…, N, P, S].

Patches.

gpflow.kernels.Coregion

class gpflow.kernels.Coregion(output_dim, rank, *, active_dims=None, name=None)[source]

Bases: Kernel

A Coregionalization kernel. The inputs to this kernel are _integers_ (we cast them from floats as needed) which usually specify the outputs of a Coregionalization model.

The kernel function is an indexing of a positive-definite matrix:

K(x, y) = B[x, y] .

To ensure that B is positive-definite, it is specified by the two parameters of this kernel, W and kappa:

B = W Wᵀ + diag(kappa) .

We refer to the size of B as “output_dim x output_dim”, since this is the number of outputs in a coregionalization model. We refer to the number of columns on W as ‘rank’: it is the number of degrees of correlation between the outputs.

NB. There is a symmetry between the elements of W, which creates a local minimum at W=0. To avoid this, it is recommended to initialize the optimization (or MCMC chain) using a random W.

Parameters:
  • output_dim (int)

  • rank (int)

  • active_dims (Union[slice, Sequence[int], None])

  • name (Optional[str])

gpflow.kernels.Cosine

class gpflow.kernels.Cosine(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: AnisotropicStationary

The Cosine kernel. Functions drawn from a GP with this kernel are sinusoids (with a random phase). The kernel equation is

k(r) = σ² cos{2πd}

where: d is the sum of the per-dimension differences between the input points, scaled by the lengthscale parameter ℓ (i.e. Σᵢ [(X - X2ᵀ) / ℓ]ᵢ), σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.Exponential

class gpflow.kernels.Exponential(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: IsotropicStationary

The Exponential kernel. It is equivalent to a Matern12 kernel with doubled lengthscales.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.HierarchicalEmbeddingKernel

class gpflow.kernels.HierarchicalEmbeddingKernel(hierarchy, base_kernel=None, *, active_dims, name=None)[source]

Bases: Kernel

Abstract base for kernels that embed conditional features into a stationary base kernel’s input space, gated by indicator-derived activity masks.

Concrete subclasses implement two methods:

  • _conditional_parameters(), which reads the constrained parameters the embedding needs and returns them as a dict. K() calls it once and shares the result between X and X2, so each parameter transform runs only once.

  • _embed_conditional(), which receives the normalised conditional features v_c, their float activity mask m_c (both [batch..., N, D_cond]) and the dict from _conditional_parameters(), and returns [batch..., N, 2 * D_cond].

Parameters:
  • hierarchy (Sequence[HierarchyNode]) – sequence of HierarchyNode. Each node binds a group of feature columns to an ActivityCondition that gates them. Feature columns must be globally unique across the hierarchy and node names must be unique. The set of indicator columns is derived from the union of all ActivityCondition keys appearing in the hierarchy; no separate indicator_dims argument is required.

  • base_kernel (Optional[Stationary]) – stationary base kernel applied in the joint embedded space. Defaults to Matern52 if omitted. The supplied kernel is deep-copied via gpflow.utilities.deepcopy() before use, so the caller’s object is never mutated. Its lengthscales must be scalar or have shape (2 * n_cond + n_uncond,) matching the embedded dimension; on the copy they are then forced to 1 and set non-trainable. The conditional dimensions carry their scale via the per-conditional-column parameters of the concrete subclass (e.g. angle / radius for ArcHierarchical or theta1 / theta2 for WedgeHierarchical). The unconditional dimensions’ scale is carried by the learnable uncond_lengthscales Parameter (shape (n_uncond,), positive transform). When base_kernel.lengthscales is supplied with shape (2 * n_cond + n_uncond,) the first n_uncond entries (which match the [uncond | cond] layout produced by _embed) are used as the initial values for uncond_lengthscales; otherwise it is initialised to ones. When n_uncond == 0 the attribute is None.

  • active_dims (Union[slice, Sequence[int]]) – required. feature_dims and ActivityCondition keys are interpreted in the sliced coordinate system. When __call__() receives inputs, the slice defined by active_dims is applied first, and the resulting coordinates are then processed according to the hierarchy. Must select exactly n_feat + n_ind columns, where n_feat is the total number of feature columns in the hierarchy and n_ind is the number of derived indicator columns. May be a sequence of int column indices, or a slice whose stop is concrete (so its width can be validated at construction).

  • name (Optional[str]) – optional kernel name.

K(X, X2=None)[source]

Evaluate the kernel function on inputs X and X2. Assumes that X and X2 are already sliced according to active_dims, if applicable. The embedding and base kernel evaluation are performed in the joint embedded space defined by the hierarchy and indicators.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

Return type:

Tensor

Returns:

  • return has shape [batch…, N, N] if X2 is None.

  • return has shape [batch…, N, batch2…, N2] if X2 is not None.

K_diag(X)[source]

Evaluate the kernel function on the diagonal of the covariance matrix for input X. Assumes that X is already sliced according to active_dims, if applicable.

Parameters:

X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

  • X has shape [batch…, N, D].

Return type:

Tensor

Returns:

  • return has shape [batch…, N].

property hierarchy: Sequence[HierarchyNode]

The hierarchy defining this kernel’s structure.

property indicator_dims: Tuple[int, ...]

Column indices (sliced coordinate system) of the indicators referenced by any ActivityCondition in the hierarchy, sorted ascending.

property n_cond_dims: int

Get number of conditional feature dimensions.

property n_uncond_dims: int

Get number of unconditional feature dimensions.

gpflow.kernels.HierarchyNode

class gpflow.kernels.HierarchyNode(name, feature_dims, feature_bounds, activity_condition=<factory>)[source]

Bases: object

One node of a hierarchical kernel’s search space.

A node groups one or more feature columns that share the same activation condition. Every feature owned by the node is gated by activity_condition; the empty default condition makes the node’s features unconditional.

Parameters:
  • name (str) – human-readable name of the node; used in error messages and for debugging. Must be unique within a hierarchy.

  • feature_dims (Sequence[int]) – column indices of the (real-valued) features owned by this node. Must be non-empty and contain no duplicates.

  • feature_bounds (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) – tensor of shape [len(feature_dims), 2] giving (lower, upper) per feature, in the same order as feature_dims. Used for normalisation to [0, 1].

  • activity_condition (ActivityCondition) – ActivityCondition shared by every feature this node owns. Defaults to an empty (unconditional) condition.

gpflow.kernels.IndependentLatent

class gpflow.kernels.IndependentLatent(active_dims=None, name=None)[source]

Bases: MultioutputKernel

Base class for multioutput kernels that are constructed from independent latent Gaussian processes.

It should always be possible to specify inducing variables for such kernels that give a block-diagonal Kuu, which can be represented as a [L, M, M] tensor. A reasonable (but not optimal) inference procedure can be specified by placing the inducing points in the latent processes and simply computing Kuu [L, M, M] and Kuf [N, P, M, L] and using fallback_independent_latent_ conditional(). This can be specified by using Fallback{Separate|Shared} IndependentInducingVariables.

Parameters:
  • active_dims (Union[slice, Sequence[int], None])

  • name (Optional[str])

gpflow.kernels.IsotropicStationary

class gpflow.kernels.IsotropicStationary(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: Stationary

Base class for isotropic stationary kernels, i.e. kernels that only depend on

r = ‖x - x’‖

Derived classes should implement one of:

K_r2(self, r2): Returns the kernel evaluated on r² (r2), which is the squared scaled Euclidean distance Should operate element-wise on r2.

K_r(self, r): Returns the kernel evaluated on r, which is the scaled Euclidean distance. Should operate element-wise on r.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

scaled_squared_euclid_dist(X, X2=None)[source]

Returns ‖(X - X2ᵀ) / ℓ‖², i.e. the squared L₂-norm.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

Return type:

Tensor

Returns:

  • return has shape [batch…, N, N] if X2 is None.

  • return has shape [batch…, N, batch2…, N2] if X2 is not None.

gpflow.kernels.Kernel

class gpflow.kernels.Kernel(active_dims=None, name=None)[source]

Bases: Module

The basic kernel class. Management of active dimensions is implemented here.

Parameters:
  • active_dims (Union[slice, Sequence[int], None]) – active dimensions, either a slice or list of indices into the columns of X.

  • name (Optional[str]) – optional kernel name.

on_separate_dims(other)[source]

Checks if the dimensions, over which the kernels are specified, overlap. Returns True if they are defined on different/separate dimensions and False otherwise.

Parameters:

other (Kernel)

Return type:

bool

slice(X, X2=None)[source]

Slice the correct dimensions for use in the kernel, as indicated by self.active_dims.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    Input 1.

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

    Input 2, can be None.

Return type:

Tuple[Tensor, Optional[Tensor]]

Returns:

  • return[0] has shape [batch…, N, I].

  • return[1] has shape [batch2…, N2, I].

Sliced X, X2.

slice_cov(cov)[source]

Slice the correct dimensions for use in the kernel, as indicated by self.active_dims for covariance matrices. This requires slicing the rows and columns. This will also turn flattened diagonal matrices into a tensor of full diagonal matrices.

Parameters:

cov (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

  • cov has shape [N, D_or_DD…].

Tensor of covariance matrices.

Return type:

Tensor

Returns:

  • return has shape [N, I, I].

Sliced covariance matrices.

gpflow.kernels.Linear

class gpflow.kernels.Linear(variance=1.0, active_dims=None)[source]

Bases: Kernel

The linear kernel. Functions drawn from a GP with this kernel are linear, i.e. f(x) = cx. The kernel equation is

k(x, y) = σ²xy

where σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

property ard: bool

Whether ARD behaviour is active.

gpflow.kernels.LinearCoregionalization

class gpflow.kernels.LinearCoregionalization(kernels, W, name=None)[source]

Bases: IndependentLatent, Combination

Linear mixing of the latent GPs to form the output.

Parameters:
  • kernels (Sequence[Kernel])

  • W (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • name (Optional[str])

K(X, X2=None, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X2), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [P, batch…, N, N] if (not full_output_cov) and (X2 is None).

  • return has shape [P, batch…, N, batch2…, N2] if (not full_output_cov) and (X2 is not None).

  • return has shape [batch…, N, P, N, P] if full_output_cov and (X2 is None).

  • return has shape [batch…, N, P, batch2…, N2, P] if full_output_cov and (X2 is not None).

cov[f(X), f(X2)]

K_diag(X, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [batch…, N, P, P] if full_output_cov.

  • return has shape [batch…, N, P] if not full_output_cov.

var[f(X)]

property latent_kernels: Tuple[Kernel, ...]

The underlying kernels in the multioutput kernel

property num_latent_gps: int

The number of latent GPs in the multioutput kernel

gpflow.kernels.Matern12

class gpflow.kernels.Matern12(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: IsotropicStationary

The Matern 1/2 kernel. Functions drawn from a GP with this kernel are not differentiable anywhere. The kernel equation is

k(r) = σ² exp{-r}

where: r is the Euclidean distance between the input points, scaled by the lengthscales parameter ℓ. σ² is the variance parameter

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.Matern32

class gpflow.kernels.Matern32(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: IsotropicStationary

The Matern 3/2 kernel. Functions drawn from a GP with this kernel are once differentiable. The kernel equation is

k(r) = σ² (1 + √3r) exp{-√3 r}

where: r is the Euclidean distance between the input points, scaled by the lengthscales parameter ℓ, σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.Matern52

class gpflow.kernels.Matern52(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: IsotropicStationary

The Matern 5/2 kernel. Functions drawn from a GP with this kernel are twice differentiable. The kernel equation is

k(r) = σ² (1 + √5r + 5/3r²) exp{-√5 r}

where: r is the Euclidean distance between the input points, scaled by the lengthscales parameter ℓ, σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.MultioutputKernel

class gpflow.kernels.MultioutputKernel(active_dims=None, name=None)[source]

Bases: Kernel

Multi Output Kernel class.

This kernel can represent correlation between outputs of different datapoints.

The full_output_cov argument holds whether the kernel should calculate the covariance between the outputs. In case there is no correlation but full_output_cov is set to True the covariance matrix will be filled with zeros until the appropriate size is reached.

Parameters:
  • active_dims (Union[slice, Sequence[int], None])

  • name (Optional[str])

abstract K(X, X2=None, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X2), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [P, batch…, N, N] if (not full_output_cov) and (X2 is None).

  • return has shape [P, batch…, N, batch2…, N2] if (not full_output_cov) and (X2 is not None).

  • return has shape [batch…, N, P, N, P] if full_output_cov and (X2 is None).

  • return has shape [batch…, N, P, batch2…, N2, P] if full_output_cov and (X2 is not None).

cov[f(X), f(X2)]

abstract K_diag(X, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [batch…, N, P, P] if full_output_cov.

  • return has shape [batch…, N, P] if not full_output_cov.

var[f(X)]

abstract property latent_kernels: Tuple[Kernel, ...]

The underlying kernels in the multioutput kernel

abstract property num_latent_gps: int

The number of latent GPs in the multioutput kernel

gpflow.kernels.Periodic

class gpflow.kernels.Periodic(base_kernel, period=1.0)[source]

Bases: Kernel

The periodic family of kernels. Can be used to wrap any Stationary kernel to transform it into a periodic version. The canonical form (based on the SquaredExponential kernel) can be found in Equation (47) of

D.J.C.MacKay. Introduction to Gaussian processes. In C.M.Bishop, editor, Neural Networks and Machine Learning, pages 133–165. Springer, 1998.

The derivation can be achieved by mapping the original inputs through the transformation u = (cos(x), sin(x)).

For the SquaredExponential base kernel, the result can be expressed as:

k(r) = σ² exp{ -0.5 sin²(π r / γ) / ℓ²}

where: r is the Euclidean distance between the input points ℓ is the lengthscales parameter, σ² is the variance parameter, γ is the period parameter.

NOTE: usually we have a factor of 4 instead of 0.5 in front but this

is absorbed into the lengthscales hyperparameter.

NOTE: periodic kernel uses active_dims of a base kernel, therefore

the constructor doesn’t have it as an argument.

Parameters:

gpflow.kernels.Polynomial

class gpflow.kernels.Polynomial(degree=3.0, variance=1.0, offset=1.0, active_dims=None)[source]

Bases: Linear

The Polynomial kernel. Functions drawn from a GP with this kernel are polynomials of degree d. The kernel equation is

k(x, y) = (σ²xy + γ)ᵈ

where: σ² is the variance parameter, γ is the offset parameter, d is the degree parameter.

Parameters:
  • degree (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • offset (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

gpflow.kernels.Product

class gpflow.kernels.Product(kernels, name=None)[source]

Bases: ReducingCombination

Parameters:
  • kernels (Sequence[Kernel])

  • name (Optional[str])

gpflow.kernels.RBF

gpflow.kernels.RBF

alias of SquaredExponential

gpflow.kernels.RationalQuadratic

class gpflow.kernels.RationalQuadratic(variance=1.0, lengthscales=1.0, alpha=1.0, active_dims=None)[source]

Bases: IsotropicStationary

Rational Quadratic kernel,

k(r) = σ² (1 + r² / 2αℓ²)^(-α)

σ² : variance ℓ : lengthscales α : alpha, determines relative weighting of small-scale and large-scale fluctuations

For α → ∞, the RQ kernel becomes equivalent to the squared exponential.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • alpha (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

gpflow.kernels.SeparateIndependent

class gpflow.kernels.SeparateIndependent(kernels, name=None)[source]

Bases: MultioutputKernel, Combination

  • Separate: we use different kernel for each output latent

  • Independent: Latents are uncorrelated a priori.

Parameters:
  • kernels (Sequence[Kernel])

  • name (Optional[str])

K(X, X2=None, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X2), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [P, batch…, N, N] if (not full_output_cov) and (X2 is None).

  • return has shape [P, batch…, N, batch2…, N2] if (not full_output_cov) and (X2 is not None).

  • return has shape [batch…, N, P, N, P] if full_output_cov and (X2 is None).

  • return has shape [batch…, N, P, batch2…, N2, P] if full_output_cov and (X2 is not None).

cov[f(X), f(X2)]

K_diag(X, full_output_cov=False)[source]

Returns the correlation of f(X) and f(X), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [batch…, N, P, P] if full_output_cov.

  • return has shape [batch…, N, P] if not full_output_cov.

var[f(X)]

property latent_kernels: Tuple[Kernel, ...]

The underlying kernels in the multioutput kernel

property num_latent_gps: int

The number of latent GPs in the multioutput kernel

gpflow.kernels.SharedIndependent

class gpflow.kernels.SharedIndependent(kernel, output_dim)[source]

Bases: MultioutputKernel

  • Shared: we use the same kernel for each latent GP

  • Independent: Latents are uncorrelated a priori.

Warning

This class is created only for testing and comparison purposes. Use gpflow.kernels instead for more efficient code.

Parameters:
  • kernel (Kernel)

  • output_dim (int)

K(X, X2=None, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X2), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • X2 (Union[ndarray[Any, Any], Tensor, Variable, Parameter, None]) –

    • X2 has shape [batch2…, N2, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [P, batch…, N, N] if (not full_output_cov) and (X2 is None).

  • return has shape [P, batch…, N, batch2…, N2] if (not full_output_cov) and (X2 is not None).

  • return has shape [batch…, N, P, N, P] if full_output_cov and (X2 is None).

  • return has shape [batch…, N, P, batch2…, N2, P] if full_output_cov and (X2 is not None).

cov[f(X), f(X2)]

K_diag(X, full_output_cov=True)[source]

Returns the correlation of f(X) and f(X), where f(.) can be multi-dimensional.

Parameters:
  • X (Union[ndarray[Any, Any], Tensor, Variable, Parameter]) –

    • X has shape [batch…, N, D].

    data matrix

  • full_output_cov (bool) – calculate correlation between outputs.

Return type:

Tensor

Returns:

  • return has shape [batch…, N, P, P] if full_output_cov.

  • return has shape [batch…, N, P] if not full_output_cov.

var[f(X)]

property latent_kernels: Tuple[Kernel, ...]

The underlying kernels in the multioutput kernel

property num_latent_gps: int

The number of latent GPs in the multioutput kernel

gpflow.kernels.SquaredExponential

class gpflow.kernels.SquaredExponential(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: IsotropicStationary

The radial basis function (RBF) or squared exponential kernel. The kernel equation is

k(r) = σ² exp{-½ r²}

where: r is the Euclidean distance between the input points, scaled by the lengthscales parameter ℓ. σ² is the variance parameter

Functions drawn from a GP with this kernel are infinitely differentiable!

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

gpflow.kernels.Static

class gpflow.kernels.Static(variance=1.0, active_dims=None)[source]

Bases: Kernel

Kernels who don’t depend on the value of the inputs are ‘Static’. The only parameter is a variance, σ².

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

gpflow.kernels.Stationary

class gpflow.kernels.Stationary(variance=1.0, lengthscales=1.0, **kwargs)[source]

Bases: Kernel

Base class for kernels that are stationary, that is, they only depend on

d = x - x’

This class handles ‘ard’ behaviour, which stands for ‘Automatic Relevance Determination’. This means that the kernel has one lengthscale per dimension, otherwise the kernel is isotropic (has a single lengthscale).

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • lengthscales (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • kwargs (Any)

property ard: bool

Whether ARD behaviour is active.

gpflow.kernels.Sum

class gpflow.kernels.Sum(kernels, name=None)[source]

Bases: ReducingCombination

Parameters:
  • kernels (Sequence[Kernel])

  • name (Optional[str])

gpflow.kernels.WedgeHierarchical

class gpflow.kernels.WedgeHierarchical(hierarchy, base_kernel=None, *, active_dims, name=None)[source]

Bases: HierarchicalEmbeddingKernel

The Wedge kernel of Horn et al. (2019).

Each conditional column c (normalised value v_c in [0, 1], activity mask m_c) is mapped into the plane via

\[\phi_c(v_c, m_c) = \big( (\theta_1 v_c + \theta_2 v_c \cos\rho)\, m_c,\; (\theta_2 v_c \sin\rho)\, m_c \big).\]

The “incomparable” distance now scales with the active value v_c rather than being constant in it. rho is bounded away from zero because at rho = 0 the embedding degenerates to a line.

Parameters:
  • hierarchy (Sequence[HierarchyNode])

  • base_kernel (Optional[Stationary])

  • active_dims (Union[slice, Sequence[int]])

  • name (Optional[str])

gpflow.kernels.White

class gpflow.kernels.White(variance=1.0, active_dims=None)[source]

Bases: Static

The White kernel: this kernel produces ‘white noise’. The kernel equation is

k(x_n, x_m) = δ(n, m) σ²

where: δ(.,.) is the Kronecker delta, σ² is the variance parameter.

Parameters:
  • variance (Union[ndarray[Any, Any], Tensor, Variable, Parameter])

  • active_dims (Union[slice, Sequence[int], None])

Functions

gpflow.kernels.validate_hierarchical_axioms

gpflow.kernels.validate_hierarchical_axioms(kernel, hierarchy, *, n_samples=8, input_dim=None, active_dims=None, atol=1e-08, rtol=1e-06, seed=0)[source]

Numerically check that kernel obeys the three conditional-distance axioms (kernel-level shadows) on every conditional HierarchyNode in hierarchy.

Test inputs are constructed in the sliced coordinate system used by gpflow.kernels.HierarchicalEmbeddingKernel. For each conditional feature column, three predicates on K are evaluated over n_samples random backgrounds; the worst violation across samples is reported.

Parameters:
  • kernel (Kernel) – any GPflow Kernel. The validator only calls kernel(...) (i.e. Kernel.__call__()), never kernel.K directly. __call__ is what routes per-child active_dims slicing through Sum / Product / future combination kernels; Sum.K bypasses that slicing and would feed the raw input to every child.

  • hierarchy (Sequence[HierarchyNode]) – the same HierarchyNode sequence the user passed (or would pass) to a gpflow.kernels.HierarchicalEmbeddingKernel. Used only to construct test inputs; the kernel itself is treated as a black box.

  • n_samples (int) – number of random backgrounds per (node, feature, axiom) triple. Each background drives independent feature samples.

  • input_dim (Optional[int]) – width of the test input vectors. Defaults to the smallest value compatible with active_dims: when active_dims is None, that is max(referenced column) + 1 across the hierarchy; when active_dims is provided, it is max(active_dims) + 1 (or the slice’s stop).

  • active_dims (Union[slice, Sequence[int], None]) – optional mapping from the hierarchy’s sliced-coord positions to full-input column indices, matching the active_dims of the hierarchical kernel under test. Use this when the kernel slices an offset sub-range of a wider input (e.g. active_dims=[2, 3, 4, 5] inside a width-6 input). None (the default) is the identity mapping.

  • atol (float) – absolute tolerance used by the equality predicates (axioms 1 and 2) and as the minimum margin for axiom 3.

  • rtol (float) – relative tolerance used by the equality predicates.

  • seed (Optional[int]) – RNG seed for reproducibility. None uses fresh entropy.

Return type:

AxiomReport

Returns:

an AxiomReport whose passed is True iff every check held. The caller decides whether to assert, log, or display.