跳转到主要内容

标签(标签)

资源精选(342) Go开发(108) Go语言(103) Go(99) angular(82) LLM(75) 大语言模型(63) 人工智能(53) 前端开发(50) LangChain(43) golang(43) 机器学习(39) Go工程师(38) Go程序员(38) Go开发者(36) React(33) Go基础(29) Python(24) Vue(22) Web开发(20) Web技术(19) 精选资源(19) 深度学习(19) Java(18) ChatGTP(17) Cookie(16) android(16) 前端框架(13) JavaScript(13) Next.js(12) 安卓(11) typescript(10) 资料精选(10) NLP(10) 第三方Cookie(9) Redwoodjs(9) LLMOps(9) Go语言中级开发(9) 自然语言处理(9) 聊天机器人(9) PostgreSQL(9) 区块链(9) mlops(9) 安全(9) 全栈开发(8) ChatGPT(8) OpenAI(8) Linux(8) AI(8) GraphQL(8) iOS(8) 软件架构(7) Go语言高级开发(7) AWS(7) C++(7) 数据科学(7) whisper(6) Prisma(6) 隐私保护(6) RAG(6) JSON(6) DevOps(6) 数据可视化(6) wasm(6) 计算机视觉(6) 算法(6) Rust(6) 微服务(6) 隐私沙盒(5) FedCM(5) 语音识别(5) Angular开发(5) 快速应用开发(5) 提示工程(5) Agent(5) LLaMA(5) 低代码开发(5) Go测试(5) gorm(5) REST API(5) 推荐系统(5) WebAssembly(5) GameDev(5) CMS(5) CSS(5) machine-learning(5) 机器人(5) 游戏开发(5) Blockchain(5) Web安全(5) Kotlin(5) 低代码平台(5) 机器学习资源(5) Go资源(5) Nodejs(5) PHP(5) Swift(5) 智能体(4) devin(4) Blitz(4) javascript框架(4) Redwood(4) GDPR(4) 生成式人工智能(4) Angular16(4) Alpaca(4) SAML(4) JWT(4) JSON处理(4) Go并发(4) kafka(4) 移动开发(4) 移动应用(4) security(4) 隐私(4) spring-boot(4) 物联网(4) nextjs(4) 网络安全(4) API(4) Ruby(4) 信息安全(4) flutter(4) 专家智能体(3) Chrome(3) CHIPS(3) 3PC(3) SSE(3) 人工智能软件工程师(3) LLM Agent(3) Remix(3) Ubuntu(3) GPT4All(3) 软件开发(3) 问答系统(3) 开发工具(3) 最佳实践(3) RxJS(3) SSR(3) Node.js(3) Dolly(3) 移动应用开发(3) 编程语言(3) 低代码(3) IAM(3) Web框架(3) CORS(3) 基准测试(3) Go语言数据库开发(3) Oauth2(3) 并发(3) 主题(3) Theme(3) earth(3) nginx(3) 软件工程(3) azure(3) keycloak(3) 生产力工具(3) gpt3(3) 工作流(3) C(3) jupyter(3) 认证(3) prometheus(3) GAN(3) Spring(3) 逆向工程(3) 应用安全(3) Docker(3) Django(3) R(3) .NET(3) 大数据(3) Hacking(3) 渗透测试(3) C++资源(3) Mac(3) 微信小程序(3) Python资源(3) JHipster(3) 大型语言模型(2) 语言模型(2) 可穿戴设备(2) JDK(2) SQL(2) Apache(2) Hashicorp Vault(2) Spring Cloud Vault(2) Go语言Web开发(2) Go测试工程师(2) WebSocket(2) 容器化(2) AES(2) 加密(2) 输入验证(2) ORM(2) Fiber(2) Postgres(2) Gorilla Mux(2) Go数据库开发(2) 模块(2) 泛型(2) 指针(2) HTTP(2) PostgreSQL开发(2) Vault(2) K8s(2) Spring boot(2) R语言(2) 深度学习资源(2) 半监督学习(2) semi-supervised-learning(2) architecture(2) 普罗米修斯(2) 嵌入模型(2) productivity(2) 编码(2) Qt(2) 前端(2) Rust语言(2) NeRF(2) 神经辐射场(2) 元宇宙(2) CPP(2) 数据分析(2) spark(2) 流处理(2) Ionic(2) 人体姿势估计(2) human-pose-estimation(2) 视频处理(2) deep-learning(2) kotlin语言(2) kotlin开发(2) burp(2) Chatbot(2) npm(2) quantum(2) OCR(2) 游戏(2) game(2) 内容管理系统(2) MySQL(2) python-books(2) pentest(2) opengl(2) IDE(2) 漏洞赏金(2) Web(2) 知识图谱(2) PyTorch(2) 数据库(2) reverse-engineering(2) 数据工程(2) swift开发(2) rest(2) robotics(2) ios-animation(2) 知识蒸馏(2) 安卓开发(2) nestjs(2) solidity(2) 爬虫(2) 面试(2) 容器(2) C++精选(2) 人工智能资源(2) Machine Learning(2) 备忘单(2) 编程书籍(2) angular资源(2) 速查表(2) cheatsheets(2) SecOps(2) mlops资源(2) R资源(2) DDD(2) 架构设计模式(2) 量化(2) Hacking资源(2) 强化学习(2) flask(2) 设计(2) 性能(2) Sysadmin(2) 系统管理员(2) Java资源(2) 机器学习精选(2) android资源(2) android-UI(2) Mac资源(2) iOS资源(2) Vue资源(2) flutter资源(2) JavaScript精选(2) JavaScript资源(2) Rust开发(2) deeplearning(2) RAD(2)

