ERA-CoT: 实体关系推理


✨✨ 欢迎大家来访Srlua的博文(づ ̄3 ̄)づ╭❤~✨✨

🌟🌟 欢迎各位亲爱的读者,感谢你们抽出宝贵的时间来阅读我的文章。

我是Srlua小谢,在这里我会分享我的知识和经验。🎥

希望在这里,我们能一起探索IT世界的奥妙,提升我们的技能。🔮

记得先点赞👍后阅读哦~ 👏👏

📘📚 所属专栏:传知代码论文复现

欢迎访问我的主页:Srlua小谢 获取更多信息和资源。✨✨🌙🌙

​​

​​

目录

1. 论文概述

2. 论文方法

2.1 实体提取:

2.2 显式关系提取:

2.3 隐式关系推断

2.4 关系过滤

2.5 问题回答

3. 实验

3.1 创建虚拟环境并激活

3.2 安装环境依赖

3.3 运行llama2的预测代码

4.关键代码


本文所有资源均可在该地址处获取。

1. 论文概述

该论文提出了一种新的框架,称为基于链式思维的实体关系分析(ERA-CoT),旨在解决涉及多个实体的复杂场景中的推理任务。通过提取文本中的所有实体及其显式关系,并基于这些关系和隐藏信息推断隐式关系,ERA-CoT显著提高了大语言模型(LLMs)的推理能力和问题回答的准确性。实验结果表明,ERA-CoT在各种基准测试中均优于现有的链式思维提示方法,在GPT-3.5上平均提升了5.1%的准确率​​。
本篇在论文代码的基础上增加了llama2模型的相关文件,修改了模型相关代码。

2. 论文方法


论文中提出的ERA-CoT框架包含五个步骤:

2.1 实体提取:

利用大型语言模型(LLMs)的信息提取能力,从文本中提取所有实体及其类型。具体来说,模型接受一个输入句子,利用其命名实体识别(NER)功能预测相应的实体范围和分类。为了提高实体提取的准确性,采用了自一致性(Self-Consistency, SC)方法,多次验证实体提取结果,确保提取的实体可靠。

2.2 显式关系提取:

在零样本设置下,探讨不同实体之间的显式关系。通过LLM的上下文理解能力,从文本中直接提取实体对之间的关系,生成关系三元组。同样地,使用SC方法评估显式关系的可靠性。

2.3 隐式关系推断

基于显式关系和文本中的隐藏信息推断实体之间的隐式关系。通过生成多个可能的隐式关系,并使用模型进行评分,确定这些关系的可靠性。具体来说,通过分析上下文中的隐含信息,推断出未显式提到但可能存在的实体关系。

2.4 关系过滤

使用模型对隐式关系的可靠性进行评分,设定阈值筛除低于阈值的隐式关系。这一步骤确保了最终关系集的高质量和准确性。

2.5 问题回答

基于提取的实体以及获得的显式和隐式关系回答问题。在这个过程中,模型利用之前提取和过滤的关系信息,提供更准确和详细的答案。
通过这些步骤,ERA-CoT框架在处理复杂实体场景中的推理任务时展示了强大的性能和准确性提升​.

3. 实验

论文通过在六个广泛采用的数据集上进行实验,验证了ERA-CoT的有效性,并与四种基线方法进行了对比。结果表明,ERA-CoT在几乎所有基准测试中均表现出色,平均提升了大约5.1%的准确率。在GPT-3.5和Llama-2两种大语言模型上,ERA-CoT在常识推理、数学推理和逻辑推理三种类型的问题上均表现出显著提升,表明增强模型的实体关系理解能力能够显著提高推理能力和问题回答的准确性​​。

3.1 创建虚拟环境并激活

3.2 安装环境依赖


安装成功

3.3 运行llama2的预测代码

4.关键代码

实体提取:
import json

# from config import args
prompt_prefix = '''Given a sentence, possible entities may include:'''

prompt_suffix = ''', Find all entities based on the provided sentence.'''

def get_ner_list(type_list_file):
    try:
        f = open(type_list_file, "r", encoding="utf-8")
        entities = "["
        for idx, entity in enumerate(f):
            entities = entities + entity[:-1] + ","
        entities = entities[:-1] + "]"
        return entities
    except FileNotFoundError as e:
        raise FileNotFoundError('can\'t find the demo file: {}'.format(type_list_file))

def get_ner_prompt(type_list_file):
    ner_prompt = prompt_prefix + get_ner_list(type_list_file) + prompt_suffix
    return ner_prompt

def ner_sentence(ner_prompt, sentence):
    prompt = ner_prompt + "\nSentence: " + sentence + "\nEntity: "
    return prompt

关系提取:
import json
import logging

# from config import args
prompt_prefix = '''Given a sentence, and all entities within the sentence. 
Extract all relationships between entities which directly stated in the sentence.
Every relationship stated as a triple: (E_A, E_B, Relation).\nSentence: '''

prompt_suffix = '''\nRelation: '''

def get_extract_prompt(entities, sent):
    relation_prompt = prompt_prefix + sent + "\nEntities: " + entities + prompt_suffix
    return relation_prompt

关系推理:
import json
import logging

# from config import args
prompt_prefix = '''Given a sentence, all entities, and all explicit relationships within the sentence. 
Infer all possible implicit relationships between entities.
For each pair of entities, infer up to '''

prompt_mid = ''' implicit relationships.
Every relationship stated as a triple: (E_A, E_B, Relation)\nSentence: '''

prompt_suffix = '''\nRelation: '''

def get_infer_num(args):
    return args.infer_num

def get_infer_prompt(args, entities, relation_ext, sent):
    relation_prompt = (prompt_prefix + get_infer_num(args) + prompt_mid
                            + sent + "\nExplicit Relationships:: " + relation_ext
                                + "\nEntities: " + entities + prompt_suffix)
    return relation_prompt

​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值