Kernel, MLP & Gaussian Process
GaussianProcessRegressor
Bayesian Gaussian Process regression with Cholesky factorization, predictive uncertainty, and Nelder-Mead LML optimization.
Overview
GaussianProcessRegressor implements Bayesian Gaussian Process (GP) regression backed by Rust Cholesky factorization and optional Nelder-Mead optimization of the log marginal likelihood (LML). At inference time, it returns the predictive mean and can optionally return either the per-point predictive standard deviation (return_std=True) or the full joint predictive covariance matrix (return_cov=True). (^▽^)
Backend Support
- Primitives:
N3(pairwise_l2_sqfor RBF, Matern, and Rational Quadratic kernels),N2(elementwise_op),E1(matvec/matmul) - Devices:
cpu(Rayon parallelf64),cuda/cuda:0(dynamic cuBLAS),wgpu(Vulkan/Metal/DX12),tpu(OpenXLA PJRT), andauto - Persistence:
.ditbinary format viasave()/load()(andochreml.save()/ochreml.load()), plus Pythonpicklesupport
Import & Signature
from ochreml.gaussian_process import GaussianProcessRegressor
# Also available from top-level: from ochreml import GaussianProcessRegressor
GaussianProcessRegressor(
kernel: str | dict | Any | None = "rbf",
length_scale: float = 1.0,
constant_value: float = 1.0,
nu: float = 1.5,
alpha: float = 1e-10,
noise_level: float = 1.0,
rq_alpha: float = 1.0,
optimize_hyperparams: bool = False,
n_restarts_optimizer: int = 0,
normalize_y: bool = False,
random_state: int | None = None,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
kernel | str, dict, or None | "rbf" | Kernel specification: "rbf", "matern", "white", "rational_quadratic", or "rbf_white". Also accepts a dict or kernel-like object with matching attributes. |
length_scale | float | 1.0 | Length scale of the covariance kernel (length_scale > 0). |
constant_value | float | 1.0 | Constant variance scaling factor of the kernel (constant_value > 0). |
nu | float | 1.5 | Smoothness parameter for the Matern kernel (0.5, 1.5, or 2.5). |
alpha | float | 1e-10 | Non-negative jitter added to the diagonal of the kernel matrix during fitting (alpha >= 0) for numerical stability or observation noise variance. |
noise_level | float | 1.0 | Noise variance for "white" and "rbf_white" kernels (noise_level > 0). |
rq_alpha | float | 1.0 | Scale mixture parameter for "rational_quadratic" kernel (rq_alpha > 0). |
optimize_hyperparams | bool | False | Whether to optimize kernel hyperparameters via Nelder-Mead maximization of the log marginal likelihood. |
n_restarts_optimizer | int | 0 | Number of random restarts for the hyperparameter optimizer. |
normalize_y | bool | False | Whether to normalize target values y by removing the mean and scaling to unit standard deviation. |
random_state | int | None | None | Random seed used for optimizer restarts. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
X_train_ | ndarray of shape (n_samples, n_features) | Training feature matrix stored for posterior prediction. |
L_ | ndarray of shape (n_samples, n_samples) | Lower-triangular Cholesky factor of K(X_train, X_train) + alpha * I. |
alpha_ | ndarray of shape (n_samples,) | Dual coefficients solving L * L^T * alpha_ = y. |
length_scale_ | float | Fitted kernel length scale (updated when optimize_hyperparams=True). |
constant_value_ | float | Fitted constant variance scale. |
noise_level_ | float | Fitted noise level for white-noise kernels. |
rq_alpha_ | float | Fitted rational quadratic scale mixture parameter. |
y_train_mean_ | float | Mean of training targets (non-zero when normalize_y=True). |
y_train_std_ | float | Standard deviation of training targets (used when normalize_y=True). |
log_marginal_likelihood_value_ | float | None | Log marginal likelihood of the fitted hyperparameters. |
n_features_in_ | int | None | Number of features seen during fit(). |
device_ | str | None | Resolved execution device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y) -> GaussianProcessRegressor | Fit the Gaussian Process regression model on (X, y). |
predict | predict(X, return_std=False, return_cov=False) | Predict posterior mean y_mean, or (y_mean, y_std) / (y_mean, y_cov). |
log_marginal_likelihood | log_marginal_likelihood() -> float | Return the log marginal likelihood of the fitted model. |
score | score(X, y) -> float | Return the coefficient of determination (R^2) on (X, y). |
to | to(device) -> GaussianProcessRegressor | Move the estimator's compute target to another device. |
save | save(path) -> None | Save the fitted model to a .dit binary file. |
load | GaussianProcessRegressor.load(path, device=None) -> GaussianProcessRegressor | Class method to load a fitted model from a .dit file. |
Example
import numpy as np
from ochreml.gaussian_process import GaussianProcessRegressor
X_train = np.linspace(0.0, 5.0, 15).reshape(-1, 1)
y_train = np.sin(X_train[:, 0])
gpr = GaussianProcessRegressor(
kernel="rbf",
length_scale=1.0,
alpha=1e-6,
optimize_hyperparams=True,
n_restarts_optimizer=2,
random_state=42,
)
gpr.fit(X_train, y_train)
X_query = np.array([[0.5], [2.5], [4.5]])
y_mean, y_std = gpr.predict(X_query, return_std=True)
print("Predictive mean:", y_mean)
print("Predictive std:", y_std)
print("LML:", gpr.log_marginal_likelihood())
print("Optimized length_scale_:", gpr.length_scale_)