Losses

RelevanceLossBase

class ml4ir.base.model.losses.loss_base.RelevanceLossBase(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)

Bases: keras.engine.base_layer.Layer

Abstract class that defines the loss and final activation function used to train a RelevanceModel

call(inputs, y_true, y_pred, training=None)

Compute the loss using predicted probabilities and expected labels

Parameters:
  • inputs (dict of dict of tensors) – Dictionary of input feature tensors
  • y_true (tensor) – True labels
  • y_pred (tensor) – Predicted scores
  • training (boolean) – Boolean indicating whether the layer is being used in training mode
Returns:

Resulting loss tensor after applying comparing the y_pred and y_true values

Return type:

tensor

final_activation_op(inputs, training=None)

Final activation layer that is applied to the logits tensor to get the scores

Parameters:
  • inputs (dict of dict of tensors) – Dictionary of input feature tensors with scores
  • training (boolean) – Boolean indicating whether the layer is being used in training mode
Returns:

Resulting score tensor after applying the function on the logits

Return type:

tensor

get_config()

Return layer config that is used while serialization

SigmoidCrossEntropy

class ml4ir.applications.ranking.model.losses.pointwise_losses.SigmoidCrossEntropy(loss_key='pointwise', scoring_type='', output_name='score', **kwargs)

Bases: ml4ir.applications.ranking.model.losses.loss_base.PointwiseLossBase

call(inputs, y_true, y_pred, training=None)

Get the sigmoid cross entropy loss Additionally can pass in record positions to handle positional bias

Parameters:
  • inputs (dict of dict of tensors) – Dictionary of input feature tensors
  • y_true (tensor) – True labels
  • y_pred (tensor) – Predicted scores
  • training (boolean) – Boolean indicating whether the layer is being used in training mode
Returns:

Scalar sigmoid cross entropy loss tensor

Return type:

tensor

Notes

Uses mask field to exclude padded records from contributing to the loss

final_activation_op(inputs, training=None)

Get sigmoid activated scores on logits

Parameters:inputs (dict of dict of tensors) – Dictionary of input feature tensors
Returns:sigmoid activated scores
Return type:tensor

RankOneListNet

class ml4ir.applications.ranking.model.losses.listwise_losses.RankOneListNet(loss_key: str = 'rank_one_listnet', scoring_type: str = 'listwise', output_name: str = 'score', **kwargs)

Bases: ml4ir.applications.ranking.model.losses.listwise_losses.SoftmaxCrossEntropy

Parameters:
  • loss_key (str) – Name of the loss function as specified by LossKey
  • scoring_type (str) – Type of scoring function - pointwise, pairwise, groupwise
  • output_name (str) – Name of the output node for the predicted scores
call(inputs, y_true, y_pred, training=None)

Define a masked rank 1 ListNet loss. This loss is useful for multi-label classification when we have multiple click labels per document. This is because the loss breaks down the comparison between y_pred and y_true into individual binary assessments. Ref -> https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-2007-40.pdf

Parameters:
  • inputs (dict of dict of tensors) – Dictionary of input feature tensors
  • y_true (tensor) – True labels
  • y_pred (tensor) – Predicted scores
  • training (boolean) – Boolean indicating whether the layer is being used in training mode
Returns:

Scalar sigmoid cross entropy loss tensor

Return type:

tensor

Notes

Uses mask field to exclude padded records from contributing to the loss

CategoricalCrossEntropy

class ml4ir.applications.classification.model.losses.categorical_cross_entropy.CategoricalCrossEntropy(output_name, **kwargs)

Bases: ml4ir.base.model.losses.loss_base.RelevanceLossBase

Initialize categorical cross entropy loss

Parameters:output_name (str) – Name of the output node after final activation op
call(inputs, y_true, y_pred, training=None)

Define a categorical cross entropy loss

Parameters:
  • inputs (dict of dict of tensors) – Dictionary of input feature tensors
  • y_true (tensor) – True labels
  • y_pred (tensor) – Predicted scores
  • training (boolean) – Boolean indicating whether the layer is being used in training mode
Returns:

Categorical cross entropy loss

Return type:

function

final_activation_op(inputs, training=None)

Get softmax activated scores on logits

Parameters:inputs (dict of dict of tensors) – Dictionary of input feature tensors
Returns:Softmax activated scores
Return type:tensor
get_config()

Return layer config that is used while serialization