Skip to main content

An Open Source Library for uncertain Knowledge Reasoning

Project description

unKR: A Python Library for Uncertain Knowledge Graph Reasoning by Representation Learning

Pypi Pypi Documentation

unKR is an python library for uncertain Knowledge graph (UKG) Reasoning based on the PyTorch Lightning. It provides a unifying workflow to implement a variety of uncertain knowledge graph representation learning models to complete UKG reasoning. unKR consists of five modules: 1) Data Processor handles low-level dataset parsing and negative sampling, then generates mini-batches of data; 2) Model Hub implements the model algorithms, containing the scoring function and loss function; 3) Trainer conducts iterative training and validation; 4) Evaluator provides confidence prediction and link prediction tasks to evaluate models' performance; 5) Controller controls the training worklow, allowing for early stopping and model saving. These modules are decoupled and independent, making unKR highly modularized and extensible. Detailed documentation of the unKR is available at here.

unKR core development team will provide long-term technical support, and developers are welcome to discuss the work and initiate questions using issue.

Demo

This is a demo shows the training and testing process of PASSLEAF model with unKR.

Datasets

unKR provides three public UKG datasets including CN15K, NL27K, and PPI5K. The following table shows the source, the number of entities, relations, and facts of each dataset.

Dataset Source #Entity #Relation #Fact
CN15K ConceptNet 15000 36 241158
NL27K NELL 27221 404 175412
PPI5K STRING 4999 7 271666

Models

Now, nine uncertain knowledge graph representation learning models are available and they can be divided to two types: normal and few-shot models.

Type Model
Normal BEURrE, FocusE, GTransE, PASSLEAF, UKGE, UKGsE, UPGAT
Few-shot GMUC, GMUC+

Reproduce Results

unKR determines two tasks, confidence prediction and link prediction, to evaluate models' ability of UKG reasoning. For confidence prediction task, MSE (Mean Squared Error) and MAE (Mean Absolute Error) are reported. For link prediction task, Hits@k(k=1,3,10), MRR (Mean Reciprocal Rank), MR (Mean Rank) under both raw and filterd settings are reported. In addition, we choose high-confidence (>0.7) triples as the test data for link prediction.

Here are the reproduce results of nine models on NL27K dataset with unKR. See more results at here.

Confidence prediction

Type Model MSE MAE
Normal BEUrRE 0.08920 0.22194
PASSLEAF(ComplEx) 0.02434 0.05176
PASSLEAF(DistMult) 0.02309 0.05107
PASSLEAF(RotatE) 0.01949 0.06253
UKGE_logi 0.02868 0.05966
UKGE_rect 0.03326 0.07015
UKGsE 0.12202 0.27065
UPGAT 0.02922 0.10107
Few-shot GMUC 0.01300 0.08200
GMUC+ 0.01300 0.08600

Link prediction

Type Model Hits@1 Hits@3 Hits@10 MRR MR
Normal BEUrRE 0.156 0.385 0.543 0.299 488.051
FocusE 0.814 0.918 0.957 0.870 384.471
GTransE 0.222 0.366 0.493 0.316 1377.564
PASSLEAF(ComplEx) 0.669 0.786 0.876 0.741 138.808
PASSLEAF(DistMult) 0.627 0.754 0.856 0.707 138.781
PASSLEAF(RotatE) 0.687 0.816 0.884 0.762 50.776
UKGE_logi 0.525 0.673 0.812 0.623 168.029
UKGE_rect 0.500 0.647 0.800 0.599 125.233
UKGsE 0.038 0.073 0.130 0.069 2329.501
UPGAT 0.618 0.751 0.862 0.701 69.120
Few-shot GMUC 0.335 0.465 0.592 0.425 58.312
GMUC+ 0.338 0.486 0.636 0.438 45.774

Usage

Installation

Step1 Create a virtual environment using Anaconda and enter it.

conda create -n unKR python=3.8
conda activate unKR

Step2 Install package.

  • Install from source
