PairwiseLogisticLoss
classkeras_rs.losses.PairwiseLogisticLoss(temperature: float = 1.0, **kwargs: Any)
Computes pairwise logistic loss between true labels and predicted scores. This loss function is designed for ranking tasks, where the goal is to correctly order items within each list. It computes the loss by comparing pairs of items within each list, penalizing cases where an item with a higher true label has a lower predicted score than an item with a lower true label.
For each list of predicted scores s
in y_pred
and the corresponding list
of true labels y
in y_true
, the loss is computed as follows:
loss = sum_{i} sum_{j} I(y_i > y_j) * log(1 + exp(-(s_i - s_j)))
where:
y_i
and y_j
are the true labels of items i
and j
, respectively.s_i
and s_j
are the predicted scores of items i
and j
,
respectively.I(y_i > y_j)
is an indicator function that equals 1 if y_i > y_j
,
and 0 otherwise.log(1 + exp(-(s_i - s_j)))
is the logistic loss, which penalizes
cases where the score difference s_i - s_j
is not sufficiently large
when y_i > y_j
. This function provides a smooth approximation of the
ideal step function, making it suitable for gradient-based optimization.Arguments
"sum_over_batch_size"
. Supported options are
"sum"
, "sum_over_batch_size"
, "mean"
,
"mean_with_sample_weight"
or None
. "sum"
sums the loss,
"sum_over_batch_size"
and "mean"
sum the loss and divide by the
sample size, and "mean_with_sample_weight"
sums the loss and
divides by the sum of the sample weights. "none"
and None
perform no aggregation. Defaults to "sum_over_batch_size"
.None
, which
means using keras.backend.floatx()
. keras.backend.floatx()
is a
"float32"
unless set to different value
(via keras.backend.set_floatx()
). If a keras.DTypePolicy
is
provided, then the compute_dtype
will be utilized.Examples
With compile()
API:
model.compile(
loss=keras_rs.losses.PairwiseLogisticLoss(),
...
)
As a standalone function with unbatched inputs:
>>> y_true = np.array([1.0, 0.0, 1.0, 3.0, 2.0])
>>> y_pred = np.array([1.0, 3.0, 2.0, 4.0, 0.8])
>>> pairwise_logistic_loss = keras_rs.losses.PairwiseLogisticLoss()
>>> pairwise_logistic_loss(y_true=y_true, y_pred=y_pred)
>>> 1.70708
With batched inputs using default 'auto'/'sum_over_batch_size' reduction:
>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_logistic_loss = keras_rs.losses.PairwiseLogisticLoss()
>>> pairwise_logistic_loss(y_true=y_true, y_pred=y_pred)
0.73936
With masked inputs (useful for ragged inputs):
>>> y_true = {
... "labels": np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]]),
... "mask": np.array(
... [[True, True, True, True], [True, True, False, False]]
... ),
... }
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_logistic_loss(y_true=y_true, y_pred=y_pred)
0.53751
With sample_weight
:
>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> sample_weight = np.array(
... [[2.0, 3.0, 1.0, 1.0], [2.0, 1.0, 0.0, 0.0]]
... )
>>> pairwise_logistic_loss = keras_rs.losses.PairwiseLogisticLoss()
>>> pairwise_logistic_loss(
... y_true=y_true, y_pred=y_pred, sample_weight=sample_weight
... )
>>> 0.80337
Using 'none'
reduction:
>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_logistic_loss = keras_rs.losses.PairwiseLogisticLoss(
... reduction="none"
... )
>>> pairwise_logistic_loss(y_true=y_true, y_pred=y_pred)
[[2.126928, 0., 1.3132616, 0.48877698], [0., 0.20000005, 0.79999995, 0.]]
call
methodPairwiseLogisticLoss.call(y_true: Any, y_pred: Any)
Compute the pairwise loss.
Arguments
(list_size)
for unbatched inputs or (batch_size, list_size)
for batched inputs. If an item has a label of -1, it is ignored
in loss computation. If it is a dictionary, it should have two
keys: "labels"
and "mask"
. "mask"
can be used to ignore
elements in loss computation, i.e., pairs will not be formed
with those items. Note that the final mask is an and
of the
passed mask, and labels >= 0
.(list_size)
for
unbatched inputs or (batch_size, list_size)
for batched
inputs. Should be of the same shape as y_true
.Returns
The loss.