Package: pysqlitekg2vec
What is SQLiteKG2Vec?SQLitKG2Vec is an experimental extension of the popular pyRDF2Vec
library for training RDF2Vec embeddings. It might in the future be
merged into the main project. This experimental extension stores the
statements of the KG as well as the generated walks into a simple SQLite
database. Hence, it is possible to train embeddings for huge knowledge
graphs without running into memory issues.RDF2Vec is an unsupervised technique that builds further onWord2Vec, 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 StartedFor most uses-cases, here is howpySQLiteKG2Vecshould be used to
generate embeddings and get literals from a given Knowledge Graph (KG)
and entities:frompyrdf2vecimportRDF2VecTransformerfrompyrdf2vec.embeddersimportWord2Vecfrompyrdf2vec.graphs.ioimportopen_from_pykeen_datasetfrompyrdf2vec.walkersimportRandomWalkerfrompyrdf2vec.walkers.vault.sqlitevaultimportSQLiteCorpusVaultFactorywithopen_from_pykeen_dataset('dbpedia50')askg:transformer=RDF2VecTransformer(Word2Vec(epochs=10),walkers=[RandomWalker(max_walks=200,max_depth=4,random_state=133,with_reverse=False,n_jobs=1)],vault_factory=SQLiteCorpusVaultFactory('corpus.db'),verbose=1)# train RDF2Vecent=kg.entities()embeddings,_=transformer.fit_transform(kg,ent)withopen('embeddings.tsv','w')asf:writer=csv.writer(f,delimiter='\t')forname,vectorinkg.pack(ent,embeddings):writer.writerow([name]+[xforxinvector])Create from PyKeen datasetPyKeenis a popular library for
knowledge graph embeddings, and it specifies a number of datasets that
are commonly referenced in scientific literature. An SQLite KG can be
constructed from a PyKeen dataset by specifying the name of the dataset
or passing the dataset instance.In the following code snippet, thedb100kdataset, which is a subsampling of DBpedia, is used to construct an
SQLite KG.frompyrdf2vec.graphs.ioimportopen_from_pykeen_datasetwithopen_from_pykeen_dataset('db100k',combined=True)askg:# ...passParameters:combined-Falseif only the
training set of a dataset shall be used for the training of RDF2Vec.Trueif all the sets (training,
testing and validation) shall be used. It isFalseby default.Create from TSV fileIn order to save memory for big knowledge graphs, it might be a good
idea to load the statements of such a knowledge graph from a TSV file
into a SQLite KG. All the rows in the TSV file must have three columns,
where the first column is the subject, the second is the predicate, and
the last column is the object.The following code snippet creates a new SQLite KG instance from the
statements of the specified TSV file, which has been compressed using
GZIP.frompyrdf2vec.graphs.ioimportopen_from_tsv_filewithopen_from_tsv_file('statements.tsv.gz',compression='gzip')askg:# ...passParameters:skip_header-Trueif the first row
shall be skipped, because it is a header row for example.Falseif it shouldn't be skipped. It isFalseby default.compression- specifies the compression type of source TSV file.
The default value isNone, which
means that the source isn't compressed. At the moment, only'gzip'is supported as compression type.Create from Pandas dataframeA knowledge graph can be represented in a Pandas dataframe, and this
method allows to create an SQLite KG from a dataframe. While the
dataframe can have more than three columns, the three columns
representing the subject, predicate and object must be specified in this
particular order.The following code snippet creates a new SQLite KG instance from a
dataframe.frompyrdf2vec.graphs.ioimportopen_from_dataframewithopen_from_dataframe(df,column_names=('subj','pred','obj'))askg:# ...passParameters:column_names- a tuple of three indices for the dataframe, which
can be an integer or string. The first entry of the tuple shall
point to the subject, the second to the predicate, and the third one
to the object.(0, 1, 2)are the
default indices.LimitationsThis extension has three limitations in contrast to the original
implementation.Literalsare ignored by this implementation for now.Samplers(besides the default one) might not work properly.InstallationpySQLiteKG2Veccan be installed in three ways:fromPyPIusingpip:pipinstallpySQLiteKG2Vecfrom any compatible Python dependency manager (e.g.,poetry):poetryaddpyRDF2vecfrom source:gitclonehttps://github.com/IBCNServices/pyRDF2Vec.git
pipinstall.DocumentationFor more information on how to usepyRDF2Vec,visit our online
documentationwhich is
automatically updated with the latest version of themainbranch.From then on, you will be able to learn more about the use of the
modules as well as their functions available to you.ContributionsYour help in the development ofpyRDF2Vecis more than welcome.The architecture ofpyRDF2Vecmakes it easy to create new extraction
and sampling strategies, new embedding techniques. In order to better
understand how you can help either through pull requests and/or issues,
please take a look at theCONTRIBUTINGfile.FAQHow to Ensure the Generation of Similar Embeddings?pySQLiteKG2Vec's walking strategies, sampling strategies and Word2Vec
work with randomness. To get reproducible embeddings, you firstly need
touse a seedto ensure determinism:PYTHONHASHSEED=42pythonfoo.pyAdded to this, you mustalso specify a random stateto the walking
strategy which will implicitly use it for the sampling strategy:frompyrdf2vec.walkersimportRandomWalkerRandomWalker(2,None,random_state=42)NOTE:thePYTHONHASHSEED(e.g., 42) is to ensure determinism.Finally, to ensure random determinism for Word2Vec, you mustspecify a
single worker:frompyrdf2vec.embeddersimportWord2VecWord2Vec(workers=1)NOTE:using then_jobsandmul_reqparameters does not affect
the random determinism.Why the Extraction Time of Walks is Faster ifmax_walks=None?Currently,the BFS function(using the Breadth-first search
algorithm) is used whenmax_walks=Nonewhich is significantlyfasterthan the DFS function (using the Depth-first search
algorithm)and extract more walks.We hope that this algorithmic complexity issue will be solved for the
next release ofpyRDf2VecHow to Silence the tcmalloc Warning When Using FastText With Mediums/Large KGs?Sets theTCMALLOC_LARGE_ALLOC_REPORT_THRESHOLDenvironment variable to
a high value.ReferencingIf you usepyRDF2Vecin a scholarly article, we would appreciate a
citation:@article{pyrdf2vec,title={pyRDF2Vec: A Python Implementation and Extension of RDF2Vec},author={Vandewiele, Gilles and Steenwinckel, Bram and Agozzino, Terencio and Ongenae, Femke},year=2022,publisher={arXiv},doi={10.48550/ARXIV.2205.02283},url={https://arxiv.org/abs/2205.02283},copyright={Creative Commons Attribution 4.0 International},organization={IDLab},keywords={Machine Learning (cs.LG), FOS: Computer and information sciences, FOS: Computer and information sciences}}