gpflux.layers.likelihood_layer#
A Keras Layer that wraps a likelihood, while containing the necessary operations for training.
Module Contents#
- class TrackableLayer[source]#
Bases:
gpflow.keras.tf_keras.layers.LayerWith the release of TensorFlow 2.5, our TrackableLayer workaround is no longer needed. See Prowler-io/gpflux#189. Will be removed in GPflux version 1.0.0
- unwrap_dist(dist: tfp.distributions.Distribution) tfp.distributions.Distribution[source]#
Unwrap the given distribution, if it is wrapped in a
_TensorCoercible.
- class LikelihoodLayer(likelihood: gpflow.likelihoods.Likelihood)[source]#
Bases:
gpflux.layers.trackable_layer.TrackableLayerA Keras layer that wraps a GPflow
Likelihood. This layer expects atfp.distributions.MultivariateNormalDiagas its input, describingq(f). When training, calling this class computes the negative variational expectation \(-\mathbb{E}_{q(f)}[\log p(y|f)]\) and adds it as a layer loss. When not training, it computes the mean and variance ofyunderq(f)usingpredict_mean_and_var().Note
Use either this
LikelihoodLayer(together withgpflux.models.DeepGP) orLikelihoodLoss(e.g. together with atf.keras.Sequentialmodel). Do not use both at once because this would add the loss twice.- call(inputs: tfp.distributions.MultivariateNormalDiag, targets: gpflow.base.TensorType | None = None, training: bool = None) LikelihoodOutputs[source]#
When training (
training=True), this method computes variational expectations (data-fit loss) and adds this information as a layer loss. When testing (the default), it computes the posterior mean and variance ofy.- Parameters:
inputs – The output distribution of the previous layer. This is currently expected to be a
MultivariateNormalDiag; that is, the precedingGPLayershould havefull_cov=full_output_cov=False.- Returns:
a
LikelihoodOutputstuple with the mean and variance offand, if not training, the mean and variance ofy.
Todo
Turn this layer into a
DistributionLambdaas well and return the correctDistributioninstead of a tuple containing mean and variance only.
- class LikelihoodOutputs(f_mean: gpflow.base.TensorType, f_var: gpflow.base.TensorType, y_mean: gpflow.base.TensorType | None, y_var: gpflow.base.TensorType | None)[source]#
Bases:
tf.ModuleThis class encapsulates the outputs of a
LikelihoodLayer.It contains the mean and variance of the marginal distribution of the final latent
GPLayer, as well as the mean and variance of the likelihood.This class includes the TensorMetaClass to make objects behave as a
tf.Tensor. This is necessary so that it can be returned from thetfp.layers.DistributionLambdaKeras layer.