Representation
- class Representation(max_id: int, shape: int | Sequence[int] = 64, normalizer: str | Callable[[Tensor], Tensor] | type[Callable[[Tensor], Tensor]] | None = None, normalizer_kwargs: Mapping[str, Any] | None = None, regularizer: str | Regularizer | type[Regularizer] | None = None, regularizer_kwargs: Mapping[str, Any] | None = None, dropout: float | None = None, unique: bool | None = None)[source]
Bases:
Module
,ExtraReprMixin
,ABC
A base class for obtaining representations for entities/relations.
A representation module maps integer IDs to representations, which are tensors of floats.
max_id defines the upper bound of indices we are allowed to request (exclusively). For simple embeddings this is equivalent to num_embeddings, but more a more appropriate word for general non-embedding representations, where the representations could come from somewhere else, e.g. a GNN encoder.
shape describes the shape of a single representation. In case of a vector embedding, this is just a single dimension. For others, e.g.
pykeen.models.RESCAL
, we have 2-d representations, and in general it can be any fixed shape.We can look at all representations as a tensor of shape (max_id, *shape), and this is exactly the result of passing indices=None to the forward method.
We can also pass multi-dimensional indices to the forward method, in which case the indices’ shape becomes the prefix of the result shape: (*indices.shape, *self.shape).
Initialize the representation module.
- Parameters:
max_id (int) – The maximum ID (exclusively). Valid Ids reach from 0, …, max_id-1
shape (tuple[int, ...]) – The shape of an individual representation.
normalizer (Callable[[Tensor], Tensor] | None) – A normalization function, which is applied to the selected representations in every forward pass.
normalizer_kwargs (OptionalKwargs) – Additional keyword arguments passed to the normalizer
regularizer (Regularizer | None) – An output regularizer, which is applied to the selected representations in forward pass
regularizer_kwargs (OptionalKwargs) – Additional keyword arguments passed to the regularizer
dropout (Dropout | None) – The optional dropout probability
unique (bool | None) – whether to optimize for calculating representations for same indices only once. This is only useful if the calculation of representations is either significantly more expensive than an index-based lookup and duplicate indices are expected, e.g., when using negative sampling and large batch sizes
Attributes Summary
Return the device.
Methods Summary
forward
([indices])Get representations for indices.
Iterate over components for
extra_repr()
.Apply constraints which should not be included in gradients.
Reset the module's parameters.
Attributes Documentation
- device
Return the device.
Methods Documentation
- forward(indices: Tensor | None = None) Tensor [source]
Get representations for indices.
Note
depending on
Representation.unique
, this implementation will use an optimization for duplicate indices. It is generally only recommended if computing individual representation is expensive, e.g., since it involves message passing, or a large encoder networks, but discouraged for cheap lookups, e.g., a plain embedding lookup.