CooccurrenceFilteredModel¶
- class CooccurrenceFilteredModel(*, triples_factory, additional_triples=None, apply_in_training=False, base='rotate', training_fill_value=-1000.0, inference_fill_value=-inf, conjunctive=False, **kwargs)[source]¶
Bases:
Model
A model which filters predictions by co-occurence.
Initialize the model.
- Parameters:
triples_factory (
CoreTriplesFactory
) – the (training) triples factory; used for creating the co-occurrence counts and for instantiating the base model.additional_triples (
Union
[None
,LongTensor
,List
[LongTensor
]]) – additional triples to use for creating the co-occurrence statisticsapply_in_training (
bool
) – whether to apply the masking also during trainingbase (
Union
[str
,Model
,Type
[Model
],None
]) – the base model, or a hint thereof.training_fill_value (
float
) – the training fill value; for most loss functions, this has to be a finite value, i.e., not infinityinference_fill_value (
float
) – the inference fill valueconjunctive (
bool
) – whether to use conjuction or disjunction to combine non-filter maskskwargs – additional keyword-based parameters passed to the base model upon instantiation
Attributes Summary
The default strategy for optimizing the model's hyper-parameters
Methods Summary
Get the regularization term for the loss function.
predict_h
(rt_batch, **kwargs)Forward pass using left side (head) prediction for obtaining scores of all possible heads.
predict_r
(ht_batch, **kwargs)Forward pass using middle (relation) prediction for obtaining scores of all possible relations.
predict_t
(hr_batch, **kwargs)Forward pass using right side (tail) prediction for obtaining scores of all possible tails.
score_h
(rt_batch, **kwargs)Forward pass using left side (head) prediction.
score_hrt
(hrt_batch, **kwargs)Forward pass.
score_r
(ht_batch, **kwargs)Forward pass using middle (relation) prediction.
score_t
(hr_batch, **kwargs)Forward pass using right side (tail) prediction.
Attributes Documentation
- hpo_default: ClassVar[Mapping[str, Any]] = {'base': {'choices': ['distmult', 'mure', 'rescal', 'rotate', 'transe'], 'type': 'categorical'}, 'conjunctive': {'type': <class 'bool'>}}¶
The default strategy for optimizing the model’s hyper-parameters
Methods Documentation
- collect_regularization_term()[source]¶
Get the regularization term for the loss function.
- Return type:
FloatTensor
- predict_h(rt_batch, **kwargs)[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.kwargs – additional keyword-based parameters passed to
Model.score_h()
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_heads), dtype: float For each r-t pair, the scores for all possible heads.
- predict_r(ht_batch, **kwargs)[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.kwargs – additional keyword-based parameters passed to
Model.score_r()
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_relations), dtype: float For each h-t pair, the scores for all possible relations.
- predict_t(hr_batch, **kwargs)[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.kwargs – additional keyword-based parameters passed to
Model.score_t()
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_tails), 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.
- score_h(rt_batch, **kwargs)[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 – >0 The divisor for the scoring function when using slicing.
mode – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.
heads – shape: (num_heads,) | (batch_size, num_heads) head entity indices to score against. If None, scores against all entities (from the given mode).
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_heads), dtype: float For each r-t pair, the scores for all possible heads.
- score_hrt(hrt_batch, **kwargs)[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 – 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, **kwargs)[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 – >0 The divisor for the scoring function when using slicing.
mode – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.
relations – shape: (num_relations,) | (batch_size, num_relations) relation indices to score against. If None, scores against all relations (from the given mode).
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_real_relations), dtype: float For each h-t pair, the scores for all possible relations.
- score_t(hr_batch, **kwargs)[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 – >0 The divisor for the scoring function when using slicing.
mode – The pass mode, which is None in the transductive setting and one of “training”, “validation”, or “testing” in the inductive setting.
tails – shape: (num_tails,) | (batch_size, num_tails) tail entity indices to score against. If None, scores against all entities (from the given mode).
- Return type:
FloatTensor
- Returns:
shape: (batch_size, num_tails), dtype: float For each h-r pair, the scores for all possible tails.