Skip to content

Losses: keras_proba.losses

Negative log-likelihoods for the probabilistic regression heads.

Loss classes

GaussianNLL

GaussianNLL(name='gaussian_nll', reduction='sum_over_batch_size', dtype=None)

Bases: keras.src.losses.loss.Loss

Gaussian negative log-likelihood loss for GaussianHead outputs.

Example:

model = keras.Sequential([keras.Input((8,)), keras.layers.Dense(64, "relu"), GaussianHead(1)])
model.compile(optimizer="adam", loss=GaussianNLL())

__doc__ class-attribute

__doc__ = 'Gaussian negative log-likelihood loss for `GaussianHead` outputs.\n\n    Example:\n\n    ```python\n    model = keras.Sequential([keras.Input((8,)), keras.layers.Dense(64, "relu"), GaussianHead(1)])\n    model.compile(optimizer="adam", loss=GaussianNLL())\n    ```\n    '

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__module__ class-attribute

__module__ = 'keras_proba.layers.gaussian_head'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

MixtureGaussianNLL

MixtureGaussianNLL(units=None, name='mixture_gaussian_nll', reduction='sum_over_batch_size', dtype=None)

Bases: keras.src.losses.loss.Loss

Negative log-likelihood loss for MixtureGaussianHead outputs.

Example:

model = keras.Sequential([
    keras.Input((1,)),
    keras.layers.Dense(64, activation="relu"),
    MixtureGaussianHead(num_components=5),
])
model.compile(optimizer="adam", loss=MixtureGaussianNLL())

Parameters:

Name Type Description Default
units

Number of regression targets. Inferred from the targets if None; set it when targets of shape (batch, units) could be confused with (batch,) targets for units=1.

None
name

Name of the loss.

'mixture_gaussian_nll'
reduction

Keras loss reduction.

'sum_over_batch_size'
dtype

Dtype of the loss computation.

None

__doc__ class-attribute

__doc__ = 'Negative log-likelihood loss for `MixtureGaussianHead` outputs.\n\n    Example:\n\n    ```python\n    model = keras.Sequential([\n        keras.Input((1,)),\n        keras.layers.Dense(64, activation="relu"),\n        MixtureGaussianHead(num_components=5),\n    ])\n    model.compile(optimizer="adam", loss=MixtureGaussianNLL())\n    ```\n\n    Args:\n        units: Number of regression targets. Inferred from the targets if\n            `None`; set it when targets of shape `(batch, units)` could be\n            confused with `(batch,)` targets for `units=1`.\n        name: Name of the loss.\n        reduction: Keras loss reduction.\n        dtype: Dtype of the loss computation.\n    '

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

__module__ class-attribute

__module__ = 'keras_proba.layers.mixture_gaussian_head'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

Functions

gaussian_nll

gaussian_nll(y_true, y_pred)

Gaussian negative log-likelihood of y_true under a GaussianHead output.

Parameters:

Name Type Description Default
y_true

Targets of shape (..., units), or (batch,) when units=1.

required
y_pred

GaussianHead output of shape (..., 2 * units).

required

Returns:

Type Description

Negative log-likelihood averaged over the last axis, shape (...).

mixture_gaussian_nll

mixture_gaussian_nll(y_true, y_pred, units=None)

Negative log-likelihood of y_true under a MixtureGaussianHead output.

Parameters:

Name Type Description Default
y_true

Targets of shape (..., units), or (batch,) when units=1.

required
y_pred

MixtureGaussianHead output of shape (..., K * (1 + 2 * units)).

required
units

Number of regression targets. Inferred from y_true if None.

None

Returns:

Type Description

Negative log-likelihood of the joint units-dimensional target, shape

(...). Unlike gaussian_nll, it is summed (not averaged) over units,

as for tfd.Independent.