Giter Club home page Giter Club logo

krst's Introduction

Multi-Aspect Explainable Inductive Relation Prediction by Sentence Transformer

This repository contains code and data for AAAI 2023 paper Multi-Aspect Explainable Inductive Relation Prediction by Sentence Transformer. In the paper, we propose a novel approach for inductive relation prediction namely KRST. KRST adopts sentence transformer for path-based prediction and could provide a comprehensive explanation.

fig

Requirements

  • python 3.6.0
  • colorama 0.4.4
  • matplotlib 3.3.4
  • networkx 2.5.1
  • numpy 1.18.5
  • scikit_learn 1.1.2
  • torch 1.9.1
  • tqdm 4.64.0
  • transformers 4.18.0

Quick Start

We provide an all-in-one file generate_train_and_test.sh to automatically extract paths, train model, and test model. To specify datasets and few-shot settings, change variables in generate_train_and_test.sh (line 1-3) as follows:

  • dataset: Available datasets include FB15k-237-subset, NELL-995-subset, WN18RR-subset, FB15k-237-subset-inductive, NELL-995-subset-inductive, and WN18RR-subset-inductive.
  • suffix: This is for few-shot settings, including _full, _2000, and _1000.
  • finding_mode: whether head or tail is fixed.

Then by executing the following command:

./generate_train_and_test.sh

you can reproduce the confidence results mentioned in the paper. And testing results are stored in save\$dataset$$suffix$\relation_prediction_$finding_mode$\ (see KRST testing section for more details).

Also, you can just download the model trained by our team here and test them using

./test.sh

Step by Step Implementation

Dataset Preparation

All datasets mentioned in the paper are provided in data\data. They are from BERTRL and are reorganized. If you want to manually create a new dataset, please provide the following files:

  • train$suffix$.txt: file for training. Each line is a triplet (h,r,t) seperated by '\t'.
  • valid.txt and test.txt: files for validation and testing.
  • relation2text.txt and entity2text.txt: description file. Each line contains a relation (or entity) and its corresponding descriptions, separated by '\t'.
  • ranking_head.txt and ranking_tail.txt (optional): files of positive and negative testing triplets. If not provided, use the following command to generate:
python neg_sampling.py --dataset $dataset --suffix $suffix --finding_mode $finding_mode --training_mode test --neg_num 50

Path Finding and Path Filtering

After preparing the dataset, you can do path finding and path filtering using the shell file 'generate.sh'. The parameters that can be fine-tuned include:

  • npaths_ranking: number of paths generated for each triplet.
  • support_type: choice of path filtering function. 0: none, 1: relation path coverage 2: relation path confidence.
  • support_threshold: threshold of path filtering function.
  • search_depth: breadth-first search depth.

Then you can see the generated files in `data\relation_prediction_path_data$dataset$\ranking_$finding_mode$$suffix$'. If you want to skip this step, please download the generated files here.

Sentence Formation and KRST Training

Using file 'train.sh', you can format sentences, load pre-trained sentence transformer, and train KRST by yourself. The parameters that can be fine-tuned include:

  • epochs: upper epoch limit.
  • batch_size: batch size.
  • learning_rate: learning rate.
  • neg_sample_num_train, neg_sample_num_valid and neg_sample_num_test: number of negative samples (should be equal to generated by negative_sampling.py)
  • max_path_num: number of paths loaded for each triplet (you can load a smaller number of paths than generated by path finding).

After training, the best model is saved in save\$dataset$$suffix$\relation_prediction_$finding_mode$\best_val.pth.

KRST Testing

By default, KRST loads the saved best validation model for testing. Run 'test.sh' to get the test results (testing results are stored in save\$dataset$$suffix$\relation_prediction_$finding_mode$\):

  • scores.txt: each line contains the scores for one positive triplet and corresponding negative triplets. Scores are separated by '\t' and follow the order in ranking_$finding_mode$.
  • indexes.txt: each line contains the sorted index of scores (argsort in python) in scores.txt.

You can just download the model trained by our team here and test them.

Interpretation

Before doing interpretation, you need to put two files in data\relation_prediction_path_data\$dataset$\interpret:

  • best_val.pth: model loaded for interpretation.
  • interpret.txt: triplets to be interpreted.

Then run 'interpret.sh' to generate clustering results (pyfig.eps) and confidence score (result.txt). You can download an example from here.

Citations

Please refer to citations here.

Q&A

For any questions, feel free to leave an issue. Thank you very much for your attention and further contribution :)

krst's People

Contributors

aaai2025submission avatar zhixiangsu avatar

Stargazers

 avatar  avatar  avatar 陈大愚 avatar  avatar  avatar

Watchers

Yotam avatar  avatar

Forkers

moguizhizi

krst's Issues

Few-shot settings

Very good work, but I have a small question about the path filter in few-shot datasets. Could you provide related datasets? (entity_path_train.txt relation_path_train.txt ... et al ) The link in README is missing. Thanks for much!

Academic request

In order to conduct interpretable research, I understand that it is necessary to place two files in the 'data/relation_prediction_path_data/$dataset$/interpret' directory. To achieve this, I manually created the 'interpret' directory within 'data/relation_prediction_path_data/$dataset$' and moved both 'best_val.pth' and 'interpret.txt' into this directory. However, when attempting to run './interpret.sh,' I encountered a 'FileNotFoundError' with the message 'No such file or directory: 'data/relation_prediction_path_data/FB15k-237-subset/interpret/interpret.txt.'' Could you please provide guidance on how to correctly execute the './interpret.sh' script?

Academic request

In your research paper, it appears that link prediction results are obtained by averaging the scores of the head entity, tail entity, and relation. Specifically, is it correct to understand that the results are calculated as the average of three separate scores, each corresponding to the finding_mode of "head," "relation," or "tail," computed as (head_score + tail_score + relation_score) / 3? Is this interpretation accurate in the context of your study?

Academic request

I have a question regarding the generate_train_and_test.sh script. In this script, I noticed that there are two options for finding_mode: finding_mode="head" and finding_mode="tail". Could you please clarify if you run these two variables separately for each dataset and then take their average?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.