jesusfbes / jRDF2Vec

A high-performance Java Implementation of RDF2Vec

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jRDF2vec

Java CI Coverage Status License

jRDF2Vec is a Java implementation of RDF2Vec. It supports multi-threaded, in-memory (or disk-access-based) walk generation and training. You can generate embeddings for any NT, OWL/XML, RDF HDT, or TTL file.

Found a bug? Don't hesitate to open an issue.

How to use the jRDF2vec Command-Line Interface?

Download this project, execute mvn clean install. Alternatively, you can download the packaged JAR of the latest successful commit here.

System Requirements

  • Java 8 or later.
  • Python 3 with the dependencies described in requirements.txt installed.

Command-Line Interface (jRDF2Vec CLI) for Training and Walk Generation

Use the resulting jar from the target directory.

Minimal Example

java -jar jrdf2vec-1.0-SNAPSHOT.jar -light ./file-to-your-entities.txt -graph ./kg_file.hdt

Required Parameters

  • -graph <graph_file>
    The file containing the knowledge graph for which you want to generate embeddings.

Optional Parameters

jRDF2Vec follows the convention over configuration design paradigm to increase usability. You can overwrite the default values by setting one or more optional parameters.

Parameters for the Walk Configuration

  • -onlyWalks
    If added to the call, this switch will deactivate the training part so that only walks are generated. If training parameters are specified, they are ignored. The walk generation also works with the -light parameter.
  • -light <entity_file>
    If you intend to use RDF2VecLight, you have to use this switch followed by the file path ot the describing the entities for which you require an embedding space. The file should contain one entity (full URI) per line.
  • -numberOfWalks <number> (default: 100)
    The number of walks to be performed per entity.
  • -walkGenerationMode <MID_WALKS | MID_WALKS_DUPLICATE_FREE | RANDOM_WALKS | RANDOM_WALKS_DUPLICATE_FREE> (default for light: MID_WALKS, default for classic: RANDOM_WALKS_DUPLICATE_FREE)
    This parameter determines the mode for the walk generation (multiple walk generation algorithms are available).
  • -depth <depth> (default: 4)
    This parameter controls the depth of each walk. Depth is defined as the number of hops. Hence, you can also set an odd number. A depth of 1 leads to a sentence in the form <s p o>.
  • -threads <number_of_threads> (default: (# of available processors) / 2)
    This parameter allows you to set the number of threads that shall be used for the walk generation as well as for the training.
  • -walkDirectory <directory where walk files shall be generated/reside>
    The directory where the walks shall be generated into. In case of -onlyTraining, the directory where the walks reside.

Parameters for the Training Configuration

  • -trainingMode <cbow | sg> (default: sg)
    This parameter controls the mode to be used for the word2vec training. Allowed values are cbow and sg.
  • -dimension <size_of_vector> (default: 200)
    This parameter allows you to control the size of the resulting vectors (e.g. 100 for 100-dimensional vectors).
  • -minCount <number> (default: 1)
    This parameter controls the minimum word count for the word2vec training. Unlike in the gensim defaults, this parameter is set to 1 by default because for knowledge graph embeddings, a vector for each node/arc is desired.
  • -noVectorTextFileGeneration | -vectorTextFileGeneration
    A switch which indicates whether a text file with the vectors shall be persisted on the disk. This is enabled by default. Use -noVectorTextFileGeneration to disable the file generation.
  • -onlyTraining
    If added to the call, this switch will deactivate the walk generation part so that only the training is performed. The parameter -walkDirectory must be set. If walk generation parameters are specified, they are ignored.

Command-Line Interface (jRDF2Vec CLI) - Additional Services

Besides generating walks and training embeddings, the CLI offers additional services which are described below.

Generating a Text Vector File

jRDF is compatible with the evaluation framework for KG embeddings (GEval). This framework requires the vectors to be present in a text file. If you have a gensim model or vector file, you can use the following command to generate this file:

java -jar jrdf2vec-1.0-SNAPSHOT.jar -generateTextVectorFile ./path-to-your-model-or-vector-file

Run jRDF2Vec using Docker

Optionally, Docker can be used to run jRDF2Vec.

Run

The image can be pulled from DockerHub 🐳

Test run to get help message:

docker run -it --rm vemonet/jrdf2vec

Mount volumes on /data in the container to provide input files and generate embeddings:

  • $(pwd) to use current working directory on Linux and MacOS
  • ${PWD} to use current working directory on Windows (also make the command a one-line)
docker run -it --rm \
  -v $(pwd)/src/test/resources:/data \
  vemonet/jrdf2vec \
  -light /data/sample_dbpedia_entity_file.txt \
  -graph /data/sample_dbpedia_nt_file.nt

Embeddings will be generated in the shared volume (/data in the container).

Build

From source code:

docker build -t jrdf2vec .

Developer Documentation

The most recent JavaDoc sites generated from the latest commit can be found here.

About

A high-performance Java Implementation of RDF2Vec

License:MIT License


Languages

Language:Java 94.6%Language:Python 5.2%Language:Dockerfile 0.3%