fusionlab.nn.losses.anomaly_loss

fusionlab.nn.losses.anomaly_loss(anomaly_scores, anomaly_loss_weight=1.0)[source]

Compute the anomaly loss based on given anomaly scores and a scaling weight.

The function returns a loss function callable that can be directly used in Keras model compilation and training workflows. The anomaly loss penalizes large anomaly scores, thereby guiding the model towards producing lower values when data points are considered normal.

Given anomaly scores \(a = [a_1, a_2, ..., a_n]\), the anomaly loss \(L\) is defined as:

\[L = w \cdot\]

rac{1}{n} sum_{i=1}^{n} a_i^{2}

where \(w\) is the anomaly_loss_weight, and \(n\) is the number of data points.

The model thus aims to reduce these anomaly scores, forcing representations or intermediate outputs to behave more normally according to its learned patterns.

anomaly_scorestf.Tensor or array-like

The anomaly scores reflecting the degree of abnormality in data points. Higher values indicate more unusual points. If provided as array-like, they will be converted into a tf.Tensor of type float32.

anomaly_loss_weightfloat, optional

A scaling factor controlling the influence of the anomaly loss on the overall training objective. Default is 1.0. Increasing this value places greater emphasis on reducing anomaly scores, encouraging the model to learn representations or predictions that minimize these values.

callable

A callable loss function with signature loss(y_true, y_pred) compatible with Keras. This returned function ignores y_true and focuses only on anomaly_scores, computing the mean of the tf_squared anomaly scores and scaling by anomaly_loss_weight.

>>> from fusionlab.nn.losses import anomaly_loss
>>> import tensorflow as tf
>>> anomaly_scores = tf.constant([0.1, 0.5, 2.0], dtype=tf.float32)
>>> loss_fn = anomaly_loss(anomaly_scores, anomaly_loss_weight=0.5)
>>> y_true_dummy = tf.zeros_like(anomaly_scores)
>>> y_pred_dummy = tf.zeros_like(anomaly_scores)
>>> loss_value = loss_fn(y_true_dummy, y_pred_dummy)
>>> print(loss_value.numpy())
1.4166666

In this example, the anomaly loss encourages the model to reduce the given anomaly scores.

  • The y_true and y_pred parameters are included for compatibility with Keras losses but are not utilized in the anomaly loss computation.

  • If anomaly_scores is provided as array-like, it is converted to float32 for consistency. If it is already a tensor, it is cast to float32 if needed.

tf.keras.losses.Loss() : Base class for all Keras losses. tf.tf_reduce_mean() : TensorFlow method for computing mean. tf.tf_square() : Squares tensor elements.