Git Product home page Git Product logo

relphormer's Introduction

Relphormer

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.

Model Architecture

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.

Environments

  • python (3.8.18)
  • Ubuntu-18.04.6 (4.15.0-156-generic)

Requirements

To run the codes, you need to install the requirements:

pip install -r requirements.txt

The 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

How to run

KGC Task

Pre-trained Model Preparation

  • 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.json
wget https://hf-mirror.com/bert-base-uncased/resolve/main/pytorch_model.bin
wget https://hf-mirror.com/bert-base-uncased/resolve/main/tokenizer.json
wget https://hf-mirror.com/bert-base-uncased/resolve/main/tokenizer_config.json
wget https://hf-mirror.com/bert-base-uncased/resolve/main/vocab.txt

Entity Embedding Initialization

  • 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-237 and wn18rr , just replace the dataset name with fb15k-237 and wn18rr will be fine.
>> cd Relphormer/
>> bash scripts/fb15k-237/pretrain_fb15k.sh

The pretrained models are saved in the Relphormer/pretrain/output directory.

For convenience, we provide users with our processed masked files and pre-trained checkpoints, you can download from here.

Entity Prediction

  • 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 the Relphormer/output directory.

QA Task

The experimental settings in QA follow the Hitter experimental settings, and the environment installation can be done by referring to GitHub. We only modified hitter-best.py to fit our model.

  • The relphormer model used by QA can be downloaded here.
>> cd QA
>> sh scripts/relphormer_fbqa.sh
>> sh scripts/relphormer_fbqa_filtered.sh 
>> sh scripts/relphormer_webqsp.sh
>> sh scripts/relphormer_webqsp_filtered.sh

Citation

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},
}

relphormer's People

Contributors

bizhen46766 avatar cheng-siyuan avatar litmirror123 avatar zxlzr 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

relphormer's Issues

about running time

Hello, I am interested in your paper. I would like to ask you about the approximate time for this code to run in each task. How much GPU memory is consumed?

some questions

在运行fb15k.sh文件时出现以下问题,请问这个问题是因为什么呀?该如何解决呜呜呜
问题如下:
Traceback (most recent call last):
File "main.py", line 149, in
main()
File "main.py", line 77, in main
parser = _setup_parser()
File "main.py", line 54, in _setup_parser
model_class = _import_class(f"models.{temp_args.model_class}") # 通过 _import_class 函数,从 'models' 模块导入指定类,类名由 temp_args.model_class 提供
File "main.py", line 24, in import_class
class
= getattr(module, class_name)
AttributeError: module 'models' has no attribute 'Relphormer'

What are the main differences between Relphormer and Kgtrransformer

Hi! The paper: "Mask and Reason: Pre-Training Knowledge Graph Transformers for Complex Logical Queries" was published as a KDD'22 paper, which proposes 'Kgtransformer'.

It seems that Triple2seq implemented in your code is similar as the mask mechanism in 'Kgtransformer'. For Link Prediction, Sampling neighbors of center/query entity, then mask the query and recover, delete the nodes about origin query triplet information to avoid leakage.

both follow the pre-training LM(BERT) for semantic text, then fine-tuning workflow.

The loss targets for Relphormer and Kgtransformer both include MKM loss and contextual loss(similar formula).

I am looking forward to your help to explain what are the main differences/advances between Relphormer and Kgtransformer?

Thanks a lot for your answering.

About entity one-hop generate and fair comparision

In the create_neighbor.py, the paper generated the graph structure sequence using triples from the train, valid, and test datasets. I worried this might cause test dataset leakage in the training process. Is it more reasonable to only use the training dataset in the training process?

And another question: in your comparison experiment, you cited the HittER model. However, when I read the codes, the HittER model only used the graph structure nodes from the training dataset. So, is the comparison fair with the HittER model in your experiment? Not only HittER, in another work's code(IMF: Interactive Multimodal Fusion Model for Link Prediction) using graph structure for KG, also only uses the training dataset.

Looking forward to your reply. Thx!

triple2seq process

Hello!

Can you please point out where in the code the process of triple2seq which is supposed to happen during each epoch, happening? I searched the repo, but I can't seem to find it.

Thanks a lot.

关于数据集

您好,在您的数据集中存在text long部分,请问在链接预测任务中该文件是必要的吗?如果必要该文件有何作用,同时我注意到关于KG BERT的数据集中均存在该文件,是否用到BERT均需要该文件?那该文件有何要求吗,是否text中只需要包含实体即可?期待收到您的回答

