# Model

class Model(*, triples_factory, loss=None, loss_kwargs=None, predict_with_sigmoid=False, random_seed=None)[source]

Bases: torch.nn.modules.module.Module, abc.ABC

A base module for KGE models.

Subclasses of Model can decide however they want on how to store entities’ and relations’ representations, how they want to be looked up, and how they should be scored. The OModel provides a commonly used interface for models storing entity and relation representations in the form of pykeen.nn.Embedding.

Initialize the module.

Parameters
• triples_factory (KGInfo) – The triples factory facilitates access to the dataset.

• loss (Union[str, Loss, Type[Loss], None]) – The loss to use. If None is given, use the loss default specific to the model subclass.

• predict_with_sigmoid (bool) – Whether to apply sigmoid onto the scores when predicting scores. Applying sigmoid at prediction time may lead to exactly equal scores for certain triples with very high, or very low score. When not trained with applying sigmoid (or using BCEWithLogitsLoss), the scores are not calibrated to perform well with sigmoid.

• random_seed (Optional[int]) – A random seed to use for initialising the model’s weights. Should be set when aiming at reproducibility.

Attributes Summary

 device Return the model's device. loss_default_kwargs The default parameters for the default loss function class num_parameter_bytes Calculate the number of bytes used for all parameters of the model. num_parameters Calculate the number of parameters of the model. num_real_relations Return the real number of relations (without inverses).

Methods Summary

 Get the regularization term for the loss function. get_all_prediction_df(*[, k, batch_size]) Compute scores for all triples, optionally returning only the k highest scoring. Get the parameters that require gradients. get_head_prediction_df(relation_label, ...) Predict heads for the given relation and tail (given by label). get_relation_prediction_df(head_label, ...) Predict relations for the given head and tail (given by label). get_tail_prediction_df(head_label, ...) Predict tails for the given head and relation (given by label). load_state(path) Load the state of the model. Run after calculating the forward loss. Has to be called after each parameter update. predict(hrt_batch, target, *[, slice_size]) Predict scores for the given target. predict_h(rt_batch, *[, slice_size, mode]) Forward pass using left side (head) prediction for obtaining scores of all possible heads. predict_hrt(hrt_batch, *[, mode]) Calculate the scores for triples. predict_r(ht_batch, *[, slice_size]) Forward pass using middle (relation) prediction for obtaining scores of all possible relations. predict_t(hr_batch, *[, slice_size, mode]) Forward pass using right side (tail) prediction for obtaining scores of all possible tails. Reset all parameters of the model and enforce model constraints. save_state(path) Save the state of the model. score_h(rt_batch, *[, slice_size, mode]) Forward pass using left side (head) prediction. score_h_inverse(rt_batch, *[, slice_size]) Score all heads for a batch of (r,t)-pairs using the tail predictions for the inverses $$(t,r_{inv},*)$$. score_hrt(hrt_batch, *[, mode]) Forward pass. score_hrt_inverse(hrt_batch, *, mode) Score triples based on inverse triples, i.e., compute $$f(h,r,t)$$ based on $$f(t,r_{inv},h)$$. score_r(ht_batch, *[, slice_size, mode]) Forward pass using middle (relation) prediction. score_t(hr_batch, *[, slice_size, mode]) Forward pass using right side (tail) prediction. score_t_inverse(hr_batch, *[, slice_size]) Score all tails for a batch of (h,r)-pairs using the head predictions for the inverses $$(*,r_{inv},h)$$.

Attributes Documentation

device

Return the model’s device.

Return type

device

loss_default_kwargs: ClassVar[Optional[Mapping[str, Any]]] = {'margin': 1.0, 'reduction': 'mean'}

The default parameters for the default loss function class

num_parameter_bytes

Calculate the number of bytes used for all parameters of the model.

Return type

int

num_parameters

Calculate the number of parameters of the model.

Return type

int

num_real_relations

Return the real number of relations (without inverses).

Return type

int

Methods Documentation

abstract collect_regularization_term()[source]

Get the regularization term for the loss function.

Return type

FloatTensor

get_all_prediction_df(*, k=None, batch_size=1, **kwargs)[source]

Compute scores for all triples, optionally returning only the k highest scoring.

Note

This operation is computationally very expensive for reasonably-sized knowledge graphs.

Warning

Setting k=None may lead to huge memory requirements.

Parameters
Return type

Union[ScorePack, DataFrame]

Returns

shape: (k, 3) A tensor containing the k highest scoring triples, or all possible triples if k=None.

Get the parameters that require gradients.

Return type

Predict heads for the given relation and tail (given by label).

Parameters

The following example shows that after you train a model on the Nations dataset, you can score all entities w.r.t a given relation and tail entity.

>>> from pykeen.pipeline import pipeline
>>> result = pipeline(
...     dataset='Nations',
...     model='RotatE',
... )
>>> df = result.model.get_head_prediction_df('accusation', 'brazil', triples_factory=result.training)

Return type

DataFrame

Predict relations for the given head and tail (given by label).

Parameters
Return type

DataFrame

Predict tails for the given head and relation (given by label).

Parameters

The following example shows that after you train a model on the Nations dataset, you can score all entities w.r.t a given head entity and relation.

>>> from pykeen.pipeline import pipeline
>>> result = pipeline(
...     dataset='Nations',
...     model='RotatE',
... )
>>> df = result.model.get_tail_prediction_df('brazil', 'accusation', triples_factory=result.training)

Return type

DataFrame

Load the state of the model.

Parameters

path (Union[str, PathLike]) – Path of the file where to load the state from.

