ERModel
- class ERModel(*, triples_factory, interaction, interaction_kwargs=None, entity_representations=None, entity_representations_kwargs=None, relation_representations=None, relation_representations_kwargs=None, skip_checks=False, **kwargs)[source]
Bases:
Generic[pykeen.typing.HeadRepresentation,pykeen.typing.RelationRepresentation,pykeen.typing.TailRepresentation],pykeen.models.nbase._NewAbstractModelA commonly useful base for KGEMs using embeddings and interaction modules.
This model does not use post-init hooks to automatically initialize all of its parameters. Rather, the call to
Model.reset_parameters_()happens at the end ofERModel.__init__. This is possible because all trainable parameters should necessarily be passed through thesuper().__init__()in subclasses ofERModel.Other code can still be put after the call to
super().__init__()in subclasses, such as registering regularizers (as done inpykeen.models.ConvKBandpykeen.models.TransH).Initialize the module.
- Parameters
triples_factory (
KGInfo) – The triples factory facilitates access to the dataset.interaction (
Union[str,Interaction[~HeadRepresentation, ~RelationRepresentation, ~TailRepresentation],Type[Interaction[~HeadRepresentation, ~RelationRepresentation, ~TailRepresentation]]]) – The interaction module (e.g., TransE)interaction_kwargs (
Optional[Mapping[str,Any]]) – Additional key-word based parameters given to the interaction module’s constructor, if not already instantiated.entity_representations (
Union[str,Representation,Type[Representation],None,Sequence[Union[str,Representation,Type[Representation],None]]]) – The entity representation or sequence of representationsentity_representations_kwargs (
Union[Mapping[str,Any],None,Sequence[Optional[Mapping[str,Any]]]]) – additional keyword-based parameters for instantiation of entity representationsrelation_representations (
Union[str,Representation,Type[Representation],None,Sequence[Union[str,Representation,Type[Representation],None]]]) – The relation representation or sequence of representationsrelation_representations_kwargs (
Union[Mapping[str,Any],None,Sequence[Optional[Mapping[str,Any]]]]) – additional keyword-based parameters for instantiation of relation representationsskip_checks (
bool) – whether to skip entity representation checks.kwargs – Keyword arguments to pass to the base model
Methods Summary
append_weight_regularizer(parameter, regularizer)Add a model weight to a regularizer's weight list, and register the regularizer with the model.
forward(h_indices, r_indices, t_indices[, ...])Forward pass.
score_h(rt_batch, *[, slice_size, mode])Forward pass using left side (head) prediction.
score_hrt(hrt_batch, *[, mode])Forward pass.
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.
Methods Documentation
- append_weight_regularizer(parameter, regularizer)[source]
Add a model weight to a regularizer’s weight list, and register the regularizer with the model.
- Parameters
parameter (
Union[str,Parameter,Iterable[Union[str,Parameter]]]) –- The parameter, either as name, or as nn.Parameter object. A list of available parameter names is shown by
sorted(dict(self.named_parameters()).keys()).
regularizer (
Regularizer) – The regularizer instance which will regularize the weights.
- Raises
KeyError – If an invalid parameter name was given
- Return type
- forward(h_indices, r_indices, t_indices, slice_size=None, slice_dim=0, *, mode)[source]
Forward pass.
This method takes head, relation and tail indices and calculates the corresponding scores. It supports broadcasting.
- Parameters
h_indices (
LongTensor) – The head indices.r_indices (
LongTensor) – The relation indices.t_indices (
LongTensor) – The tail indices.slice_dim (
int) – The dimension along which to slicemode (
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
The scores
- Raises
NotImplementedError – if score repetition becomes necessary
- 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
- Return type
FloatTensor- Returns
shape: (batch_size, num_entities), dtype: float For each r-t pair, the scores for all possible heads.
- 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_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
- 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, *, 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
- Return type
FloatTensor- Returns
shape: (batch_size, num_entities), dtype: float For each h-r pair, the scores for all possible tails.