标记单词的顺序-简洁明了。

NLP

Figure 1: Colour-coded recognised entities

这篇文章假设读者对从文本中提取实体有一些概念,并希望进一步了解新的自定义实体识别的最先进技术以及如何使用这些技术。然而,如果你是NER问题的新手,请在这里阅读。

话虽如此,这篇文章的目的是描述spaCy的预训练自然语言处理(NLP)核心模型用于学习识别新实体的使用。来自spacy的现有核心NLP模型被训练来识别各种实体,如图2所示。

NLP

Figure 2: Existing entity recognised by spaCy core models (source)

尽管如此,用户可能希望构建自己的实体来解决问题需求。在这种情况下,预先存在的实体会使自己变得不足,因此,需要训练NLP模型来完成这项工作。感谢spaCy的文档和预训练模型,这不是很困难。

如果你不想进一步阅读,而是想学习如何使用它,那么请使用这款jupyter笔记本——它是独立的。无论如何,我也建议浏览一下。

数据预处理

就像任何需要输入和输出才能学习的监督学习算法一样,类似地,这里的输入是文本,输出是根据BILOO编码的,如图3所示。然而,尽管存在不同的方案,Ratinov和Roth表明,最小Begin,In,Out(IOB)方案比明确标记边界令牌的BILOO方案更难学习。spaCy提供了一个IOB编码的例子,我发现它与所提供的参数一致。因此,从这里开始,任何提及注释方案的内容都将是BILOO。

NLP

Figure 3: BILUO scheme

下图显示了BILOO编码实体的简短示例。

NLP

要用BILOO方案对您进行编码,有三种可能的方法。其中一种方法是创建一个spaCy-doc表单文本字符串,并将从doc中提取的令牌保存在用新行分隔的文本文件中。然后根据BILOO方案对每个令牌进行标记。人们可以创建自己的令牌并对其进行标记,但可能会降低性能——稍后将对此进行详细介绍。以下是如何标记您的数据,以便以后在NER培训中使用。

import spacy
import numpy as np
import pandas as pd

nlp = spacy.load('en')
text = ("When Sebastian Thrun started working on self-driving cars at "
        "Google in 2007, few people outside of the company took him "
        "seriously. “I can tell you very senior CEOs of major American "
        "car companies would shake my hand and turn away because I wasn’t "
        "worth talking to,” said Thrun, in an interview with Recode earlier "
        "this week.")
doc = nlp(text)
words = []
labels = []

for token in doc:
    words.append(token.text)
    labels.append('O') # As most of token will be non-entity (OUT). Replace this later with actual entity a/c the scheme.

df = pd.DataFrame({'word': words, 'label': labels})
df.to_csv('ner-token-per-line.biluo', index=False) # biluo in extension to indicate the type of encoding, it is ok to keep csv

上面的片段使注释变得更容易,但这不能直接输入到spaCy模型中进行学习。尽管如此,spaCy提供的另一个模块GoldParse解析模型所接受的保存格式。使用下面的snipped从保存的文件中读取数据,并将其解析为模型接受的形式。

dpath = 'ner-token-per-line.biluo'

df = pd.read_csv(dpath, sep=',')
words  = df.word.values
ents = df.label.values
text = ' '.join(words)

from spacy.gold import GoldParse

doc = nlp.make_doc(text)
g = GoldParse(doc, entities=ents)
X = [doc]
Y = [g]

