PairwiseHingeLoss

class KGE.loss.PairwiseHingeLoss[source]

Bases: KGE.loss.Loss

An implementation of Pairwise Hinge Loss / Margin Ranking Loss.

Pairwise Hinge Loss or Margin Ranking Loss is a common loss function that used in many models such as UM, SE, TransE, TransH, TransR, TransD, DistMult.

For each pair of postive triplet \((h,r,t)_i^+\) and negative triplet \((h,r,t)_i^-\), Pairwise Hinge Loss compare the difference of scores between postivie triplet and negative triplet:

\[\Delta_i = f\left( (h,r,t)_i^- \right) - f\left( (h,r,t)_i^+ \right)\]

Since the socre of triplet \(f(h,r,t)\) measures how plausible \((h,r,t)\) is, so \(\Delta_i < 0\) is favorable. If the difference \(\Delta_i\) does not execeed the given margin \(\gamma\), Pairwise Hinge Loss penalize this pair:

\[\mathscr{L} = \sum_i max \left( 0, \gamma + \Delta_i \right)\]

Methods Summary

__call__(pos_score, neg_score)

Calculate loss.

Methods Documentation

__call__(pos_score, neg_score)[source]

Calculate loss.

Parameters
  • pos_score (tf.Tensor) – score of postive triplets, with shape (n,)

  • neg_score (tf.Tensor) – score of negative triplets, with shape (n,)

__init__(margin)[source]

Initialize loss

__new__(*args, **kwargs)