gpflux.models.deep_gp#

This module provides the base implementation for DeepGP models.

Module Contents#

class DeepGP(f_layers: List[tf.keras.layers.Layer], likelihood: gpflux.layers.LikelihoodLayer | gpflow.likelihoods.Likelihood, *, input_dim: int | None = None, target_dim: int | None = None, default_model_class: Type[tf.keras.Model] = tf.keras.Model, num_data: int | None = None)[source]#

Bases: gpflow.base.Module

This class combines a sequential function model f(x) = fₙ(⋯ (f₂(f₁(x)))) and a likelihood p(y|f).

Layers might depend on both inputs x and targets y during training by inheriting from LayerWithObservations; those will be passed the argument observations=[inputs, targets].

When data is used with methods in this class (e.g. predict_f() method), it needs to be with dtype corresponding to GPflow’s default dtype as in default_float().

Note

This class is not a tf.keras.Model subclass itself. To access Keras features, call either as_training_model() or as_prediction_model() (depending on the use-case) to create a tf.keras.Model instance. See the method docstrings for more details.

Parameters:
  • f_layers – The layers [f₁, f₂, …, fₙ] describing the latent function f(x) = fₙ(⋯ (f₂(f₁(x)))).

  • likelihood – The layer for the likelihood p(y|f). If this is a GPflow likelihood, it will be wrapped in a LikelihoodLayer. Alternatively, you can provide a LikelihoodLayer explicitly.

  • input_dim – The input dimensionality.

  • target_dim – The target dimensionality.

  • default_model_class – The default for the model_class argument of as_training_model() and as_prediction_model(); see the default_model_class attribute.

  • num_data – The number of points in the training dataset; see the num_data attribute. If you do not specify a value for this parameter explicitly, it is automatically detected from the num_data attribute in the GP layers.

f_layers: List[tf.keras.layers.Layer][source]#

A list of all layers in this DeepGP (just likelihood_layer is separate).

likelihood_layer: gpflux.layers.LikelihoodLayer[source]#

The likelihood layer.

default_model_class: Type[tf.keras.Model][source]#

The default for the model_class argument of as_training_model() and as_prediction_model(). This must have the same semantics as tf.keras.Model, that is, it must accept a list of inputs and an output. This could be tf.keras.Model itself or gpflux.optimization.NatGradModel (but not, for example, tf.keras.Sequential).

num_data: int[source]#

The number of points in the training dataset. This information is used to obtain correct scaling between the data-fit and the KL term in the evidence lower bound (elbo()).

static _validate_num_data(f_layers: List[tf.keras.layers.Layer], num_data: int | None = None) int[source]#

Check that the num_data attributes of all layers in f_layers are consistent with each other and with the (optional) num_data argument.

Returns:

The validated number of datapoints.

static _validate_dtype(x: gpflow.base.TensorType) None[source]#

Check that data x is of correct dtype, corresponding to GPflow’s default dtype as defined by default_float().

Raises:

ValueError – If x is of incorrect dtype.

_evaluate_deep_gp(inputs: gpflow.base.TensorType, targets: gpflow.base.TensorType | None, training: bool | None = None) tf.Tensor[source]#

Evaluate f(x) = fₙ(⋯ (f₂(f₁(x)))) on the inputs argument.

Layers that inherit from LayerWithObservations are passed the additional keyword argument observations=[inputs, targets] if targets contains a value, or observations=None when targets is None.

_evaluate_likelihood(f_outputs: gpflow.base.TensorType, targets: gpflow.base.TensorType | None, training: bool | None = None) tf.Tensor[source]#

Call the likelihood_layer on f_outputs, which adds the corresponding layer loss when training.

predict_f(inputs: gpflow.base.TensorType) Tuple[tf.Tensor, tf.Tensor][source]#
Returns:

The mean and variance (not the scale!) of f, for compatibility with GPflow models.

Raises:

ValueError – If x is of incorrect dtype.

Note

This method does not support full_cov or full_output_cov.

elbo(data: Tuple[gpflow.base.TensorType, gpflow.base.TensorType]) tf.Tensor[source]#
Returns:

The ELBO (not the per-datapoint loss!), for compatibility with GPflow models.

as_training_model(model_class: Type[tf.keras.Model] | None = None) tf.keras.Model[source]#

Construct a tf.keras.Model instance that requires you to provide both inputs and targets to its call. This information is required for training the model, because the targets need to be passed to the likelihood_layer (and to LayerWithObservations instances such as LatentVariableLayers, if present).

When compiling the returned model, do not provide any additional losses (this is handled by the likelihood_layer).

Train with

model.compile(optimizer)  # do NOT pass a loss here
model.fit({"inputs": X, "targets": Y}, ...)

See Keras’s Endpoint layer pattern for more details.

Note

Use as_prediction_model if you want only to predict, and do not want to pass in a dummy array for the targets.

Parameters:

model_class – The model class to use; overrides default_model_class.

as_prediction_model(model_class: Type[tf.keras.Model] | None = None) tf.keras.Model[source]#

Construct a tf.keras.Model instance that requires only inputs, which means you do not have to provide dummy target values when predicting at test points.

Predict with

model.predict(Xtest, ...)

Note

The returned model will not support training; for that, use as_training_model.

Parameters:

model_class – The model class to use; overrides default_model_class.