GPJax

repository·main·Indexed 20 days ago

https://github.com/thomaspinder/gpjax

A low-level Gaussian process (GP) framework implemented in JAX, designed for researchers to maintain maximum flexibility by keeping implementations close to mathematical theory. It supports various inference methods including Conjugate, Sparse Variational, Stochastic Variational, and Laplace Approximation, as well as regression, classification, and deep kernel regression. The library utilizes a declarative API where models are composed of mean functions, kernels, priors, and likelihoods.

Tokens
27.4K
Snippets
85
Records
130
Agent score
68%

What's inside gpjax

  1. Overview of GPJax capabilities

    main

    GPJax provides a low-level interface to Gaussian process (GP) models built on Jax. It is designed for researchers, aiming to keep the code as close as possible to the mathematical formulations used in GP modeling.

    Supported inference methods and use cases include:

    • Inference Methods: Conjugate Inference, Sparse Variational Inference, Stochastic Variational Inference, Laplace Approximation.
    • Regression/Classification: Regression, Classification, Poisson Regression, Heteroscedastic Inference, Deep Kernel Regression.
    • Advanced Topics: Inference on Non-Euclidean Spaces, Inference on Graphs, Learning Gaussian Process Barycentres.
  2. Overview of GPJax: A Gaussian Process Framework in JAX

    main

    GPJax is a didactic Gaussian Process (GP) library designed for researchers to develop novel GP methodology. It provides a set of composable objects that closely resemble mathematical notation, making it easier to translate theory into code.

    Built on top of JAX, GPJax natively supports:

    • Efficient compilation to XLA
    • Automatic differentiation
    • Vectorised operations
    • Hardware acceleration on CPUs, GPUs, and TPUs

    Because GPJax objects are built using Chex dataclasses and registered as PyTree nodes, they are fully compatible with standard JAX transformations like jit, grad, and vmap.

  3. Explore the GPJax API Reference

    main

    The GPJax API reference is a complete, auto-generated documentation set for all modules. It is synchronized with the source code via Sphinx autodoc, ensuring that the documentation reflects the current public API (the __all__ exports) of the package.

    To understand the library, you can explore the following core modules:

    • dataset: Data handling and loading.
    • distributions: Probability distributions used in GP modeling.
    • gps: Gaussian Process implementations.
    • kernels: Covariance function definitions.
    • likelihoods: Likelihood functions for different noise models.
    • mean_functions: Prior mean function definitions.
    • parameters: Parameter management and optimization.
    • objectives: Loss functions and optimization targets.
    • fit: Fitting routines and optimization workflows.
    • variational_families: Approximations for non-Gaussian or complex GPs.
    • models: High-level model abstractions.
    • state_space: State-space representations for efficient GP inference.
    • linalg: Linear algebra utilities.
    • integrators: Numerical integration tools.
    • scan: Scanning operations for sequential data.
    • summary: Tools for summarizing model results.
    • typing: Type hints and structural definitions.
    • citation: Information for citing GPJax in research.
  4. Use State-Space GPs in GPJax

    main

    The gpjax.state_space module provides tools for working with State-Space Gaussian Processes. This includes defining state-space priors, computing state-space marginal likelihoods, and performing model fitting using various optimizers.

    Key components available in this module include:

    • Priors: StateSpacePrior for defining the GP prior in state-space form.
    • Posterior Inference: StateSpaceConjugatePosterior for efficient conjugate inference.
    • Fitting: fit, fit_lbfgs, and fit_scipy for optimizing model hyperparameters.
    • Utilities: state_space_mll for calculating the marginal log-likelihood and to_sde for converting models to Stochastic Differential Equations.
  5. Understand GPJax mathematical notation

    main

    GPJax is designed to provide an accurate representation of Gaussian process mathematics. Variable names in the API are chosen to closely match the notation used in Rasmussen & Williams (2006).

    When reading GPJax code or documentation, use the following mapping to relate code variables to mathematical quantities:

    Standard Gaussian Process Notation

    • n: Number of training inputs
    • x: Training inputs
    • y: Training labels
    • t: Test inputs
    • f: Latent function modelled as a GP
    • fx: Latent function at inputs x
    • mux: Prior mean at inputs x
    • Kxx: Kernel Gram matrix at inputs x
    • Lx: Lower Cholesky factor of Kxx
    • Ktx: Cross-covariance between inputs t and x

    Sparse Gaussian Process Notation

    • m: Number of inducing points
    • z: Inducing inputs
    • u: Inducing outputs
  6. Optimize using natural parameters for faster convergence

    main

    GPJax supports parameterisation using natural parameters $(\mathbf{\Sigma}^{-1}\boldsymbol{\mu}, -\tfrac{1}{2}\mathbf{\Sigma}^{-1})$ instead of moment parameterisation $(\boldsymbol{\mu}, \mathbf{\Sigma})$. Using natural parameters makes natural gradients computationally cheap (avoiding Fisher-matrix inversion) and typically leads to much faster convergence than plain gradient descent on moments.

    Relevant classes include:

    • NaturalVariationalGaussian
    • ExpectationVariationalGaussian
  7. How GPJax models are composed

    main

    GPJax follows a mathematical composition pattern. A Gaussian Process posterior is represented as the product of a Prior and a Likelihood:

    posterior = prior * likelihood

    This abstraction maps directly to the Bayesian identity $p(f|y) ext{ } ext{'} ext{ } p(f)p(y|f)$, where the Prior represents $p(f)$ and the Likelihood represents $p(y|f)$.

    posterior = prior * likelihood
  8. Integration with the JAX Ecosystem

    main

    GPJax is designed to integrate seamlessly with the broader JAX ecosystem for various probabilistic and machine learning tasks:

    • Inference (MCMC): Can utilize samplers from BlackJax and TensorFlow Probability.
    • Optimisation: Integrates with Optax for gradient-based optimisation, providing access to various optimisers and learning rate schedules.
    • Probability Distributions: Leverages Distrax and TensorFlow Probability for efficient representation.
    • Deep Learning: Can incorporate functionality from Haiku to combine GPs with deep learning methods.
  9. How the GPJax parameter system works with Bijectors

    main

    GPJax uses Bijectors to handle constrained parameters (e.g., lengthscales that must be positive). Instead of optimizing in the constrained space, GPJax optimizes in an unconstrained space and uses a functional mapping to project values back to their valid support.

    Implementation via Paramax

    GPJax uses the Paramax library to manage these constraints. Constrained parameters like PositiveReal and SigmoidBounded are subclasses of paramax.AbstractUnwrappable.

    • Optimization: During fit, paramax.unwrap is called inside the loss function. This recursively maps internal unconstrained values to their constrained counterparts. Gradients are computed in the unconstrained space, and updates are applied directly to the unconstrained arrays.
    • Freezing Parameters: To prevent specific parameters from being updated during optimization, wrap them with paramax.non_trainable. This excludes the subtree from gradient updates while keeping the value available for evaluation.
  10. Understand ELBO objectives for variational inference

    main

    When the marginal log-likelihood is unavailable (e.g., with non-conjugate likelihoods), GPJax uses the Evidence Lower Bound (ELBO). There are two primary implementations:

    1. elbo: The uncollapsed, mini-batchable bound.
    2. collapsed_elbo: The collapsed bound, which solves variational parameters analytically. This requires a conjugate likelihood and a full pass over the data.
  11. Use inducing points for sparse Gaussian processes

    main
    Inducing points (also called pseudo-points) are a set of $m$ pseudo-inputs $\boldsymbol{z}$ (where $m \ll n$) that summarise the training data. Using them reduces inference complexity from $\mathcal{O}(n^3)$ to $\mathcal{O}(nm^2)$. These points are treated as ordinary model parameters and are optimised alongside kernel hyperparameters.
  12. Rules for writing valid benchmarks

    main

    To ensure benchmark accuracy and compatibility with the ASV (Airspeed Velocity) continuous benchmarking system, follow these five rules:

    1. Always realise(result): Call realise(result) at the end of every timed call. Because JAX is asynchronous, you must use the block_until_ready() calls inside realise to time the actual work rather than just the dispatch.
    2. No beartype or hypothesis: Do not use these in the benchmark environment. They are provided via tests/conftest.py, which ASV does not load. Do not add a conftest.py to the benchmarks directory.
    3. Pin --machine: Always specify the --machine flag when running. Trends are keyed by machine identity; changing machines creates a new series.
    4. Cache discipline:
      • time_* benchmarks require a warm JAX cache (initialized in setup() with one untimed call).
      • track_compile_* benchmarks must call jax.clear_caches() in setup().
    5. Python 3.13: The environment is pinned to Python 3.13. Do not change the version in asv.conf.json as it resets historical series.