merlin.models.tf.losses.TOP1v2Loss

class merlin.models.tf.losses.TOP1v2Loss(reduction='auto', name=None)[source]

Bases: merlin.models.utils.registry.RegistryMixin[LossRegistryMixin]

An adapted version of the TOP pairwise loss proposed in [1]_, but following the current GRU4Rec implementation 2.

References: .. [1] B. Hidasi, A. Karatzoglou, L. Baltrunas, and D. Tikk, “Session-based recommendations

with recurrent neural networks,” in Proceedings of Fourth International Conference on Learning Representations (ICLR’16), 2016.

2

https://arxiv.org/abs/1511.06939

__init__(reduction='auto', name=None)

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 with tf.distribute.Strategy, outside of built-in training loops such as tf.keras compile and fit, using AUTO or SUM_OVER_BATCH_SIZE will raise an error. Please see this custom training [tutorial](

  • name – Optional name for the instance.

Methods

__init__([reduction, name])

Initializes Loss class.

call(y_true, y_pred)

Loss computation.

compute(positives_scores, negatives_scores)

Computes the loss

from_config(config)

Instantiates a Loss from its config (output of get_config()).

get_config()

Returns the config dictionary for a Loss instance.

parse(class_or_str)

Attributes

registry

valid_negatives_mask

compute(positives_scores: tensorflow.python.framework.ops.Tensor, negatives_scores: tensorflow.python.framework.ops.Tensor)tensorflow.python.framework.ops.Tensor[source]

Computes the loss

Parameters
  • positives_scores (tf.Tensor) – Prediction scores for the positive items (batch size x 1)

  • negatives_scores (tf.Tensor) – Prediction scores for the positive items (batch size x number negative samples)

Returns

Loss per negative sample

Return type

tf.Tensor (batch size x number negative samples)