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.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 passingindices=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
tomax_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 significantly more expensive than an index-based lookup and duplicate indices are expected, e.g., when using negative sampling and large batch sizes.
Warning
When using this optimization you may encounter unexpected results for stochastic operations, e.g.,
torch.nn.Dropout
.
Note
2 resolvers are used in this function.
The parameter pair
(normalizer, normalizer_kwargs)
is used forpykeen.nn.representation.normalizer_resolver
The parameter pair
(regularizer, regularizer_kwargs)
is used forpykeen.regularizers.regularizer_resolver
An explanation of resolvers and how to use them is given in https://class-resolver.readthedocs.io/en/latest/.
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