TextRepresentation

class TextRepresentation(labels, max_id=None, shape=None, encoder=None, encoder_kwargs=None, missing_action='error', **kwargs)[source]

Bases: Representation

Textual representations using a text encoder on labels.

Example Usage:

Entity representations are obtained by encoding the labels with a Transformer model. The transformer model becomes part of the KGE model, and its parameters are trained jointly.

from pykeen.datasets import get_dataset
from pykeen.nn.representation import TextRepresentation
from pykeen.models import ERModel

dataset = get_dataset(dataset="nations")
entity_representations = TextRepresentation.from_dataset(
    dataset=dataset,
    encoder="transformer",
)
model = ERModel(
    interaction="ermlp",
    entity_representations=entity_representations,
    relation_representations_kwargs=dict(shape=entity_representations.shape),
)

Initialize the representation.

Parameters:
  • labels (Sequence[Optional[str]]) – an ordered, finite collection of labels

  • max_id (Optional[int]) – the number of representations. If provided, has to match the number of labels

  • shape (Union[int, Sequence[int], None]) – The shape of an individual representation.

  • encoder (Union[str, TextEncoder, Type[TextEncoder], None]) – the text encoder, or a hint thereof

  • encoder_kwargs (Optional[Mapping[str, Any]]) – keyword-based parameters used to instantiate the text encoder

  • missing_action (Literal[‘blank’, ‘error’]) – Which policy for handling nones in the given labels. If “error”, raises an error on any nones. If “blank”, replaces nones with an empty string.

  • kwargs – additional keyword-based parameters passed to Representation.__init__()

Raises:

ValueError – if the max_id does not match

Methods Summary

from_dataset(dataset, **kwargs)

Prepare text representation with labels from a dataset.

from_triples_factory(triples_factory[, ...])

Prepare a text representations with labels from a triples factory.

Methods Documentation

classmethod from_dataset(dataset, **kwargs)[source]

Prepare text representation with labels from a dataset.

Parameters:
Return type:

TextRepresentation

Returns:

a text representation from the dataset

Raises:

TypeError – if the dataset’s triples factory does not provide labels

classmethod from_triples_factory(triples_factory, for_entities=True, **kwargs)[source]

Prepare a text representations with labels from a triples factory.

Parameters:
  • triples_factory (TriplesFactory) – the triples factory

  • for_entities (bool) – whether to create the initializer for entities (or relations)

  • kwargs – additional keyword-based arguments passed to TextRepresentation.__init__()

Return type:

TextRepresentation

Returns:

a text representation from the triples factory