Return type

None

post_forward_pass()[source]

Run after calculating the forward loss.

post_parameter_update()[source]

Has to be called after each parameter update.

Return type

None

predict(hrt_batch, target, *, slice_size=None, mode)[source]

Predict scores for the given target.

Return type

FloatTensor

predict_h(rt_batch, *, slice_size=None, mode=None)[source]

Forward pass using left side (head) prediction for obtaining scores of all possible heads.

This method calculates the score for all possible heads for each (relation, tail) pair.

Note

If the model has been trained with inverse relations, the task of predicting the head entities becomes the task of predicting the tail entities of the inverse triples, i.e., $$f(*,r,t)$$ is predicted by means of $$f(t,r_{inv},*)$$.

Additionally, the model is set to evaluation mode.

Parameters
• rt_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (relation, tail) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode. Is None for transductive and “training” / “validation” / “testing” in inductive.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities), dtype: float For each r-t pair, the scores for all possible heads.

predict_hrt(hrt_batch, *, mode=None)[source]

Calculate the scores for triples.

This method takes head, relation and tail of each triple and calculates the corresponding score.

Additionally, the model is set to evaluation mode.

Parameters
• hrt_batch (LongTensor) – shape: (number of triples, 3), dtype: long The indices of (head, relation, tail) triples.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode. Is None for transductive and “training” / “validation” / “testing” in inductive.

Return type

FloatTensor

Returns

shape: (number of triples, 1), dtype: float The score for each triple.

predict_r(ht_batch, *, slice_size=None, mode)[source]

Forward pass using middle (relation) prediction for obtaining scores of all possible relations.

This method calculates the score for all possible relations for each (head, tail) pair.

Additionally, the model is set to evaluation mode.

Parameters
• ht_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (head, tail) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode. Is None for transductive and “training” / “validation” / “testing” in inductive.

Return type

FloatTensor

Returns

shape: (batch_size, num_real_relations), dtype: float For each h-t pair, the scores for all possible relations.

predict_t(hr_batch, *, slice_size=None, mode=None)[source]

Forward pass using right side (tail) prediction for obtaining scores of all possible tails.

This method calculates the score for all possible tails for each (head, relation) pair.

Additionally, the model is set to evaluation mode.

Parameters
• hr_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (head, relation) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode. Is None for transductive and “training” / “validation” / “testing” in inductive.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities), dtype: float For each h-r pair, the scores for all possible tails.

Note

We only expect the right side-predictions, i.e., $$(h,r,*)$$ to change its default behavior when the model has been trained with inverse relations (mainly because of the behavior of the LCWA training approach). This is why the predict_h() has different behavior depending on if inverse triples were used in training, and why this function has the same behavior regardless of the use of inverse triples.

reset_parameters_()[source]

Reset all parameters of the model and enforce model constraints.

save_state(path)[source]

Save the state of the model.

Parameters

path (Union[str, PathLike]) – Path of the file where to store the state in.

Return type

None

abstract score_h(rt_batch, *, slice_size=None, mode=None)[source]

Forward pass using left side (head) prediction.

This method calculates the score for all possible heads for each (relation, tail) pair.

Parameters
• rt_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (relation, tail) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities), dtype: float For each r-t pair, the scores for all possible heads.

score_h_inverse(rt_batch, *, slice_size=None, mode)[source]

Score all heads for a batch of (r,t)-pairs using the tail predictions for the inverses $$(t,r_{inv},*)$$.

abstract score_hrt(hrt_batch, *, mode=None)[source]

Forward pass.

This method takes head, relation and tail of each triple and calculates the corresponding score.

Parameters
• hrt_batch (LongTensor) – shape: (batch_size, 3), dtype: long The indices of (head, relation, tail) triples.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

Return type

FloatTensor

Returns

shape: (batch_size, 1), dtype: float The score for each triple.

score_hrt_inverse(hrt_batch, *, mode)[source]

Score triples based on inverse triples, i.e., compute $$f(h,r,t)$$ based on $$f(t,r_{inv},h)$$.

When training with inverse relations, the model produces two (different) scores for a triple $$(h,r,t) \in K$$. The forward score is calculated from $$f(h,r,t)$$ and the inverse score is calculated from $$f(t,r_{inv},h)$$. This function enables users to inspect the scores obtained by using the corresponding inverse triples.

Return type

FloatTensor

abstract score_r(ht_batch, *, slice_size=None, mode=None)[source]

Forward pass using middle (relation) prediction.

This method calculates the score for all possible relations for each (head, tail) pair.

Parameters
• ht_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (head, tail) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

Return type

FloatTensor

Returns

shape: (batch_size, num_real_relations), dtype: float For each h-t pair, the scores for all possible relations.

abstract score_t(hr_batch, *, slice_size=None, mode=None)[source]

Forward pass using right side (tail) prediction.

This method calculates the score for all possible tails for each (head, relation) pair.

Parameters
• hr_batch (LongTensor) – shape: (batch_size, 2), dtype: long The indices of (head, relation) pairs.

• slice_size (Optional[int]) – >0 The divisor for the scoring function when using slicing.

• mode (Optional[Literal[‘training’, ‘validation’, ‘testing’]]) – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.

Return type

FloatTensor

Returns

shape: (batch_size, num_entities), dtype: float For each h-r pair, the scores for all possible tails.

score_t_inverse(hr_batch, *, slice_size=None, mode)[source]

Score all tails for a batch of (h,r)-pairs using the head predictions for the inverses $$(*,r_{inv},h)$$.