SelfAdversarialNegativeSamplingLoss

class KGE.loss.SelfAdversarialNegativeSamplingLoss[source]

Bases: KGE.loss.Loss

An implementation of Self Adversarial Negative Sampling Loss.

Described in RotatE: Knowledge Graph Embedding by Relational Rotation in Complex Space.

Self Adversarial Negative Sampling Loss samples negative triples according to the current embedding model. Specifically, it sample negative triples from the following distribution:

\[p\left((h,r,t)_{i,j}^- \vert (h,r,t)_i^+ \right) = \frac{exp~ \alpha f((h,r,t)_{i.j}^-)}{\sum_k exp~ \alpha f((h,r,t)_{i,k}^-)}\]

where \((h,r,t)_i^+\) denotes i-th positive triplet, \((h,r,t)_{i,j}^-\) denotes j-th negative triplet generate from i-th positive triplet, \(\alpha\) is the temperature of sampling.

Since the sampling procedure may be costly, Self Adversarial Negative Sampling Loss treats the above probability as the weight of the negative sample. Therefore, the final negative sampling loss with self-adversarial training takes the following form:

\[\mathscr{L} = - \sum_i log~ \sigma(\gamma + f((h,r,t)_i^+)) - \sum_i \sum_j p\left( (h,r,t)_{i,j}^- \right) log~ \sigma(-\gamma - f((h,r,t)_{i,j}^-))\]

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, temperature)[source]

Initialize loss

__new__(*args, **kwargs)