pyrdf2vec package

Subpackages

Submodules

Module contents

class pyrdf2vec.RDF2VecTransformer(embedder=NOTHING, walkers=NOTHING, *, verbose=0)

Bases: object

Transforms nodes in a Knowledge Graph into an embedding.

_embeddings

All the embeddings of the model. Defaults to [].

_entities

All the entities of the model. Defaults to [].

_is_extract_walks_literals

True if the session must be closed after the call to the transform function. False, otherwise. Defaults to False.

_literals

All the literals of the model. Defaults to [].

_pos_entities

The positions of existing entities to be updated. Defaults to [].

_pos_walks

The positions of existing walks to be updated. Defaults to [].

_walks

All the walks of the model. Defaults to [].

embedder

The embedding technique. Defaults to Word2Vec.

walkers

The walking strategies. Defaults to [RandomWalker(2, None)]

verbose

The verbosity level. 0: does not display anything; 1: display of the progress of extraction and training of walks; 2: debugging. Defaults to 0.

fit(kg, entities, is_update=False)

Fits the embeddings based on the provided entities.

Parameters
  • kg (KG) – The KG from which walks should be extracted.

  • entities (List[str]) – The entities of interest, starting points for walkers.

  • is_update (bool) – True if the new corpus should be added to old model’s corpus, False otherwise. Defaults to False.

Return type

RDF2VecTransformer

Returns

The RDF2VecTransformer.

fit_transform(kg, entities, is_update=False)

Creates a model and generates embeddings and literals for the provided entities.

Parameters
  • kg (KG) – The Knowledge Graph.

  • entities (List[str]) – The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage.

  • is_update (bool) – True if the new corpus should be added to old model’s corpus, False otherwise. Defaults to False.

Return type

Tuple[List[str], List[List[Union[float, str, Tuple[Union[float, str], ...]]]]]

Returns

The embeddings and the literals of the provided entities.

get_walks(kg, entities)

Gets the walks of an entity based on a Knowledge Graph and a list of walkers

Parameters
  • kg (KG) – The Knowledge Graph.

  • entities (List[str]) – The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage.

Return type

List[List[Tuple[str, ...]]]

Returns

The walks for the given entities.

Raises

ValueError – If the provided entities aren’t in the Knowledge Graph.

static load(filename='transformer_data')

Loads a RDF2VecTransformer object.

Parameters

filename (str) – The binary file to load the RDF2VecTransformer object.

Return type

RDF2VecTransformer

Returns

The loaded RDF2VecTransformer.

save(filename='transformer_data')

Saves a RDF2VecTransformer object.

Parameters

filename (str) – The binary file to save the RDF2VecTransformer object.

Return type

None

transform(kg, entities)

Transforms the provided entities into embeddings and literals.

Parameters
  • kg (KG) – The Knowledge Graph.

  • entities (List[str]) – The entities including test entities to create the embeddings. Since RDF2Vec is unsupervised, there is no label leakage.

Return type

Tuple[List[str], List[List[Union[float, str, Tuple[Union[float, str], ...]]]]]

Returns

The embeddings and the literals of the provided entities.