_OldAbstractModel

class _OldAbstractModel(triples_factory, loss=None, predict_with_sigmoid=False, preferred_device=None, random_seed=None, regularizer=None)[source]

Bases: pykeen.models.base.Model, abc.ABC

A base module for PyKEEN 1.0-style KGE models.

Initialize the module.

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

  • loss (Optional[Loss]) – 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.

  • preferred_device (Union[str, device, None]) – The preferred device for model training and inference.

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

  • regularizer (Optional[Regularizer]) – A regularizer to use for training.

Attributes Summary

regularizer_default

The default regularizer class

regularizer_default_kwargs

The default parameters for the default regularizer class

Methods Summary

collect_regularization_term()

Get the regularization term for the loss function.

post_forward_pass()

Run after calculating the forward loss.

post_parameter_update()

Has to be called after each parameter update.

regularize_if_necessary(*tensors)

Update the regularizer's term given some tensors, if regularization is requested.

score_h(rt_batch)

Forward pass using left side (head) prediction.

score_r(ht_batch)

Forward pass using middle (relation) prediction.

score_t(hr_batch)

Forward pass using right side (tail) prediction.

Attributes Documentation

regularizer_default: ClassVar[Optional[Type[Regularizer]]] = None

The default regularizer class

regularizer_default_kwargs: ClassVar[Optional[Mapping[str, Any]]] = None

The default parameters for the default regularizer class

Methods Documentation

collect_regularization_term()[source]

Get the regularization term for the loss function.

Return type

FloatTensor

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

regularize_if_necessary(*tensors)[source]

Update the regularizer’s term given some tensors, if regularization is requested.

Parameters

tensors (FloatTensor) – The tensors that should be passed to the regularizer to update its term.

Return type

None

score_h(rt_batch)[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.

Return type

FloatTensor

Returns

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

score_r(ht_batch)[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.

Return type

FloatTensor

Returns

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

score_t(hr_batch)[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.

Return type

FloatTensor

Returns

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