Skip to content
DeepForgeby svx
All problems
MediumGraph Algorithmsgraph-202

Contrastive Graph Loss Lite

Problem

Compute a contrastive graph loss over aligned positive and negative pairs: -log(sigmoid((pos - neg) / temperature)).

pos_scores and neg_scores are equal-length lists of similarity scores. Return the mean loss over pairs, or 0.0 when the lists are empty.

Hint

The loss shrinks as positive scores pull ahead of negative scores.

Test cases

case 1
in: [[0.9,0.7],[0.1,0.3],1]
expected: 0.44205795917386514
case 2
in: [[0.5],[0.5],2]
expected: 0.6931471805599453
case 3
in: [[],[],1]
expected: 0
Starter codePython
import math
def contrastive_graph_loss(pos_scores, neg_scores, temperature):
    # Your code here
    pass

3 test cases · run your code against them in the editor