AutoSFInteraction
- class AutoSFInteraction(coefficients: Iterable[tuple[int, int, int, Literal[-1, 1]]], *, num_blocks: int | None = None, num_entity_representations: int | None = None, num_relation_representations: int | None = None)[source]
Bases:
Interaction
[HeadRepresentation
,RelationRepresentation
,TailRepresentation
]The AutoSF interaction as described by [zhang2020].
This interaction function is a parametrized way to express bi-linear models with block structure. It divides the entity and relation representations into blocks, and expresses the interaction as a sequence of 4-tuples \((i_h, i_r, i_t, s)\), where \(i_h, i_r, i_t\) index a _block_ of the head, relation, or tail representation, and \(s \in {-1, 1}\) is the sign.
The interaction function is then given as
\[\sum_{(i_h, i_r, i_t, s) \in \mathcal{C}} s \cdot \langle h[i_h], r[i_r], t[i_t] \rangle\]where \(\langle \cdot, \cdot, \cdot \rangle\) denotes the tri-linear dot product.
This parametrization allows to express several well-known interaction functions, e.g.
pykeen.nn.DistMultInteraction
:one block, \(\mathcal{C} = \{(0, 0, 0, 1)\}\)
pykeen.nn.ComplExInteraction
:two blocks, \(\mathcal{C} = \{(0, 0, 0, 1), (0, 1, 1, 1), (1, 0, 1, -1), (1, 0, 1, 1)\}\)
pykeen.nn.SimplEInteraction
:two blocks: \(\mathcal{C} = \{(0, 0, 1, 1), (1, 1, 0, 1)\}\)
While in theory, we can have up to num_blocks**3 unique triples, usually, a smaller number is preferable to have some sparsity.
Initialize the interaction function.
- Parameters:
coefficients (tuple[tuple[int, int, int, Literal[-1, 1]], ...]) – the coefficients for the individual blocks, cf.
pykeen.nn.AutoSFInteraction
num_blocks (int | None) – the number of blocks. If given, will be used for both, entity and relation representations.
num_entity_representations (int | None) – an explicit number of entity representations / blocks. Only used if num_blocks is None. If num_entity_representations is None, too, this number if inferred from coefficients.
num_relation_representations (int | None) – an explicit number of relation representations / blocks. Only used if num_blocks is None. If num_relation_representations is None, too, this number if inferred from coefficients.
Methods Summary
extend
(*new_coefficients)Extend AutoSF function, as described in the greedy search algorithm in the paper.
forward
(h, r, t)Evaluate the interaction function.
from_searched_sf
(coefficients, **kwargs)Instantiate AutoSF interaction from the "official" serialization format.
Create the LaTeX + tikz visualization as shown in the paper.
Methods Documentation
- extend(*new_coefficients: tuple[int, int, int, Literal[-1, 1]]) AutoSFInteraction [source]
Extend AutoSF function, as described in the greedy search algorithm in the paper.
- forward(h: HeadRepresentation, r: RelationRepresentation, t: TailRepresentation) Tensor [source]
Evaluate the interaction function.
See also
Interaction.forward
for a detailed description about the generic batched form of the interaction function.- Parameters:
h (HeadRepresentation) – shape:
(*batch_dims, d)
The head representations.r (RelationRepresentation) – shape:
(*batch_dims, d)
The relation representations.t (TailRepresentation) – shape:
(*batch_dims, d)
The tail representations.
- Returns:
shape:
batch_dims
The scores.- Return type:
- classmethod from_searched_sf(coefficients: Sequence[int], **kwargs) AutoSFInteraction [source]
Instantiate AutoSF interaction from the “official” serialization format.
> The first 4 values (a,b,c,d) represent h_1 * r_1 * t_a + h_2 * r_2 * t_b + h_3 * r_3 * t_c + h_4 * r_4 * t_d. > For the others, every 4 values represent one adding block: index of r, index of h, index of t, the sign s.
- Parameters:
- Returns:
An AutoSF interaction module
- Return type: