- Notifications
You must be signed in to change notification settings - Fork12
[Neurocomputing 2023] Relational Graph Transformer for Knowledge Graph Representation
License
zjunlp/Relphormer
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Code for the Neurocomputing 2023 paper: "Relphormer: Relational Graph Transformer for Knowledge Graph Representations".
Transformers have achieved remarkable performance in widespread fields, including natural language processing, computer vision and graph mining. However, vanilla Transformer architectures have not yielded promising improvements in the Knowledge Graph (KG) representations, where the translational distance paradigm dominates this area. Note that vanilla Transformer architectures struggle to capture the intrinsically heterogeneous semantic and structural information of knowledge graphs. To this end, we propose a new variant of Transformer for knowledge graph representations dubbed Relphormer. Specifically, we introduce Triple2Seq which can dynamically sample contextualized sub-graph sequences as the input to alleviate the heterogeneity issue. We propose a novel structure-enhanced self-attention mechanism to encode the relational information and keep the globally semantic information among sub-graphs. Moreover, we propose masked knowledge modeling as a new paradigm for knowledge graph representation learning. We apply Relphormer to three tasks, namely, knowledge graph completion, KG-based question answering and KG-based recommendation for evaluation. Experimental results show that Relphormer can obtain better performance on benchmark datasets compared with baselines.
The model architecture of Relphormer.The contextualized sub-graph is sampled with Triple2Seq, and then it will be converted into sequences while maintaining its sub-graph structure.Next, we conduct masked knowledge modeling, which randomly masks the nodes in the center triple in the contextualized sub-graph sequences.For the transformer architecture, we design a novel structure-enhanced mechanism to preserve the structure feature.Finally, we utilize our pre-trained KG transformer for KG-based downstream tasks.
- python (3.8.18)
- Ubuntu-18.04.6 (4.15.0-156-generic)
To run the codes, you need to install the requirements:
pip install -r requirements.txtThe expected structure of files is:
── Relphormer ├── data ├── dataset │ ├── fb15k-237 │ ├── wn18rr │ ├── umls │ ├── create_neighbor.py ├── lit_models │ ├── _init_.py │ ├── base.py │ ├── transformer.py │ └── utils.py ├── models │ ├── _init_.py │ ├── huggingface_relformer.py │ ├── model.py │ └── utils.py ├── resource │ └── model.png ├── scripts │ ├── fb15k-237 │ └── wn18rr ├── QA ├── main.py └── requirements.txt- First download the pre-trained model.
>>cd Relphormer>> mkdir -p Pre-trained_models/bert-base-uncased>>cd Pre-trained_models/bert-base-uncased
To download the pre-training model, run the following commands.
wget https://hf-mirror.com/bert-base-uncased/resolve/main/config.jsonwget https://hf-mirror.com/bert-base-uncased/resolve/main/pytorch_model.binwget https://hf-mirror.com/bert-base-uncased/resolve/main/tokenizer.jsonwget https://hf-mirror.com/bert-base-uncased/resolve/main/tokenizer_config.jsonwget https://hf-mirror.com/bert-base-uncased/resolve/main/vocab.txt- Then use the command below to add entities to BERT and initialize the entity embedding layer to be used in the later training. For other datasets
fb15k-237andwn18rr, just replace the dataset name withfb15k-237andwn18rrwill be fine.
>>cd Relphormer/>> bash scripts/fb15k-237/pretrain_fb15k.sh
The pretrained models are saved in theRelphormer/pretrain/output directory.
For convenience, we provide users with our processed masked files andpre-trained checkpoints, you can download fromhere.
- Next use the command below to train the model to predict the correct entity in the masked position. Same as above for other datasets.
>>cd Relphormer>> python dataset/create_neighbor.py --dataset"fb15k-237">> bash scripts/fb15k-237/fb15k.sh
The trained models are saved in theRelphormer/output directory.
The experimental settings in QA follow theHitter experimental settings, and the environment installation can be done by referring toGitHub. We only modifiedhitter-best.py to fit our model.
- The relphormer model used by QA can be downloadedhere.
>>cd QA>> sh scripts/relphormer_fbqa.sh>> sh scripts/relphormer_fbqa_filtered.sh>> sh scripts/relphormer_webqsp.sh>> sh scripts/relphormer_webqsp_filtered.sh
If you use the code, please cite the following paper:
@article{BI2023127044,title ={Relphormer: Relational Graph Transformer for knowledge graph representations},journal ={Neurocomputing},pages ={127044},year ={2023},issn ={0925-2312},author ={Zhen Bi and Siyuan Cheng and Jing Chen and Xiaozhuan Liang and Feiyu Xiong and Ningyu Zhang},}
About
[Neurocomputing 2023] Relational Graph Transformer for Knowledge Graph Representation
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