dependency issues

Hi, thanks for sharing the source code! Could you please provide a requirement.txt about library and their versions .I had a big trouble when running the project for the dependency issues. Thank a lot.

About structrue-enhanced self-attention

I saw in your paper that the adjacency matrix is generated from the central triplet as structural information and added to self-attention, because your code is very confusing and I can't find the corresponding code, can you tell me the code for this part where is it?
image

Linking prediction

Hello, I found the following error while performing a link prediction task, how can I change the code?
image

关于KeyError: 'label'的问题

你好,最近在运行您的代码,Epoch0的features后一半数据是没有lables的,所以Epoch 0 进行到50%的时候,出现KeyError: 'label',查看了代码后发现是data_module.py的DataCollatorForSeq2Seq类的__init__方法中features处理问题,当lables不存在的时候产生错误,我在运行KNN-KG的时候遇见了同样的问题,请问有什么解决方案吗,错误提示如下:
File "C:\Users\admin\Desktop\code\Relphormer-main1\Relphormer-main\pretrain\data\data_module.py", line 88, in
label = [feature.pop("label") for feature in features]
KeyError: 'label'
Epoch 0: 50%|█████ | 10280/20558 [41:37<41:37, 4.12it/s, loss=6.37, v_num=3, hits10=0.000, hits1=0.000]

1

我在问答部分的环境配置中遇到了一些问题,请问是否有关于问答任务环境配置的具体内容,尤其是关于kge的版本及所需要的相关内容的版本

conda environment file

Hello,
Do you have a conda environment file for this project to share? I am having a lot of problems trying to resolve conflicts and many sorts of errors due to that.

It'd be great if you can share that!
Thanks in advance,

Question about examples.

I have a question about examples.

example file is basically made up of dictionary and there is a key 'en_id'. I thought the 'en_id' corresponds with 'en'. The relation id and entities are matched, on the other hand the head or tail entities and their id are not matched in example file. Also i don't understand that the length of the fb15k-237 based on the entity2id file is 14541. But i added a line "print(num_entities)" on the processor.py file to check the length of the fb15k-237 is 14951. Could you explain?

About Triple2Seq part

I saw that in the Triple2Seq section, it is mentioned that the neighbors are triple-level, but in the preprocessing code.

def get_triplet(triplet):
In fact, it is entity-level. For example, for [0, 1, mask_id] and [0, 2, mask_id], the corresponding get_triplet output is exactly the same. If it is triple-level, because the relations of the two triplet are different, and the output should be different. Can you explain this?

Issue while saving model for Entity Embedding Initialization

Hello.. I was trying to run your repository for UMLS.
I am facing few issues:

  1. I had you git cloned 3 weeks back and the entity embedding part was fine and the I tried to execute Entity Prediction but I has issue for tex_d missing from convert_examples_to_features function. From the reading #5 I thought of updating the repo on my end.
  2. Now I have updated I am not able to save the model in Entity Embedding Initialization where bash scripts/pretrain_umls.sh is not saming the best model. The variable path desn't have the location to save.
  3. Also when I found the three files features_train.txt features_tvalid.txt and features_test.txt empty for me during this experiment with umls.

Can you please help me with the it and let me know the best way to run your repo for UMLS. My goal is to try it for link prediction for UMLS. Thank you for your repo.

BrokenPipe

image

BrokenPipe error when running as instructed?

Error: masked_head_neighbor.txt not found - How to reproduce recommendation task

I love how Relphormer utilizes text in addition to graph information. I would like to train a model using Relphormer that reproduces your results for the recommendation task.
However, I am hitting an error.

Here is a notebook that reproduces the problem: https://colab.research.google.com/drive/1PHGALZo6AkimU5jEq0I__lnoJaWxUhgg?usp=sharing
(Run in a T4 GPU runtime.)

It fails at this step:
trainer.fit(lit_model, datamodule=data)
with the following stake trace:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
[<ipython-input-7-aea1f0bf8188>](https://localhost:8080/#) in <cell line: 14>()
     12 )
     13 
---> 14 trainer.fit(lit_model, datamodule=data)
     15 
     16 # Explore lit_model and its hidden states or proceed with further actions as needed

