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.Layer
With 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.TrackableLayer
A Keras layer that wraps a GPflow
Likelihood
. This layer expects atfp.distributions.MultivariateNormalDiag
as 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 ofy
underq(f)
usingpredict_mean_and_var()
.Note
Use either this
LikelihoodLayer
(together withgpflux.models.DeepGP
) orLikelihoodLoss
(e.g. together with atf.keras.Sequential
model). 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 precedingGPLayer
should havefull_cov=full_output_cov=False
.- Returns:
a
LikelihoodOutputs
tuple with the mean and variance off
and, if not training, the mean and variance ofy
.
Todo
Turn this layer into a
DistributionLambda
as well and return the correctDistribution
instead 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.Module
This 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.DistributionLambda
Keras layer.