Triples

Classes for creating and storing training data from triples.

class pykeen.triples.Instances(mapped_triples, entity_to_id, relation_to_id)[source]

Triples and mappings to their indices.

property num_entities

The number of entities.

Return type

int

property num_instances

The number of instances.

Return type

int

class pykeen.triples.LCWAInstances(mapped_triples, entity_to_id, relation_to_id, labels)[source]

Triples and mappings to their indices for LCWA.

class pykeen.triples.MultimodalInstances(mapped_triples, entity_to_id, relation_to_id, numeric_literals, literals_to_id)[source]

Triples and mappings to their indices as well as multimodal data.

class pykeen.triples.MultimodalLCWAInstances(mapped_triples, entity_to_id, relation_to_id, numeric_literals, literals_to_id, labels)[source]

Triples and mappings to their indices as well as multimodal data for LCWA.

class pykeen.triples.MultimodalSLCWAInstances(mapped_triples, entity_to_id, relation_to_id, numeric_literals, literals_to_id)[source]

Triples and mappings to their indices as well as multimodal data for sLCWA.

class pykeen.triples.SLCWAInstances(mapped_triples, entity_to_id, relation_to_id)[source]

Triples and mappings to their indices for sLCWA.

class pykeen.triples.TriplesFactory(*, path=None, triples=None, create_inverse_triples=False, entity_to_id=None, relation_to_id=None, compact_id=True)[source]

Create instances given the path to triples.

Initialize the triples factory.

Parameters
  • path (Union[None, str, TextIO]) – The path to a 3-column TSV file with triples in it. If not specified, you should specify triples.

  • triples (Optional[ndarray]) – A 3-column numpy array with triples in it. If not specified, you should specify path

  • create_inverse_triples (bool) – Should inverse triples be created? Defaults to False.

  • compact_id (bool) – Whether to compact the IDs such that they range from 0 to (num_entities or num_relations)-1

create_lcwa_instances(use_tqdm=None)[source]

Create LCWA instances for this factory’s triples.

Return type

LCWAInstances

create_slcwa_instances()[source]

Create sLCWA instances for this factory’s triples.

Return type

SLCWAInstances

entity_to_id: Mapping[str, int]

The mapping from entities’ labels to their indexes

get_idx_for_relations(relations, invert=False)[source]

Get an np.array index for triples with the given relations.

get_inverse_relation_id(relation)[source]

Get the inverse relation identifier for the given relation.

Return type

int

get_most_frequent_relations(n)[source]

Get the n most frequent relations.

Parameters

n (Union[int, float]) – Either the (integer) number of top relations to keep or the (float) percentage of top relationships to keep

Return type

Set[str]

get_triples_for_relations(relations, invert=False)[source]

Get the labeled triples containing the given relations.

Return type

ndarray

map_triples_to_id(triples)[source]

Load triples and map to ids based on the existing id mappings of the triples factory.

Works from either the path to a file containing triples given as string or a numpy array containing triples.

Return type

LongTensor

mapped_triples: torch.LongTensor

A three-column matrix where each row are the head identifier, relation identifier, then tail identifier

new_with_relations(relations)[source]

Make a new triples factory only keeping the given relations.

Return type

TriplesFactory

new_without_relations(relations)[source]

Make a new triples factory without the given relations.

Return type

TriplesFactory

property num_entities

The number of unique entities.

Return type

int

property num_relations

The number of unique relations.

Return type

int

property num_triples

The number of triples.

Return type

int

relation_to_id: Mapping[str, int]

The mapping from relations’ labels to their indexes

relation_to_inverse: Optional[Mapping[str, str]]

A dictionary mapping each relation to its inverse, if inverse triples were created

split(ratios=0.8, *, random_state=None)[source]

Split a triples factory into a train/test.

Parameters
  • ratios (Union[float, Sequence[float]]) – There are three options for this argument. First, a float can be given between 0 and 1.0, non-inclusive. The first triples factory will get this ratio and the second will get the rest. Second, a list of ratios can be given for which factory in which order should get what ratios as in [0.8, 0.1]. The final ratio can be omitted because that can be calculated. Third, all ratios can be explicitly set in order such as in [0.8, 0.1, 0.1] where the sum of all ratios is 1.0.

  • random_state (Union[None, int, RandomState]) – The random state used to shuffle and split the triples in this factory.

ratio = 0.8  # makes a [0.8, 0.2] split
training_factory, testing_factory = factory.split(ratio)

ratios = [0.8, 0.1]  # makes a [0.8, 0.1, 0.1] split
training_factory, testing_factory, validation_factory = factory.split(ratios)

ratios = [0.8, 0.1, 0.1]  # also makes a [0.8, 0.1, 0.1] split
training_factory, testing_factory, validation_factory = factory.split(ratios)
Return type

List[TriplesFactory]

triples: numpy.ndarray

A three-column matrix where each row are the head label, relation label, then tail label

class pykeen.triples.TriplesNumericLiteralsFactory(*, path=None, triples=None, path_to_numeric_triples=None, numeric_triples=None)[source]

Create multi-modal instances given the path to triples.

Initialize the multi-modal triples factory.

Parameters
  • path (Union[None, str, TextIO]) – The path to a 3-column TSV file with triples in it. If not specified, you should specify triples.

  • triples (Optional[ndarray]) – A 3-column numpy array with triples in it. If not specified, you should specify path

  • path_to_numeric_triples (Union[None, str, TextIO]) – The path to a 3-column TSV file with triples and numeric. If not specified, you should specify numeric_triples.

  • numeric_triples (Optional[ndarray]) – A 3-column numpy array with numeric triples in it. If not specified, you should specify path_to_numeric_triples.

create_lcwa_instances(use_tqdm=None)[source]

Create multi-modal LCWA instances for this factory’s triples.

Return type

MultimodalLCWAInstances

create_slcwa_instances()[source]

Create multi-modal sLCWA instances for this factory’s triples.

Return type

MultimodalSLCWAInstances

Instance creation utilities.

pykeen.triples.utils.load_triples(path, delimiter='\\t')[source]

Load triples saved as tab separated values.

Besides TSV handling, PyKEEN does not come with any importers pre-installed. A few can be found at:

Return type

ndarray