use std::marker::PhantomData;
use faer::Mat;
#[cfg(test)]
use faer::MatRef;
use crate::error::GprError;
use crate::policy::with_kernel_exp;
use crate::sparse::{
PredictScratch, SparseCore, SparseScratch, sparse_core_accessors, sparse_kernel_accessor,
sparse_point_accessors,
};
use crate::kernel::{KernelScalar, KernelSpec, ModelKernel};
use crate::optimizer::{Fixed, Lbfgs, OptResult, Optimizer};
use crate::param::Interval;
use crate::precision::{DoublePrecision, ModelPrecision};
use crate::sgpr::SgprObjective;
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::factor::{
VfeExact, VfeState, VfeSystem, analytic_gradient, analytic_hessian, assemble_vfe,
fill_z_intervals, predict_vfe_covariance, predict_vfe_into, publish_sgpr_weights, vfe_loo,
vfe_neg_log_marginal_likelihood,
};
use super::model::Sgpr;
use super::online::OnlineSgpr;
use super::{FixedInducing, InducingLayout};
use crate::kernel::NoSupply;
#[derive(Clone, Debug)]
pub struct FittedSgpr<
O = Lbfgs,
I = FixedInducing,
P: ModelPrecision = DoublePrecision,
K: ModelKernel = KernelSpec,
> {
pub(super) core: SparseCore<K::Supply>,
pub(super) scratch: SparseScratch<P::Storage, K::Supply>,
pub(super) optimizer: O,
pub(super) inducing: PhantomData<I>,
pub(super) _kernel: PhantomData<K>,
pub(super) k_mm_l: Mat<P::Storage>,
pub(super) k_mm_l_f64: <P::Storage as crate::kernel::ScalarOps>::F64Factor,
pub(super) k_mm_jitter: f64,
pub(super) a: Mat<P::Storage>,
pub(super) b_l: Mat<P::Storage>,
pub(super) w: Vec<P::Storage>,
pub(super) predict_w: Vec<P::Refine>,
pub(super) k_diag_sum: P::Storage,
pub(super) a_frobenius2: P::Storage,
pub(super) exact: <P::Storage as crate::kernel::ScalarOps>::F64Held<VfeExact>,
}
impl<O, I: InducingLayout<K::Supply>, P, K> FittedSgpr<O, I, P, K>
where
P: crate::precision::GpScalar,
K: ModelKernel,
{
sparse_core_accessors!();
pub fn num_params(&self) -> usize {
self.core.theta_len() + I::z_params(self.core.m, self.core.d)
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
crate::data::require_count(out.len(), self.num_params(), "parameters")?;
let n_theta = self.core.theta_len();
self.core.read_theta(&mut out[..n_theta])?;
if I::z_params(self.core.m, self.core.d) > 0 {
out[n_theta..].copy_from_slice(&self.core.z_train);
}
Ok(())
}
fn same_stored_params(&self, params: &[f64]) -> Result<bool, GprError> {
let mut current = vec![0.0; params.len()];
self.get_params(&mut current)?;
Ok(current == params)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
let n_theta = self.core.theta_len();
crate::data::require_count(params.len(), self.num_params(), "parameters")?;
if self.same_stored_params(params)? {
return Ok(());
}
let (kernel, likelihood) = self.core.stage_theta(¶ms[..n_theta])?;
let free_z = I::z_params(self.core.m, self.core.d) > 0;
let z = if free_z {
params[n_theta..].to_vec()
} else {
self.core.z_train.clone()
};
let z_obs = if free_z {
self.core.inducing_obs(&z, self.core.m)?
} else {
self.core.z_obs.clone()
};
let data = crate::sparse::SparseData {
z: &z,
..self.core.data()
};
let state = with_kernel_exp!(self.core.math, M => assemble_vfe::<M, _, K::Supply>(
&kernel,
self.core.jitter,
likelihood,
data,
&mut self.scratch.storage,
&mut self.scratch.f64,
))?;
self.core.kernel = kernel;
self.core.likelihood = likelihood;
self.core.z_train = z;
self.core.z_obs = z_obs;
self.apply_vfe(state);
Ok(())
}
pub(crate) fn fill_intervals(&self, out: &mut [Interval]) -> Result<(), GprError> {
crate::data::require_count(out.len(), self.num_params(), "intervals")?;
let n_theta = self.core.theta_len();
self.core.theta_intervals(&mut out[..n_theta])?;
if I::z_params(self.core.m, self.core.d) > 0 {
fill_z_intervals(
&self.core.x_train,
self.core.n,
self.core.d,
&mut out[n_theta..],
)?;
}
Ok(())
}
pub fn value_and_gradient_into(
&mut self,
params: &[f64],
out: &mut [f64],
) -> Result<f64, GprError>
where
P: crate::precision::GpScalar,
{
let n_params = self.num_params();
crate::data::require_count(params.len(), n_params, "parameters")?;
crate::data::require_count(out.len(), n_params, "parameters")?;
self.set_params(params)?;
let value = self.neg_log_marginal_likelihood()?;
let mut ks = std::mem::take(&mut self.scratch.storage);
let result = with_kernel_exp!(self.core.math, M => analytic_gradient::<M, _, _, _, _>(
self, out, &mut ks
));
self.scratch.storage = ks;
result?;
Ok(value)
}
pub fn hessian_into(&mut self, params: &[f64], out: &mut [f64]) -> Result<(), GprError>
where
P: crate::precision::GpScalar,
{
let n_params = self.num_params();
crate::data::require_count(params.len(), n_params, "parameters")?;
crate::data::require_count(out.len(), n_params * n_params, "parameters")?;
self.set_params(params)?;
let mut ks = std::mem::take(&mut self.scratch.storage);
let result = with_kernel_exp!(self.core.math, M => analytic_hessian::<M, _, _, _, _>(
self, out, &mut ks
));
self.scratch.storage = ks;
result?;
Ok(())
}
pub(crate) fn refresh_predict_w(&mut self) -> Result<(), GprError> {
self.predict_w = with_kernel_exp!(self.core.math, M => publish_sgpr_weights::<M, P, K::Supply>(
&self.core.kernel,
self.core.jitter,
self.a.as_ref(),
self.b_l.as_ref(),
&self.w,
self.core.data(),
self.core.likelihood.noise_variance(),
))?;
Ok(())
}
pub(crate) fn apply_vfe(&mut self, state: VfeState<P::Storage>) {
self.predict_w = promote_predict_w::<P>(&state.w);
self.k_mm_l = state.k_mm_l;
self.k_mm_l_f64 = state.k_mm_l_f64;
self.k_mm_jitter = state.k_mm_jitter;
self.a = state.a;
self.b_l = state.b_l;
self.w = state.w;
self.k_diag_sum = state.k_diag_sum;
self.a_frobenius2 = state.a_frobenius2;
self.exact = state.exact;
}
#[cfg(test)]
pub(crate) fn vfe_w_and_b_l(&self) -> (&[P::Storage], MatRef<'_, P::Storage>) {
(&self.w, self.b_l.as_ref())
}
pub(crate) fn core(&self) -> &SparseCore<K::Supply> {
&self.core
}
pub(crate) fn into_trainer(self) -> Sgpr<O, I, P, K> {
Sgpr {
spec: self.core.spec(),
optimizer: self.optimizer,
inducing: PhantomData,
_precision: PhantomData,
_kernel: PhantomData,
}
}
pub(crate) fn optimize_hyperparameters(&mut self) -> Result<(), GprError>
where
O: Clone + for<'a> Optimizer<SgprObjective<'a, O, I, P, K>>,
{
let mut init = vec![0.0; self.num_params()];
self.get_params(&mut init)?;
let optimizer = self.optimizer.clone();
let result = {
let mut obj = SgprObjective::new(self);
optimizer.minimize(&mut obj, &init)
};
self.commit_or_revert_optimize(&init, result)
}
fn commit_or_revert_optimize(
&mut self,
init: &[f64],
result: Result<OptResult, GprError>,
) -> Result<(), GprError> {
let committed = match result {
Ok(opt) if opt.params.len() != self.num_params() || !opt.value.is_finite() => {
Err(GprError::OptimizationNotConverged {
iterations: opt.iterations as usize,
})
}
Ok(opt) => self
.set_params(&opt.params)
.and_then(|()| self.refresh_predict_w()),
Err(err) => Err(err),
};
if let Err(err) = committed {
self.revert_to(init);
return Err(err);
}
Ok(())
}
fn revert_to(&mut self, init: &[f64]) {
if self.set_params(init).is_ok() {
let _ = self.refresh_predict_w();
}
}
pub fn neg_log_marginal_likelihood(&self) -> Result<f64, GprError> {
vfe_neg_log_marginal_likelihood(
self.a.as_ref(),
self.b_l.as_ref(),
&self.w,
&self.core.y_train,
self.k_diag_sum,
self.a_frobenius2,
self.core.likelihood.noise_variance(),
self.core.n,
self.core.m,
)
}
pub fn loo_predict(&self) -> Result<Prediction<P::Refine>, GprError> {
self.loo_predict_with(PredictOptions::default())
}
pub fn loo_predict_with(
&self,
options: PredictOptions,
) -> Result<Prediction<P::Refine>, GprError> {
vfe_loo::<P, K::Supply>(
&self.core,
self.a.as_ref(),
self.b_l.as_ref(),
&self.w,
options,
)
}
#[cfg(test)]
pub(crate) fn k_mm_l(&self) -> MatRef<'_, P::Storage> {
self.k_mm_l.as_ref()
}
}
impl<O, I, P, K> FittedSgpr<O, I, P, K>
where
P: crate::precision::GpScalar,
K: crate::kernel::PointKernel,
{
sparse_point_accessors!();
}
impl<O, I: InducingLayout<NoSupply>, P> FittedSgpr<O, I, P>
where
P: crate::precision::GpScalar,
{
sparse_kernel_accessor!();
pub fn save(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
crate::persist::save_sgpr(self, dir.as_ref())
}
pub fn into_online(self) -> OnlineSgpr<O, P> {
OnlineSgpr::from_fitted(self)
}
pub fn predict(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
) -> Result<Prediction<P::Refine>, GprError> {
self.predict_with(xs, n_rows, n_cols, PredictOptions::default())
}
pub fn predict_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
) -> Result<Prediction<P::Refine>, GprError> {
let mut out = Prediction::default();
predict_vfe_into::<P, crate::kernel::NoSupply>(
&self.core,
&VfeSystem::new(
&self.core,
self.k_mm_l.as_ref(),
self.b_l.as_ref(),
&self.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
options,
&mut PredictScratch::default(),
&mut out,
)?;
Ok(out)
}
pub fn predict_covariance(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
) -> Result<PredictiveCovariance<P::Refine>, GprError> {
self.predict_covariance_with(xs, n_rows, n_cols, PredictOptions::default())
}
pub fn predict_covariance_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
) -> Result<PredictiveCovariance<P::Refine>, GprError> {
predict_vfe_covariance::<P, crate::kernel::NoSupply>(
&self.core,
&VfeSystem::new(
&self.core,
self.k_mm_l.as_ref(),
self.b_l.as_ref(),
&self.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
std::iter::empty(),
options,
)
}
pub fn sample(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
n_draws: usize,
seed: u64,
) -> Result<Vec<P::Refine>, GprError> {
self.sample_with(xs, n_rows, n_cols, PredictOptions::default(), n_draws, seed)
}
pub fn sample_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
n_draws: usize,
seed: u64,
) -> Result<Vec<P::Refine>, GprError> {
self.predict_covariance_with(xs, n_rows, n_cols, options)?
.draw(n_draws, seed, self.core.jitter)
}
pub fn predict_into(
&mut self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
out: &mut Prediction<P::Refine>,
) -> Result<(), GprError> {
self.predict_with_into(xs, n_rows, n_cols, PredictOptions::default(), out)
}
pub fn predict_with_into(
&mut self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
out: &mut Prediction<P::Refine>,
) -> Result<(), GprError> {
predict_vfe_into::<P, crate::kernel::NoSupply>(
&self.core,
&VfeSystem::new(
&self.core,
self.k_mm_l.as_ref(),
self.b_l.as_ref(),
&self.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
options,
&mut self.scratch.predict,
out,
)
}
}
impl<P: crate::precision::GpScalar, K: ModelKernel> FittedSgpr<Fixed, FixedInducing, P, K> {
pub(crate) fn from_persisted(core: SparseCore<K::Supply>) -> Result<Self, GprError> {
with_kernel_exp!(core.math, M => super::factor::assemble_fitted::<Fixed, FixedInducing, M, P, K>(
core, Fixed
))
}
}
pub(crate) fn promote_predict_w<P: ModelPrecision>(w: &[P::Storage]) -> Vec<P::Refine> {
w.iter()
.map(|value| P::Refine::from_f64(value.to_f64()))
.collect()
}