gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils#

Functions#

average_pos_neg_scores(scores, labels)

Computes the average positive and negative scores from scores and labels.

bpr_loss(scores, labels)

Computes Bayesian Personalized Ranking (BPR) loss.

hit_rate_at_k(scores, labels, ks)

Computes HitRate@K using pure tensor operations.

infonce_loss(scores, labels[, temperature])

Computes InfoNCE contrastive loss.

mean_reciprocal_rank(scores, labels)

Computes Mean Reciprocal Rank (MRR) using pure tensor operations.

Module Contents#

gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils.average_pos_neg_scores(scores, labels)[source]#

Computes the average positive and negative scores from scores and labels.

Parameters:
  • scores (torch.Tensor) – Score tensor.

  • labels (torch.Tensor) – Label tensor of corresponding shape. 1s indicate positive, 0s indicate negative.

Returns:

(avg_pos_score, avg_neg_score). These are scalars, and the tensors are detached from the computation graph since we don’t want to backprop.

Return type:

tuple[torch.Tensor, torch.Tensor]

gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils.bpr_loss(scores, labels)[source]#

Computes Bayesian Personalized Ranking (BPR) loss.

For each positive score s⁺ and its negatives s⁻₁, …, s⁻_K, we compute:

$$ mathcal{L}_text{BPR} = - frac{1}{BK} sum_{i=1}^B sum_{j=1}^K log sigma(s_i^+ - s_{ij}^-) $$

Parameters:
  • scores (torch.Tensor) – Score tensor of shape [B, 1 + K], where B is the batch size and K is the number of negatives. 1st column contains positive scores, and the rest are negative scores.

  • labels (torch.Tensor) – Label tensor of shape [B, 1 + K], where 1 indicates positive and 0 indicates negative. 1st column contains positive labels, and the rest are negative labels.

Returns:

Scalar BPR loss

Return type:

torch.Tensor

gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils.hit_rate_at_k(scores, labels, ks)[source]#

Computes HitRate@K using pure tensor operations.

HitRate@K is defined as:
[

ext{HitRate@K} =

rac{1}{N} sum_{i=1}^N mathbb{1}{ ext{positive in top-K} }

]

Args:
scores: Score tensor of shape [B, 1 + K], where B is the batch size and K is the number of negatives.

1st column contains positive scores, and the rest are negative scores.

labels: Label tensor of shape [B, 1 + K], where 1 indicates positive and 0 indicates negative.

1st column contains positive labels, and the rest are negative labels.

ks: An integer or list of integers indicating K values. Maximum K should be less than or equal

to the number of negatives + 1.

Returns:

A tensor (if one K) or dict of tensors (if multiple Ks), each giving HitRate@K.

Parameters:
  • scores (torch.Tensor)

  • labels (torch.Tensor)

  • ks (Union[int, list[int]])

Return type:

torch.Tensor

gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils.infonce_loss(scores, labels, temperature=1.0)[source]#

Computes InfoNCE contrastive loss.

We treat each group of (1 positive + K negatives) as a (1 + K)-way classification:

$$ mathcal{L}_text{InfoNCE} = - frac{1}{B} sum_{i=1}^B log

rac{exp(s_i^+ / tau)}{sum_{j=0}^{K} exp(s_{ij} / tau)}

$$

Args:
scores: Score tensor of shape [B, 1 + K], where B is the batch size and K is the number of negatives.

1st column contains positive scores, and the rest are negative scores.

labels: Label tensor of shape [B, 1 + K], where 1 indicates positive and 0 indicates negative.

1st column contains positive labels, and the rest are negative labels.

num_negatives: K, number of negatives per positive

Returns:

Scalar InfoNCE loss

Parameters:
  • scores (torch.Tensor)

  • labels (torch.Tensor)

  • temperature (float)

Return type:

torch.Tensor

gigl.experimental.knowledge_graph_embedding.lib.model.loss_utils.mean_reciprocal_rank(scores, labels)[source]#

Computes Mean Reciprocal Rank (MRR) using pure tensor operations.

MRR is defined as:
[

ext{MRR} =

rac{1}{N} sum_{i=1}^N rac{1}{ ext{rank}_i}

] where rank_i is the 1-based index of the positive in the sorted list.

Args:
scores: Score tensor of shape [B, 1 + K], where B is the batch size and K is the number of negatives.

1st column contains positive scores, and the rest are negative scores.

labels: Label tensor of shape [B, 1 + K], where 1 indicates positive and 0 indicates negative.

1st column contains positive labels, and the rest are negative labels.

Returns:

Scalar tensor with MRR.

Parameters:
  • scores (torch.Tensor)

  • labels (torch.Tensor)

Return type:

torch.Tensor