10 frames
[/content/Relphormer/data/processor.py](https://localhost:8080/#) in _create_examples(self, lines, set_type, data_dir, args)
    435         '''
    436         if not args.pretrain:
--> 437             with open(os.path.join(data_dir, "masked_head_neighbor.txt"), 'r') as file:
    438                 masked_head_neighbor = json.load(file)
    439             with open(os.path.join(data_dir, "masked_tail_neighbor.txt"), 'r') as file:

FileNotFoundError: [Errno 2] No such file or directory: '/content/Relphormer/dataset/fb15k-237/masked_head_neighbor.txt'

I dug into where this masked_head_neighbor.txt file is loaded and written. This is where I am confused.

  1. The stack trace is trying to read the file from processor.py (https://github.com/devinbost/Relphormer/blob/feddb75f4e6e344d3daa83d8048127c3537b2baa/data/processor.py#L436):
        if not args.pretrain:
            with open(os.path.join(data_dir, "masked_head_neighbor.txt"), 'r') as file:
                masked_head_neighbor = json.load(file)
            with open(os.path.join(data_dir, "masked_tail_neighbor.txt"), 'r') as file:
                masked_tail_neighbor = json.load(file)
            print(f'\n \t Not pre-training stage, masked subgraphs loaded.')
        else:
            masked_head_neighbor = []
            masked_tail_neighbor = []

However, even if I set args.pretrain = None, it still appears to be trying to read the "masked_head_neighbor.txt" file.

  1. On the write path, the files is written at the global scope in create_neighbor.py (https://github.com/devinbost/Relphormer/blob/10e2fbfcf8a2aed0d9b50f6e896bf9892991b9f2/dataset/create_neighbor.py#L148). However, the only references to create_neighbor appear to be in the README:
image

So, I have two questions:

  1. If I need this masked_head_neighbor.txt file, how do I create it?
  2. If I don't need this file, how can I skip it?

Thanks for all your great work on this project. I'm really looking forward to getting it working.

The pre-train result is unsatisfactory with default parameter.

In pre-training of umls with default parameter, the result is as followed:

{'Test/hits1': 0.00202020202020202,
'Test/hits10': 0.06868686868686869,
'Test/hits20': 0.13198653198653199,
'Test/hits3': 0.017508417508417508,
'Test/mean_rank': 70.0895622895623,
'Test/mrr': 0.034388057056645285}

In pre-training of WN18RR with default parameter, the result is as followed:

{'Test/hits1': 3.552610835907126e-05,
'Test/hits10': 0.00027088657623791835,
'Test/hits20': 0.0005639769702002563,
'Test/hits3': 7.993374380791033e-05,
'Test/mean_rank': 20417.76962206882,
'Test/mrr': 0.00030194088791004475}

how to run

Hi, another error [Errno 2] No such file or directory: 'dataset/FB15k-237/masked_head_neighbor.txt' occurred after I solved the dependency issue.Actually, there is no such file in dataset.So, I wonder if the project you upload is complete and can be run, if yes, could you tell about how to run it.Thanks!

关于代码复现与实现中的问题

您好!请问:

  1. 在运行fb15k.sh时,现有的项目中导入没有找到Relphormer,请问怎么解决呢?
    具体显示如下:
    Traceback (most recent call last):
    File "main.py", line 149, in
    main()
    File "main.py", line 77, in main
    parser = _setup_parser()
    File "main.py", line 54, in _setup_parser
    model_class = _import_class(f"models.{temp_args.model_class}") # 通过 _import_class 函数,从 'models' 模块导入指定类,类名由 temp_args.model_class 提供
    File "main.py", line 24, in import_class
    class = getattr(module, class_name)
    AttributeError: module 'models' has no attribute 'Relphormer'
  2. 文章中提到了Structure-enhanced self-attention,在代码运行过程中,只发现了使用algos.floyd_warshall获取到了最短路径,但后续代码处理尤其是在huggingface_relformer模型文件中并没有找到相关的使用步骤,请问具体在什么地方实现的呢?
  3. 同样的提到的使用了contextual_loss,也没有找到相关实现,请问具体在什么地方实现的呢?
    谢谢!!!

似乎现有的pipeline不能跑通

运行更新后的create_neighbors.py,现有的pipeline报错:
image
似乎是test里没有出现在train里的查询,没有对应的子图储存在masked_neighbor文件中

Can't pickle lambda object

Hi, i'm having a problem while training fb15k237 using default parameters. Any suggestions? thanks

Can't pickle local object 'get_linear_schedule_with_warmup..lr_lambda'
File "/Relphormer/main.py", line 126, in main
trainer.fit(lit_model, datamodule=data)
File "/Relphormer/main.py", line 139, in
main()
AttributeError: Can't pickle local object 'get_linear_schedule_with_warmup..lr_lambda'

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.