_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 (TriplesFactory) – 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[None, str, device]) – 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_kwargs

The default parameters for the default regularizer class

Methods Summary

compute_loss(tensor_1, tensor_2)

Compute the loss for functions requiring two separate tensors as input.

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_kwargs: ClassVar[Optional[Mapping[str, Any]]] = None

The default parameters for the default regularizer class

Methods Documentation

compute_loss(tensor_1, tensor_2)[source]

Compute the loss for functions requiring two separate tensors as input.

Parameters
  • tensor_1 (FloatTensor) – shape: s The tensor containing predictions or positive scores.

  • tensor_2 (FloatTensor) – shape: s The tensor containing target values or the negative scores.

Return type

FloatTensor

Returns

dtype: float, scalar The label loss value.

Note

generally the two tensors do not need to have the same shape, but only one which is broadcastable.

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.