SelfAdversarialLoss¶
- class ampligraph.latent_features.SelfAdversarialLoss(loss_params={}, verbose=False)¶
Self Adversarial Sampling loss.
Introduced in [SDNT19].
\[\mathcal{L} = -log \left( \sigma(\gamma + f_{model} (\mathbf{s},\mathbf{o})) \right) - \sum_{i=1}^{n} p(h'_{i}, r, t'_{i} ) \cdot log \left( \sigma(-f_{model}(\mathbf{s}'_{i},\mathbf{o}'_{i}) - \gamma) \right)\]where \(\mathbf{s}, \mathbf{o} \in \mathcal{R}^k\) are the embeddings of the subject and object of a triple \(t=(s,r,o)\), \(\gamma \in \mathbb{R}\) is the margin, \(\sigma\) the sigmoid function, and \(p(s'_{i}, r, o'_{i})\) is the negatives sampling distribution which is defined as:
\[p(s'_j, r, o'_j | \{(s_i, r_i, o_i)\}) = \frac{\exp \left( \alpha \, f_{model}(\mathbf{s'_j}, \mathbf{o'_j}) \right)} {\sum_i \exp \left( \alpha \, f_{model}(\mathbf{s'_i}, \mathbf{o'_i}) \right)}\]where \(\alpha\) is the temperature of sampling and \(f_{model}\) is the scoring function of the desired embedding model.
Example
>>> import ampligraph.latent_features.loss_functions as lfs >>> loss = lfs.SelfAdversarialLoss({'margin': 1, 'alpha': 0.1, 'reduction': 'mean'}) >>> isinstance(loss, lfs.SelfAdversarialLoss) True
>>> loss = lfs.get('self_adversarial') >>> isinstance(loss, lfs.SelfAdversarialLoss) True
Attributes
external_paramsnameMethods
__init__([loss_params, verbose])Initialize the loss.
- __init__(loss_params={}, verbose=False)¶
Initialize the loss.
- Parameters:
loss_params (dict) –
Dictionary of loss-specific hyperparams:
”margin”: (float) - Margin to be used for loss computation (default: 1).
”alpha”: (float) - Temperature of sampling (default: 0.5).
”reduction”: (str) - Specifies whether to “sum” or take the “mean” of the loss per sample w.r.t. corruption (default: “sum”).
Example: loss_params={‘margin’: 1, ‘alpha’: 0.5}.