fewshot_NER:SpanProto: A Two-stage Span-based Prototypical Network for Few-shot Named Entity Recogni

该博客介绍了一种创新的SpanProto模型,用于解决少量NER任务,采用两阶段方法进行跨度提取和提及分类。模型首先通过转换标签为边界矩阵聚焦边界信息,然后利用原型学习捕捉实体语义。在每个episode中,模型更新同一类型实体的原型,并用分类损失函数训练。对于误报,模型将其视为未知类型的特殊实体,避免影响现有原型。

Introduction

我们提出了一个开创性的基于跨度的原型网络(SpanProto),它通过一个两阶段的方法来解决少量的NER问题,包括跨度提取和提及分类。在跨度提取阶段,我们将顺序标签转化为全局边界矩阵,使模型能够专注于明确的边界信息。对于提及分类,我们利用原型学习来捕捉每个标记的跨度的语义表示,并使模型更好地适应小说类实体。

模型分为两个部分,通过表填充方式解决mention识别问题,使用原型网络解决mention分类问题。

Model

在这里插入图片描述
第一部分:mention识别是通过pointer network解决的。损失函数时二分类交叉熵损失函数
在这里插入图片描述

第二部分:原型学习实现关系分类。在每一个episode中,通过平均化相同实体类型的span的表示得到对应class的原型。损失函数是分类损失函数

当识别出的flase positive类型,则将the false positive can be viewed as a special entity mention, which has no type to be assigned in Ttrain, but could be an entity in other
episode data
. In other words, the real type of this
false positive is unknown. Thus, a natural idea is
that we can keep it away from all current prototypes
in the semantic space. S

在这里插入图片描述

### BERT NER Transformer-Based Named Entity Recognition Overview Transformer架构下的命名实体识别(NER)方法,尤其是基于BERT的实现,在处理不平衡数据集方面表现出显著优势[^2]。这些模型通过预训练阶段获取丰富的语义信息,并在微调过程中针对具体任务优化参数。 #### 实现概述 BERT-NER的主要流程如下: - **预训练**:采用大规模无标注文本进行双向编码器表示(Bidirectional Encoder Representations from Transformers),即BERT模型的学习过程。 - **特征提取**:对于给定句子中的每一个token,BERT能够提供深层次的语言理解能力,捕捉到复杂的语法和语义模式。 - **分类层构建**:在BERT之上添加一层或多层全连接神经网络用于预测各个位置上的标签类别,如`O`, `B-Person`, `I-Person`等。 ```python import torch from transformers import BertTokenizer, BertForTokenClassification tokenizer = BertTokenizer.from_pretrained('bert-base-cased') model = BertForTokenClassification.from_pretrained('dbmdz/bert-large-cased-finetuned-conll03-english') def predict_entities(text): inputs = tokenizer.encode_plus( text, add_special_tokens=True, return_tensors="pt" ) outputs = model(**inputs)[0] predictions = torch.argmax(outputs, dim=2) tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) entities = [] current_entity = None for idx, pred in enumerate(predictions[0]): label = model.config.id2label[pred.item()] if not label.startswith("I") and current_entity is not None: entities.append(current_entity) current_entity = None if label != "O": token_text = tokens[idx].replace("##", "") if label.startswith("B"): current_entity = {"entity": label.split("-")[1], "text": token_text} elif label.startswith("I") and current_entity is not None: current_entity["text"] += f" {token_text}" if current_entity is not None: entities.append(current_entity) return entities ``` 此代码片段展示了如何加载预先训练好的BERT模型并应用于新的文本输入以执行命名实体识别的任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YJII

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值