Kernel, MLP & Gaussian Process
KernelRidge
Kernel Ridge Regression combining L2-regularized least squares with the kernel trick and closed-form dual solving.
Overview
KernelRidge combines Ridge Regression (L2-regularized linear least squares) with the kernel trick, learning a linear function in the reproducing kernel Hilbert space induced by the selected kernel. Unlike SVR (which solves a sparse dual QP via SMO), KernelRidge solves the closed-form dual system (K + alpha * I) * dual_coef = y directly and supports both 1D and 2D multi-output targets as well as sample_weight. (^▽^)
Backend Support
- Primitives:
N3(pairwise L2 distance for RBF kernels),E1(dense matrix multiplication for Gram matrices and prediction) - 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.kernel_ridge import KernelRidge
# Also available from top-level: from ochreml import KernelRidge
KernelRidge(
alpha: float = 1.0,
kernel: str = "linear",
gamma: float | None = None,
degree: int = 3,
coef0: float = 1.0,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
alpha | float | 1.0 | Regularization strength; must be a non-negative float (alpha >= 0). |
kernel | {"linear", "rbf", "poly", "sigmoid", "precomputed"} | "linear" | Kernel mapping used to construct the Gram matrix. |
gamma | float | None | None | Kernel coefficient for "rbf", "poly", and "sigmoid". If None, defaults to 1.0 / n_features. |
degree | int | 3 | Degree of the polynomial kernel (kernel="poly"). Ignored by other kernels. |
coef0 | float | 1.0 | Zero coefficient for "poly" and "sigmoid" kernels. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
dual_coef_ | ndarray of shape (n_samples,) or (n_samples, n_targets) | Representation of the weight vector(s) in kernel space. |
X_fit_ | ndarray of shape (n_samples, n_features) | Training feature matrix stored for kernel evaluation at prediction time. |
n_features_in_ | int | Number of features seen during fit(). |
device_ | str | None | Resolved execution device string. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y, sample_weight=None) -> KernelRidge | Fit the Kernel Ridge regression model on 1D or 2D targets y with optional sample weights. |
predict | predict(X) -> ndarray | Predict target values for samples in X (1D if trained on 1D y, 2D otherwise). |
score | score(X, y, sample_weight=None) -> float | Return the coefficient of determination (R^2) on (X, y). |
to | to(device) -> KernelRidge | Move the estimator's compute target to another device. |
save | save(path) -> None | Save the fitted model to a .dit binary file. |
load | KernelRidge.load(path, device=None) -> KernelRidge | Class method to load a fitted model from a .dit file. |
Example
import numpy as np
from ochreml.kernel_ridge import KernelRidge
X = np.linspace(-3.0, 3.0, 50).reshape(-1, 1)
y = np.cos(X[:, 0])
model = KernelRidge(alpha=0.1, kernel="rbf", gamma=0.5)
model.fit(X, y)
X_test = np.array([[-1.5], [0.0], [1.5]])
print("Predictions:", model.predict(X_test))
print("R^2 score:", model.score(X, y))
print("dual_coef_ shape:", model.dual_coef_.shape)