fusionlab.nn.components.AnomalyLoss

class fusionlab.nn.components.AnomalyLoss[source]

Bases: Loss, NNLearner

Anomaly Loss layer computing mean squared anomaly scores.

This layer expects anomaly scores of shape \((B, H, D)\) and multiplies their mean squared value by a weight factor.

\[\text{AnomalyLoss}(\mathbf{a}) = w \cdot \frac{1}{BHD} \sum (\mathbf{a})^2\]

where \(\mathbf{a}\) is the anomaly score, and \(w\) is the weight.

Parameters:

weight (float, optional) – Scalar multiplier for the computed mean squared anomaly scores. Defaults to 1.0.

Notes

Anomaly loss is often combined with other losses in a multi-task setting where predictive performance and anomaly detection performance are both important.

call(`anomaly_scores`)[source]

Compute mean squared anomaly loss.

Parameters:

anomaly_scores (Tensor)

get_config()[source]

Return configuration for serialization.

from_config(`config`)[source]

Instantiates a new instance from config.

Examples

>>> from fusionlab.nn.components import AnomalyLoss
>>> import tensorflow as tf
>>> # Suppose anomaly_scores is (B, H, D)
>>> anomaly_scores = tf.random.normal((32, 10, 8))
>>> # Instantiate anomaly loss
>>> anomaly_loss_fn = AnomalyLoss(weight=2.0)
>>> # Compute anomaly loss
>>> loss_value = anomaly_loss_fn(anomaly_scores)

See also

AdaptiveQuantileLoss

Another specialized loss that can be combined for multi-objective optimization.

MultiObjectiveLoss

Demonstrates how anomaly and quantile loss can be merged.

References

__init__(weight=1.0, name='AnomalyLoss')[source]
Parameters:

weight (float)

Methods

__init__([weight, name])

call(anomaly_scores[, y_pred])

Forward pass that computes the mean squared anomaly score multiplied by weight.

from_config(config)

Recreates an AnomalyLoss layer from a config.

get_config()

Return configuration dictionary for this layer.

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

dtype

my_params

__init__(weight=1.0, name='AnomalyLoss')[source]
Parameters:

weight (float)

call(anomaly_scores, y_pred=None)[source]

Forward pass that computes the mean squared anomaly score multiplied by weight.

Parameters:
  • anomaly_scores (tf.Tensor) – Tensor of shape (B, H, D) representing anomaly scores.

  • y_pred (Optional) – Does nothing, just for API consistency.

Returns:

A scalar loss value representing the weighted mean squared anomaly.

Return type:

tf.Tensor

get_config()[source]

Return configuration dictionary for this layer.

Returns:

Includes ‘weight’.

Return type:

dict

classmethod from_config(config)[source]

Recreates an AnomalyLoss layer from a config.

Parameters:

config (dict) – Configuration containing ‘weight’.

Returns:

A new instance of this layer.

Return type:

AnomalyLoss

help(**kwargs)
my_params = AnomalyLoss(weight=1.0, name='AnomalyLoss')