Python implementation and extension of RDF2Vec to create a 2D feature matrix from a Knowledge Graph for downstream ML tasks.
What is RDF2Vec?
RDF2Vec is an unsupervised technique that builds further on Word2Vec, where an embedding is learned per word, in two ways:
- the word based on its context: Continuous Bag-of-Words (CBOW);
- the context based on a word: Skip-Gram (SG).
To create this embedding, RDF2Vec first creates "sentences" which can be fed to Word2Vec by extracting walks of a certain depth from a Knowledge Graph.
This repository contains an implementation of the algorithm in "RDF2Vec: RDF Graph Embeddings and Their Applications" by Petar Ristoski, Jessica Rosati, Tommaso Di Noia, Renato De Leone, Heiko Paulheim ([paper] [original code]).
Getting Started
We provide a blog post with a tutorial on how to use pyRDF2Vec
here. Below
is a short overview of the different functionalities.
Installation
pyRDF2Vec
can be installed in two ways:
- from PyPI using
pip
:
pip install pyRDF2vec
- from any compatible Python dependency manager (e.g.,
poetry
):
poetry add pyRDF2vec
Introduction
To create embeddings for a list of entities, there are two steps to do beforehand:
- create a Knowledge Graph object;
- define a walking strategy.
For a more elaborate example, check at the example.py file:
PYTHONHASHSEED=42 python3 example.py
NOTE: the PYTHONHASHSEED
(e.g., 42) is to ensure determinism.
Create a Knowledge Graph Object
To create a Knowledge Graph object, you can initialize it in two ways.
- from a file using RDFlib:
from pyrdf2vec.graphs import KG
# Define the label predicates, all triples with these predicates
# will be excluded from the graph
label_predicates = ["http://dl-learner.org/carcinogenesis#isMutagenic"]
kg = KG("samples/mutag/mutag.owl", label_predicates=label_predicates)
- from a server using SPARQL:
from pyrdf2vec.graphs import KG
kg = KG("https://dbpedia.org/sparql", is_remote=True)
Define Walking Strategies With Their Sampling Strategy
All supported walking strategies can be found on the Wiki page.
As the number of walks grows exponentially in function of the depth, exhaustively extracting all walks quickly becomes infeasible for larger Knowledge Graphs. In order to circumvent this issue, sampling strategies can be applied. These will extract a fixed maximum number of walks per entity. The walks are sampled according to a certain metric.
For example, if one wants to extract a maximum of 5 walks of depth 4 for each entity using the Random walking strategy and Uniform sampling strategy (SEE: the Wiki page for other sampling strategies), the following code snippet can be used:
from pyrdf2vec.samplers import UniformSampler
from pyrdf2vec.walkers import RandomWalker
walkers = [RandomWalker(4, 5, UniformSampler())]
Create Embeddings
Finally, the creation of embeddings for a list of entities simply goes like this:
from pyrdf2vec import RDF2VecTransformer
transformer = RDF2VecTransformer(walkers=walkers)
# Entities should be a list of URIs that can be found in the Knowledge Graph
embeddings = transformer.fit_transform(kg, entities)
Documentation
For more information on how to use pyRDF2Vec
, visit our online documentation which is automatically updated
with the latest version of the master
branch.
From then on, you will be able to learn more about the use of the modules as well as their functions available to you.
Contributions
Your help in the development of pyRDF2Vec
is more than welcome. In order to
better understand how you can help either through pull requests and/or issues,
please take a look at the CONTRIBUTING
file.
FAQ: I cannot load my large KG into memory or the public endpoint I use is very slow
Loading large RDF files into memory will cause memory issues as the code is not optimized for larger files. We welcome any PRs that better optimize the memory usage! Remote KGs serve as a solution for larger KGs, but using a public endpoint will be very slow due to overhead caused by HTTP requests. For that reason, it is better to set-up your own local server and use that for your "Remote" KG. Please find a guide on our wiki.
Referencing
If you use pyRDF2Vec
in a scholarly article, we would appreciate a
citation:
@inproceedings{pyrdf2vec,
author = {Gilles Vandewiele and Bram Steenwinckel and Terencio Agozzino
and Michael Weyns and Pieter Bonte and Femke Ongenae
and Filip De Turck},
title = {{pyRDF2Vec: Python Implementation and Extension of RDF2Vec}},
organization = {IDLab},
year = {2020},
url = {https://github.com/IBCNServices/pyRDF2Vec}
}