Giter VIP home page Giter VIP logo

cbr-subg's Introduction

Knowledge Base Question Answering using Case-based Reasoning over Subgraphs

This is the official implementation of the paper - Knowledge Base Question Answering using Case-based Reasoning over Subgraphs.

Installation

conda create -n pygnn python=3.9
conda activate pygnn
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c nvidia ## this installs pytorch 1.9
pip install transformers
pip install wandb
conda install pytorch-scatter -c rusty1s
conda install pytorch-sparse -c rusty1s
conda install pytorch-cluster -c rusty1s
conda install pytorch-spline-conv -c rusty1s
conda install pytorch-geometric -c rusty1s -c conda-forge
pip install ordered_set

Please refer to the environment.yml file for an exhaustitve list.

Adaptive subgraphs

The subgraphs collected by our adaptive subgraph gathering strategy (Sec 3.2) of the paper for all the datasets used in the paper can be downloaded from this link. Download and uncompress the file and you should be able to see a directory for each of the datasets. In each of the directory, you should be able to see the following files {train|valid|dev}_roberta-base_mean_pool_masked_cbr_subgraph_k={25|75}.json. The subgraph for each query is present in these files. Note, that they are mapped to integers - to convert them to strings (i.e. KBids) refer to the {entities, relations}_roberta-base_mean_pool_masked_cbr_subgraph_k={25|75}.txt

Parsing the file name

Lets consider the filename train_roberta-base_mean_pool_masked_cbr_subgraph_k=25.json.

train - refers to the fact this file denotes the train set

roberta-base_mean_pool_masked - refers to the fact that to obtain the KNN questions, the question representations were obtained by using a roberta-base model followed by a mean pool over the token representations in the last layer. Moreover masked denotes that the entity mentions in each question were replaced by the token (refer to sec 3.1 of the paper for details)

k=25 - means that to obtain the subgraphs, we used paths collected from 25 KNN queries (refer to sec 3.2 of the paper)

Collecting your own subgraphs

Please refer to the Readme in the adaptive_subgraph_collection directory.

Training

The runner.py file is the main file that is needed to run the code. A few of the popular flags are listed below.

python runner.py 
--do_train # flag for training
--do_predict # flag for prediction
--add_dist_feature=1 # add distance feature to node embeddings
--add_inv_edges_to_edge_index=1 # add inverse KB edges
--data_dir=<path_to_data_dir> 
--data_file_suffix=roberta-base_mean_pool_masked_cbr_subgraph_k=25 # which subgraph file to use?
--dataset_name=webqsp # dataset name
--dist_metric=cosine # distance metric to use (l2 or cosine)
--eval_batch_size=1 # batch size during eval
--eval_steps=100 # how many gradient steps before evaluating on validation
--gradient_accumulation_steps=4 # gradient accumulation steps
--learning_rate=0.0024129869604528702 # learning rate
--logging_steps=10 
--loss_metric=txent # loss fn (tx-ent/margin)
--max_grad_norm=1 
--num_gcn_layers=3 # num GCN layers
--num_neighbors_eval=5 # number of KNNs at eval
--num_neighbors_train=10 # number of KNNs during train
--num_train_epochs=30 
--output_dir=<path_to_output_models> 
--temperature=0.06453104622317246 # temperature in loss (txent)
--train_batch_size=1 # train batch size
--transform_input=1 # add linear layer after sparse input embedding layer
--use_fast_rgcn=1 # use fast_rgcn
--use_wandb=1 # use wandb?

Pre-trained models and collected subgraphs

The pre-trained models and subgraphs can be downloaded from here. The query specific subgraphs are present in the input json files.

The commands to reproduce the results from the paper are present here.

Citation

If you use the code, data (e.g. subgraphs) or models, we would be grateful if you cite

@inproceedings{cbr_subg,
title={Knowledge Base Question Answering by Case-based Reasoning over Subgraphs},
author={Das, Rajarshi and Godbole, Ameya and Naik, Ankita and Tower, Elliot and Jia, Robin and Zaheer, Manzil and Hajishirzi, Hannaneh and McCallum, Andrew},
booktitle={ICML},
year={2022}
}

cbr-subg's People

Contributors

rajarshd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cbr-subg's Issues

When will this code be open sourced?

Hello, I just read your paper a few days ago and am very interested in it, but there are still a few places I don’t understand very well. I hope to deepen my understanding by reading the code. When will this code be open sourced?

about learning rate

1.Thanks a lot for the code you shared, I would like to know why you set the learning rate to 0.0024129869604528702(WebQSP) or 0.000999196499343006 (MetaQA-3)
2.how to get knn_file ? when run adaptive_graph_collection.py?

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.