zjunlp / Relphormer

[Neurocomputing 2023] Relational Graph Transformer for Knowledge Graph Representation
MIT License
124 stars 10 forks source link

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

Closed Hakstar closed 9 months ago

Hakstar commented 9 months ago

您好!请问:

  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,也没有找到相关实现,请问具体在什么地方实现的呢? 谢谢!!!
zxlzr commented 9 months ago

您好,这个工作是2年前做的,可能之前整理的代码遗漏了一些bug,马上会有同学回复您的问题。

zxlzr commented 9 months ago

您好!请问:

  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,也没有找到相关实现,请问具体在什么地方实现的呢? 谢谢!!!

您好,我粗略看了一下,因为不是我开发的不知道回答是否准确仅供您参考。开发同学因有急事近期可能无法直接回答您的问题,非常抱歉。

问题2: https://github.com/zjunlp/Relphormer/blob/main/data/processor.py#L920

bizhen46766 commented 9 months ago

您好呀,非常抱歉给您带来困扰呀

  1. 关于attributeError: module 'models' has no attribute 'Relphormer'的问题,应该是文件或者模块命名的问题,我们这边会尽快修改更新;
  2. 关于algos.floyd_warshall处理结果,如上个回复所示,是作为参数传到了transformer的模块里面;
  3. 关于contextual_loss,复现实验中我们发现加上对比学习loss之后模型在一些比较大的图谱上迭代收敛速度会很慢;为了快速得到论文中接近的实验结果,当前版本并没有包含,后续我们会整合成可选择的模块;
zxlzr commented 9 months ago

您好,如果您还有其他问题欢迎随时提问,这个项目有点历史悠久了,非常抱歉给您带来了不便。

Hakstar commented 9 months ago

非常感谢您的解答!关于问题2我注意到在生成最短路径之后是作为distance_attention作为feature,经过处理后传入到了transformer模块中(https://github.com/zjunlp/Relphormer/blob/7ad8823d521b37f23f1fa142c7699460a7e3de75/lit_models/transformer.py#L125),但是在huggingface_relphomor.py文件中并没有找到关于distance_attention的特殊运用(https://github.com/zjunlp/Relphormer/blob/7ad8823d521b37f23f1fa142c7699460a7e3de75/models/huggingface_relformer.py#L927),请问这个与文章中提到的Structure-enhanced self-attention有关呢?同时请问Structure-enhanced self-attention具体是在哪部分代码中实现的呢?再次感谢您的解答!

zxlzr commented 9 months ago

非常感谢您的解答!关于问题2我注意到在生成最短路径之后是作为distance_attention作为feature,经过处理后传入到了transformer模块中并且是作为attention_mask参数传入的(https://github.com/zjunlp/Relphormer/blob/7ad8823d521b37f23f1fa142c7699460a7e3de75/lit_models/transformer.py#L125),但是在huggingface_relphomor.py文件中并没有找到关于attention_mask的特殊运用,请问这个特殊的attention_mask是否与文章中提到的Structure-enhanced self-attention有关呢?同时请问Structure-enhanced self-attention具体是在哪部分代码中实现的呢?再次感谢您的解答!

您好,self-attention中有一个attention mask,这个是transformers的标准模块,可以控制token和token之间是否可见,structure信息已经通过attention mask传进去了。

Hakstar commented 9 months ago

非常感谢您的解答!关于问题2我注意到在生成最短路径之后是作为distance_attention作为feature,经过处理后传入到了transformer模块中并且是作为attention_mask参数传入的(https://github.com/zjunlp/Relphormer/blob/7ad8823d521b37f23f1fa142c7699460a7e3de75/lit_models/transformer.py#L125),但是在huggingface_relphomor.py文件中并没有找到关于attention_mask的特殊运用,请问这个特殊的attention_mask是否与文章中提到的Structure-enhanced self-attention有关呢?同时请问Structure-enhanced self-attention具体是在哪部分代码中实现的呢?再次感谢您的解答!

您好,self-attention中有一个attention mask,这个是transformers的标准模块,可以控制token和token之间是否可见,structure信息已经通过attention mask传进去了。

感谢您的解答!由于现在无法复现kgc finetune部分的训练,所以没能看到完整的distance_attention的计算流。目前我看到的distance_attention是通过algos.floyd_warshall计算之后得到的其中中矩阵参数代表节点之间最短路径长度,随后作为feature并进行padding,padding中似乎是以-inf作为value的。数据进入模型之后,在BertModel中赋值给了extended_attention_mask并作为attention_mask直接传入encoder中,encoder中直接使用softmax进行计算,感觉这个计算方式与论文中提到的Structure-enhanced self-attention计算方式不太一样呢TAT。我的理解或许太浅显了,还希望能够解答一下!非常感谢!!!

bizhen46766 commented 9 months ago

目前在传入bert module里面的structure bias,我们这边对结构特征做了归一化之后作为参数传入进去; 这边其实是一个超参数设置,您可以额外设置一个线性层对structure bias做一层网络的映射,或者作为参数覆盖直接传进去; 当前开源的实现方式是直接归一化特征传入进去的,也可以根据实际情况做更复杂的特征处理(比如添加线性层,或者取邻接矩阵的多次方); 最后padding都是-inf作为value的,因为不参与具体的attention计算;

抱歉目前这个代码可能框架太老了,所以比较难读;如果您这边后续要进一步的验证,最好使用最新的huggingface的transformer做更改。

具体可以加微信(bizhen_zju)交流呀

zxlzr commented 9 months ago

您好,这部分主要对应论文的公式3,structure信息就是那个phi偏置项,通过attention mask是最简单的实现,这个技术在多个相关论文里都有介绍的,例如您可以参阅Do Transformers Really Perform Bad for Graph Representation?

Hakstar commented 9 months ago

非常感谢您的解答!!!应该是我的理解还不够深!!!谢谢!!!