fusionlab.nn.components.MultiObjectiveLoss

class fusionlab.nn.components.MultiObjectiveLoss[source]

Bases: Loss, NNLearner

Multi-Objective Loss layer combining quantile loss and anomaly loss [1].

This layer expects: 1. y_true: \((B, H, O)\) 2. y_pred: \((B, H, Q, O)\), if

quantiles are used (or (B, H, 1, O) for a single quantile).

  1. anomaly_scores: \((B, H, D)\), optional.

\[\text{Loss} = \text{QuantileLoss} + \text{AnomalyLoss}\]

If anomaly_scores is None, only quantile loss is returned.

Parameters:
  • quantile_loss_fn (Layer) – A callable implementing quantile loss, e.g. AdaptiveQuantileLoss.

  • anomaly_loss_fn (Layer) – A callable implementing anomaly loss, e.g. AnomalyLoss.

Notes

This layer allows multi-task learning by combining two objectives: forecasting accuracy (quantile loss) and anomaly detection (anomaly loss).

call(`y_true`, `y_pred`, `anomaly_scores`=None,

training=False)

Compute the combined loss.

get_config()[source]

Returns configuration for serialization.

from_config(`config`)[source]

Rebuilds the layer from a config dict.

Examples

>>> from fusionlab.nn.components import (
...     MultiObjectiveLoss,
...     AdaptiveQuantileLoss,
...     AnomalyLoss
... )
>>> import tensorflow as tf
>>> # Suppose y_true is (B, H, O),
... # and y_pred is (B, H, Q, O).
>>> y_true = tf.random.normal((32, 10, 1))
>>> y_pred = tf.random.normal((32, 10, 3, 1))
>>> anomaly_scores = tf.random.normal((32, 10, 8))
>>> # Instantiate loss components
>>> q_loss_fn = AdaptiveQuantileLoss([0.2, 0.5, 0.8])
>>> a_loss_fn = AnomalyLoss(weight=2.0)
>>> # Combine them
>>> mo_loss = MultiObjectiveLoss(q_loss_fn, a_loss_fn)
>>> # Compute the combined loss
>>> total_loss = mo_loss(y_true, y_pred, anomaly_scores)

See also

AdaptiveQuantileLoss

Implements quantile loss to handle uncertainty in predictions.

AnomalyLoss

Computes anomaly-based MSE for anomaly detection tasks.

References

__init__(quantile_loss_fn, anomaly_loss_fn, anomaly_scores=None, name='MultiObjectiveLoss')[source]

Initializes Loss class.

Parameters:
  • reduction – Type of tf.keras.losses.Reduction to apply to loss. Default value is AUTO. AUTO indicates that the reduction option will be determined by the usage context. For almost all cases this defaults to SUM_OVER_BATCH_SIZE. When used under a tf.distribute.Strategy, except via Model.compile() and Model.fit(), using AUTO or SUM_OVER_BATCH_SIZE will raise an error. Please see this custom training [tutorial]( https://www.tensorflow.org/tutorials/distribute/custom_training) for more details.

  • name – Optional name for the instance.

Methods

__init__(quantile_loss_fn, anomaly_loss_fn)

Initializes Loss class.

call(y_true, y_pred)

Compute combined quantile and anomaly loss.

from_config(config)

Creates a new MultiObjectiveLoss from the config dictionary.

get_config()

Returns configuration dictionary, including configs of the sub-layers.

get_params([deep])

Get the parameters for this learner.

help(**kwargs)

load(file_path[, format])

Load the learner's state from a specified file in the desired format.

save([file_path, format, overwrite, ...])

Save the learner's state to a specified file in the desired format.

set_params(**params)

Set the parameters of this learner.

summary()

Provide a summary of the learner's parameters.

Attributes

__init__(quantile_loss_fn, anomaly_loss_fn, anomaly_scores=None, name='MultiObjectiveLoss')[source]

Initializes Loss class.

Parameters:
  • reduction – Type of tf.keras.losses.Reduction to apply to loss. Default value is AUTO. AUTO indicates that the reduction option will be determined by the usage context. For almost all cases this defaults to SUM_OVER_BATCH_SIZE. When used under a tf.distribute.Strategy, except via Model.compile() and Model.fit(), using AUTO or SUM_OVER_BATCH_SIZE will raise an error. Please see this custom training [tutorial]( https://www.tensorflow.org/tutorials/distribute/custom_training) for more details.

  • name – Optional name for the instance.

call(y_true, y_pred)[source]

Compute combined quantile and anomaly loss.

Parameters:
  • y_true (tf.Tensor) – Ground truth of shape (B, H, O).

  • y_pred (tf.Tensor) – Predictions of shape (B, H, Q, O) (or (B, H, 1, O) if Q=1).

  • anomaly_scores (tf.Tensor or None, optional) – Tensor of shape (B, H, D). If None, anomaly loss is omitted.

  • training (bool, optional) – Indicates training mode. Defaults to False.

Returns:

A scalar representing the sum of quantile loss and anomaly loss (if anomaly_scores is provided).

Return type:

tf.Tensor

get_config()[source]

Returns configuration dictionary, including configs of the sub-layers.

Returns:

Contains serialized configs of quantile_loss_fn and anomaly_loss_fn.

Return type:

dict

classmethod from_config(config)[source]

Creates a new MultiObjectiveLoss from the config dictionary.

Parameters:

config (dict) – Configuration dictionary with sub-layer configs.

Returns:

A new instance combining quantile and anomaly losses.

Return type:

MultiObjectiveLoss

help(**kwargs)
my_params = MultiObjectiveLoss(quantile_loss_fn, anomaly_loss_fn, anomaly_scores=None, name='MultiObjectiveLoss')