git clone https://github.com/seucoin/unKR.git
cd unKR
pip install -r requirements.txt
python setup.py install
  • Install by pypi
pip install unKR

Data Format

For normal models, train.tsv, val.tsv, and test.tsv are required.

  • train.tsv: All facts used for training in the format (h, r, t, s), one fact per line.

  • val.tsv: All facts used for validation in the format (h, r, t, s), one fact per line.

  • test.tsv: All facts used for testing in the format (h, r, t, s), one fact per line.

For few-shot models, train_tasks.json, dev_tasks.json, test_tasks.json and path_graph are required.

  • train/dev/test_tasks.json: Few-shot dataset with one task per relation in the format{r:[[h, r, t, s], ...]}. The key of the dictionary is the task name and the values are all the facts under the task.

  • path_graph: background knowledge, i.e., all data except training, validation and testing tasks, in the format(h, r, t, s). One fact per line.

For UKGE, thesoftlogic.tsvfile is also required.

  • softlogic.tsv: All facts inferred by PSL in the format(h, r, t, s), one fact per line.

Parameter Setting

You can set up parameters by config file. The desciption of each paramter is at here.

Model Training

python main.py --load_config --config_path <your-config>

Model Testing

python main.py --test_only --checkpoint_dir <your-model-path>

Model Customization

If you want to customize your own model using unKR, you need to create the following classes/functions.

data: Implement data processing functions, including DataPreprocess, Sampler and KGDataModule.

DataPreprocess.py: 
    class unKR.data.DataPreprocess.<your-model-name>BaseSampler
    class unKR.data.DataPreprocess.<your-model-name>Data
Sampler:
    class unKR.data.Sampler.<your-model-name>Sampler
    class unKR.data.Sampler.<your-model-name>TestSampler
KGDataModule.py: 
    class unKR.data.KGDataModule.<your-model-name>DataModule

lit_model: Implement model training, validation, and testing functions.

<your-model-name>LitModel.py:
    class unKR.lit_model.<your-model-name>LitModel.<your-model-name>LitModel

loss: Implement loss functions.

<your-model-name>_Loss.py:
    class unKR.loss.<your-model-name>_Loss.<your-model-name>_Loss

model: Implement model framework functions, classified as UKGModel and FSUKGModel based on whether it is a few-shot model.

<your-model-name>.py:
    class unKR.model.UKGModel/FSUKGModel.<your-model-name>.<your-model-name>

config: Implement parameter settings.

<your-model-name>_<dataset-name>.yaml:
    data_class, litmodel_name, loss_name, model_name, test_sampler_class, train_sampler_class

demo: Implement the model run file.

<your-model-name>demo.py

unKR Core Team

Southeast University: Jingting Wang, Tianxing Wu, Shilin Chen, Yunchang Liu, Shutong Zhu, Wei Li, Jingyi Xu, Guilin Qi.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unKR-1.0.0.tar.gz (58.6 kB view details)

Uploaded Source

Built Distribution

unKR-1.0.0-py3-none-any.whl (84.4 kB view details)

Uploaded Python 3

File details

Details for the file unKR-1.0.0.tar.gz.

File metadata

  • Download URL: unKR-1.0.0.tar.gz
  • Upload date:
  • Size: 58.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for unKR-1.0.0.tar.gz
Algorithm Hash digest
SHA256 de08a064f423216f9faa85f6f9b39a901b5bc9489b6e3c7a9acac217b6cb1ae7
MD5 4f615ab1ecfd7219060743ced5cc42ff
BLAKE2b-256 bae8eac52aeb7d7f91156d45deaecf4ce25a19ec8cf718699a4d243cdf3ec581

See more details on using hashes here.

File details

Details for the file unKR-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: unKR-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 84.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.18

File hashes

Hashes for unKR-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5130c8a3873088192da53f90bf98a94b7078c1f540a810787faf39a9abeaf9a8
MD5 0cc2dc8eedf1367d36b44b001e083102
BLAKE2b-256 1cd1d2a7a11855ba88b2ef9165bc6651d9671120005680533fff8652863f7bd7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page