另一种方式是使用每个实体/标签的偏移索引,即实体的开始和结束(即,实体的开始、内部、最后部分结合在一起)的索引与标签一起提供,例如:

text = ("When Sebastian Thrun started working on self-driving cars at "
        "Google in 2007, few people outside of the company took him "
        "seriously. “I can tell you very senior CEOs of major American "
        "car companies would shake my hand and turn away because I wasn’t "
        "worth talking to,” said Thrun, in an interview with Recode earlier "
        "this week.")
g = {'entities': [(5, 20, 'PERSON'), (61, 67, 'ORG'), (71, 75, 'DATE'), (173, 181, 'NORP'), 
          (271, 276, 'PERSON'), (299, 305, 'ORG'), (306, 323, 'DATED')]}

X = [text]
Y = [g]

第三个与第一个类似,只是在这里我们可以修复自己的令牌并标记它们,而不是用NLP模型生成令牌然后标记它们。然而,在我的实验中,虽然这也可以起作用,但我发现这会降低性能。尽管如此,以下是您可以做到的方法:

# words = <list of user token> for example space splitted tokens  
spaces = [True]*len(words)
spaces[-1] = False # so remove space in last
doc = Doc(nlp.vocab, words=words, spaces=spaces) # Custom Doc
g = GoldParse(doc, entities=ents)
X = [doc]
Y = [g]

训练

在对数据进行预处理并准备好进行训练后,我们需要在模型NER管道中进一步添加新实体的词汇表。核心spaCy模型有三个管道:Tagger、Parser和NER。此外,我们需要禁用标记器和解析器管道,因为我们将只训练NER管道,尽管可以同时训练所有其他管道。点击此处了解更多信息。

add_ents = ['DATED'] # The new entity
# Piplines in core pretrained model are tagger, parser, ner. Create new if blank model is to be trained using `spacy.blank('en')` else get the existing one.
if "ner" not in nlp.pipe_names:
    ner = nlp.create_pipe("ner") # "architecture": "ensemble" simple_cnn ensemble, bow # https://spacy.io/api/annotation
    nlp.add_pipe(ner)
else:
    ner = nlp.get_pipe("ner")
prev_ents = ner.move_names # All the existing entities recognised by the model
print('[Existing Entities] = ', ner.move_names)
for ent in add_ents:
    ner.add_label(ent)
    
new_ents = ner.move_names
# print('\n[All Entities] = ', ner.move_names)
print('\n\n[New Entities] = ', list(set(new_ents) - set(prev_ents)))
## Training
model = None # Since we are training a fresh model not a saved model
n_iter = 20
with nlp.disable_pipes(*other_pipes):  # only train ner
    # optimizer = nlp.begin_training()
    if model is None:
        optimizer = nlp.begin_training()
    else:
        optimizer = nlp.resume_training()
    for i in range(n_iter):
        losses = {}
        nlp.update(X, Y,  sgd=optimizer, drop=0.0, losses=losses)
        # nlp.entity.update(d, g)
        print("Losses", losses)

在这里,对于训练,丢弃被指定为0.0,以故意过度拟合模型,并表明它可以学习识别所有新实体。

推论

训练模型的结果:

doc = nlp(text)
for ent in doc.ents:
    print(ent.text, ent.label_)
    
# Output  
Sebastian Thrun PERSON
Google ORG
2007 DATE
American NORP
Thrun PERSON
Recode ORG
earlier this week. DATED

spaCy还提供了一种生成颜色编码实体的方法(如图1所示),可以使用以下片段在web浏览器或笔记本电脑中查看这些实体:

from spacy import displacydisplacy.render(doc, style="ent") # if from notebook else displacy.serve(doc, style="ent") generally

注意事项

这里提供的培训新实体的过程可能看起来有点容易,但它确实带来了警告。在训练时,新训练的模型可能会忘记识别旧实体,因此,强烈建议将一些文本与先前训练的实体中的实体混合,除非旧实体对解决问题并不重要。其次,学习更具体的实体可能比学习广义实体更好。

结论

我们看到,开始学习新的实体并不难,但人们确实需要尝试不同的注释技术,并选择最适合给定问题的方法。

其他注意事项

  • 这篇文章进一步扩展了spaCy在这里提供的例子
  • 整个代码块可以在这个jupyter笔记本中访问。自述文件还包含如何安装spaCy库以及在安装和加载预训练模型期间调试错误问题。
  • 阅读Akbik等人的这篇论文。它应该有助于理解序列标记背后的算法,即多个单词实体。

 

文章链接