ListMLELoss classkeras_rs.losses.ListMLELoss(temperature: float = 1.0, **kwargs: Any)
Implements ListMLE (Maximum Likelihood Estimation) loss for ranking.
ListMLE loss is a listwise ranking loss that maximizes the likelihood of the ground truth ranking. It works by: 1. Sorting items by their relevance scores (labels) 2. Computing the probability of observing this ranking given the predicted scores 3. Maximizing this likelihood (minimizing negative log-likelihood)
The loss is computed as the negative log-likelihood of the ground truth ranking given the predicted scores:
loss = -sum(log(exp(s_i) / sum(exp(s_j) for j >= i)))
where s_i is the predicted score for item i in the sorted order.
Arguments
"sum_over_batch_size". Supported options are
"sum", "sum_over_batch_size", "mean",
"mean_with_sample_weight" or None. Defaults to
"sum_over_batch_size".None.Examples
# Basic usage
loss_fn = ListMLELoss()
# With temperature scaling
loss_fn = ListMLELoss(temperature=0.5)
# Example with synthetic data
y_true = [[3, 2, 1, 0]] # Relevance scores
y_pred = [[0.8, 0.6, 0.4, 0.2]] # Predicted scores
loss = loss_fn(y_true, y_pred)
call methodListMLELoss.call(y_true: Any, y_pred: Any)
Compute the ListMLE 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.(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 tensor of shape [batch_size].