fusionlab.nn.components.AnomalyLoss¶
- class fusionlab.nn.components.AnomalyLoss[source]¶
Bases:
Loss,NNLearnerAnomaly 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)
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
AdaptiveQuantileLossAnother specialized loss that can be combined for multi-objective optimization.
MultiObjectiveLossDemonstrates how anomaly and quantile loss can be merged.
References
- __init__(weight=1.0, name='AnomalyLoss')[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.
weight (float)
Methods
__init__([weight, name])Initializes Loss class.
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.
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
- __init__(weight=1.0, name='AnomalyLoss')[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.
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:
- help(**kwargs)¶
- my_params = AnomalyLoss(weight=1.0, name='AnomalyLoss')¶