romcomma.gpf.models.MOGPR§
- class MOGPR(data, kernel, mean_function=None, noise_variance=1.0)[source]§
Bases:
GPModel
,InternalDataTrainingLossMixin
Gaussian Process Regression.
This is a vanilla implementation of MOGP regression with a Gaussian likelihood. Multiple columns of Y are treated independently.
The log likelihood of this model is given by
\[\log p(Y \,|\, \mathbf f) = \mathcal N(Y \,|\, 0, \sigma_n^2 \mathbf{I})\]To train the model, we maximise the log _marginal_ likelihood w.r.t. the likelihood variance and kernel hyperparameters theta. The marginal likelihood is found by integrating the likelihood over the prior, and has the form
\[\log p(Y \,|\, \sigma_n, \theta) = \mathcal N(Y \,|\, 0, \mathbf{KXX} + \sigma_n^2 \mathbf{I})\]- Parameters:
data (Tuple[ndarray[Any, Any] | Tensor | Variable | Parameter, ndarray[Any, Any] | Tensor | Variable | Parameter]) –
kernel (MOStationary) –
mean_function (MOMeanFunction | None) –
noise_variance (float) –
- __init__(data, kernel, mean_function=None, noise_variance=1.0)[source]§
- Parameters:
data (Tuple[ndarray[Any, Any] | Tensor | Variable | Parameter, ndarray[Any, Any] | Tensor | Variable | Parameter]) – Tuple[InputData, OutputData], which determines L, M and N. Both InputData and OutputData must be of rank 2.
kernel (MOStationary) – Must be well-formed, with an (L,L) variance and an (L,M) lengthscales matrix.
mean_function (MOMeanFunction | None) – Defaults to Zero.
noise_variance (float) – Broadcast to (diagonal) (L,L) if necessary.
Methods
__init__
(data, kernel[, mean_function, ...])- param data:
Tuple[InputData, OutputData], which determines L, M and N. Both InputData and OutputData must be of rank 2.
calc_num_latent_gps
(kernel, likelihood, ...)Calculates the number of latent GPs required given the number of outputs output_dim and the type of likelihood and kernel.
calc_num_latent_gps_from_data
(data, kernel, ...)Calculates the number of latent GPs required based on the data as well as the type of kernel and likelihood.
Computes the log marginal likelihood.
log_posterior_density
(*args, **kwargs)This may be the posterior with respect to the hyperparameters (e.g. for GPR) or the posterior with respect to the function (e.g. for GPMC and SGPMC).
Sum of the log prior probability densities of all (constrained) variables in this model.
Objective for maximum likelihood estimation.
predict_f
(Xnew[, full_cov, full_output_cov])This method computes predictions at X in R^{N x D} input points
predict_f_samples
(Xnew[, num_samples, ...])Produce samples from the posterior latent function(s) at the input points.
predict_log_density
(data[, full_cov, ...])Compute the log density of the data at the new data points.
predict_y
(Xnew[, full_cov, full_output_cov])Compute the mean and variance of the held-out data at the input points.
Returns the training loss for this model.
training_loss_closure
(*[, compile])Convenience method.
with_name_scope
(method)Decorator to automatically enter the module name scope.
Attributes
KXX
The output dimensionality.
The input dimensionality.
Returns the name of this module as passed or determined in the ctor.
Returns a tf.name_scope instance for this class.
Sequence of non-trainable variables owned by this module and its submodules.
parameters
Sequence of all sub-modules.
trainable_parameters
Sequence of trainable variables owned by this module and its submodules.
Sequence of variables owned by this module and its submodules.
- property M§
The input dimensionality.
- property L§
The output dimensionality.
- maximum_log_likelihood_objective()[source]§
Objective for maximum likelihood estimation. Should be maximized. E.g. log-marginal likelihood (hyperparameter likelihood) for GPR, or lower bound to the log-marginal likelihood (ELBO) for sparse and variational GPs.
- Return type:
- log_marginal_likelihood()[source]§
Computes the log marginal likelihood.
\[\log p(Y | \theta).\]- Return type:
- predict_f(Xnew, full_cov=False, full_output_cov=False)[source]§
This method computes predictions at X in R^{N x D} input points
\[p(F* | Y)\]where F* are points on the MOGP at new data points, Y are noisy observations at training data points. Note that full_cov => full_output_cov (regardless of the ordinate given for full_output_cov), to avoid ambiguity.
- static calc_num_latent_gps(kernel, likelihood, output_dim)§
Calculates the number of latent GPs required given the number of outputs output_dim and the type of likelihood and kernel.
Note: It’s not nice for GPModel to need to be aware of specific likelihoods as here. However, num_latent_gps is a bit more broken in general, we should fix this in the future. There are also some slightly problematic assumptions re the output dimensions of mean_function. See https://github.com/GPflow/GPflow/issues/1343
- Parameters:
kernel (Kernel) –
likelihood (Likelihood) –
output_dim (int) –
- Return type:
int
- static calc_num_latent_gps_from_data(data, kernel, likelihood)§
Calculates the number of latent GPs required based on the data as well as the type of kernel and likelihood.
- log_posterior_density(*args, **kwargs)§
This may be the posterior with respect to the hyperparameters (e.g. for GPR) or the posterior with respect to the function (e.g. for GPMC and SGPMC). It assumes that maximum_log_likelihood_objective() is defined sensibly.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
- log_prior_density()§
Sum of the log prior probability densities of all (constrained) variables in this model.
- Return type:
- property name§
Returns the name of this module as passed or determined in the ctor.
NOTE: This is not the same as the self.name_scope.name which includes parent module names.
- property name_scope§
Returns a tf.name_scope instance for this class.
- property non_trainable_variables§
Sequence of non-trainable variables owned by this module and its submodules.
Note: this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don’t expect the return value to change.
- Returns:
A sequence of variables for the current module (sorted by attribute name) followed by variables from all submodules recursively (breadth first).
- predict_f_samples(Xnew, num_samples=None, full_cov=True, full_output_cov=False)§
Produce samples from the posterior latent function(s) at the input points.
- Parameters:
Xnew (ndarray[Any, Any] | Tensor | Variable | Parameter) – InputData Input locations at which to draw samples, shape […, N, D] where N is the number of rows and D is the input dimension of each point.
num_samples (int | None) – Number of samples to draw. If None, a single sample is drawn and the return shape is […, N, P], for any positive integer the return shape contains an extra batch dimension, […, S, N, P], with S = num_samples and P is the number of outputs.
full_cov (bool) – If True, draw correlated samples over the inputs. Computes the Cholesky over the dense covariance matrix of size [num_data, num_data]. If False, draw samples that are uncorrelated over the inputs.
full_output_cov (bool) – If True, draw correlated samples over the outputs. If False, draw samples that are uncorrelated over the outputs.
- Return type:
Currently, the method does not support full_output_cov=True and full_cov=True.
- predict_log_density(data, full_cov=False, full_output_cov=False)§
Compute the log density of the data at the new data points.
- predict_y(Xnew, full_cov=False, full_output_cov=False)§
Compute the mean and variance of the held-out data at the input points.
- property submodules§
Sequence of all sub-modules.
Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on).
>>> a = tf.Module() >>> b = tf.Module() >>> c = tf.Module() >>> a.b = b >>> b.c = c >>> list(a.submodules) == [b, c] True >>> list(b.submodules) == [c] True >>> list(c.submodules) == [] True
- Returns:
A sequence of all submodules.
- property trainable_variables§
Sequence of trainable variables owned by this module and its submodules.
Note: this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don’t expect the return value to change.
- Returns:
A sequence of variables for the current module (sorted by attribute name) followed by variables from all submodules recursively (breadth first).
- training_loss_closure(*, compile=True)§
Convenience method. Returns a closure which itself returns the training loss. This closure can be passed to the minimize methods on
gpflow.optimizers.Scipy
and subclasses of tf.optimizers.Optimizer.- Parameters:
compile (bool) – If True (default), compile the training loss function in a TensorFlow graph by wrapping it in tf.function()
- Return type:
Callable[[], Tensor]
- property variables§
Sequence of variables owned by this module and its submodules.
Note: this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don’t expect the return value to change.
- Returns:
A sequence of variables for the current module (sorted by attribute name) followed by variables from all submodules recursively (breadth first).
- classmethod with_name_scope(method)§
Decorator to automatically enter the module name scope.
>>> class MyModule(tf.Module): ... @tf.Module.with_name_scope ... def __call__(self, x): ... if not hasattr(self, 'w'): ... self.w = tf.Variable(tf.random.normal([x.shape[1], 3])) ... return tf.matmul(x, self.w)
Using the above module would produce `tf.Variable`s and `tf.Tensor`s whose names included the module name:
>>> mod = MyModule() >>> mod(tf.ones([1, 2])) <tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)> >>> mod.w <tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32, numpy=..., dtype=float32)>
- Parameters:
method – The method to wrap.
- Returns:
The original method wrapped such that it enters the module’s name scope.