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.

What's New

  • [May, 2024] We perform instruction tuning with LLaMA-2-7B for UKG reasoning and achieve stronger performance in several UKG resoning tasks. We have relesed the implementation source code and tuned checkpoints.
  • [Mar, 2024] Our paper "unKR: A Python Library for Uncertain Knowledge Graph Reasoning by Representation Learning" is accepted by SIGIR2024 Demonstration Track.
  • [Feb, 2024] We have relesed this repo that includes the source code and documentation of unKR.

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+

Reproduced 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

Citation

Please cite our paper if you use unKR in your work.

@inproceedings{unkr,
  author    = {Jingting Wang and
               Tianxing Wu and
               Shilin Chen and
               Yunchang Liu and
               Shutong Zhu and
               Wei Li and
               Jingyi Xu and
               Guilin Qi},
  title     = {unKR: A Python Library for Uncertain Knowledge Graph Reasoning by Representation Learning},
  booktitle = {{SIGIR}},
  pages     = {2822--2826},
  publisher = {{ACM}},
  year      = {2024}
}

unKR Core Team

Southeast University: Tianxing Wu, Jingting Wang, 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.1.tar.gz (59.3 kB view details)

Uploaded Source

Built Distribution

unKR-1.0.1-py3-none-any.whl (84.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for unKR-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d043567019b91ffcd0f927e11c90c7bd01a34c7b6e9e6a5102a69eb48b30bd79
MD5 e38981e493715342e5570c9facd089a5
BLAKE2b-256 d75f4123379fdf3fd0e3bb1a96810321a49028915a4fac6df532966f6b9edd19

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for unKR-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd42c580ec18b32cbcc60071415f453228f95d8cafe8c0baa410322230df130a
MD5 82f878fdd4a3e419f8a3ec2300480e4f
BLAKE2b-256 8fca6b74a94912d2d3e210956540c0795a5e8125099218b65214d85821ae9f89

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