Skip to main content

Module gradient

Module gradient 

Source
Expand description

Matrix-level gradient computation for kernel hyperparameter optimization.

This module provides utilities for computing gradients of kernel matrices with respect to hyperparameters, essential for Gaussian Process optimization.

§Overview

When optimizing kernel hyperparameters (e.g., via gradient descent or L-BFGS), we need to compute the gradient of the kernel matrix K with respect to each hyperparameter θ: dK/dθ (an N×N matrix of partial derivatives).

§Supported Kernels

  • RBF/Gaussian: dK/dγ (gamma) and dK/dl (length scale)
  • Polynomial: dK/dc (constant) and dK/dd (degree, continuous)
  • Matérn: dK/dl (length scale) for nu = 0.5, 1.5, 2.5
  • Laplacian: dK/dγ (gamma) and dK/dσ (sigma)
  • Rational Quadratic: dK/dl (length scale) and dK/dα (alpha)

§Example

use tensorlogic_sklears_kernels::{RbfKernel, RbfKernelConfig};
use tensorlogic_sklears_kernels::gradient::{KernelGradientMatrix, compute_rbf_gradient_matrix};

let kernel = RbfKernel::new(RbfKernelConfig::new(0.5)).unwrap();
let data = vec![
    vec![1.0, 2.0],
    vec![3.0, 4.0],
    vec![5.0, 6.0],
];

let grad_result = compute_rbf_gradient_matrix(&kernel, &data).unwrap();
// grad_result.kernel_matrix is the N×N kernel matrix K
// grad_result.gradient_gamma is dK/dγ (N×N matrix)
// grad_result.gradient_length_scale is dK/dl (N×N matrix)

Structs§

GradientComponent
A single gradient component with its name and matrix.
KernelGradientMatrix
Result of computing kernel matrix with gradients.
LaplacianGradientResult
Result of Laplacian gradient computation.
MaternGradientResult
Result of Matérn gradient computation.
PolynomialGradientResult
Result of Polynomial gradient computation.
RationalQuadraticGradientResult
Result of Rational Quadratic gradient computation.
RbfGradientResult
Result of RBF gradient computation with specific gradient accessors.

Functions§

compute_generic_gradient_matrix
Generic function to compute gradient matrix for any kernel with gradient support.
compute_laplacian_gradient_matrix
Compute the kernel matrix and gradients for a Laplacian kernel.
compute_matern_gradient_matrix
Compute the kernel matrix and gradients for a Matérn kernel.
compute_polynomial_gradient_matrix
Compute the kernel matrix and gradients for a Polynomial kernel.
compute_rational_quadratic_gradient_matrix
Compute the kernel matrix and gradients for a Rational Quadratic kernel.
compute_rbf_gradient_matrix
Compute the kernel matrix and gradients for an RBF kernel.
frobenius_norm
Compute the Frobenius norm of a matrix: ||A||F = sqrt(sum{i,j} A_{ij}^2).
is_symmetric
Check if a gradient matrix is symmetric (as it should be for symmetric kernels).
trace_product
Compute the trace of the product of two matrices: tr(A * B).