Spacy 自定义名称实体识别 (NER)“灾难性遗忘”问题

2024-01-23

该模型无法记住之前训练它的标签 我知道这是“灾难性的遗忘”,但似乎没有例子或博客可以帮助解决这个问题。 对此最常见的反应是这个博客是这样的https://explosion.ai/blog/pseudo-rehearsal-catastropic-forgetting https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting但这现在已经很旧了,没有帮助

这是我的代码:

from __future__ import unicode_literals, print_function
import json
labeled_data = []
with open(r"/content/emails_labeled.jsonl", "r") as read_file:
    for line in read_file:
        data = json.loads(line)
        labeled_data.append(data)

TRAIN_DATA = []
for entry in labeled_data:
    entities = []
    for e in entry['labels']:
        entities.append((e[0], e[1],e[2]))
    spacy_entry = (entry['text'], {"entities": entities})
    TRAIN_DATA.append(spacy_entry)       
import plac
import random
import warnings
from pathlib import Path
import spacy
from spacy.util import minibatch, compounding


# new entity label
LABEL = "OIL"

# training data
# Note: If you're using an existing model, make sure to mix in examples of
# other entity types that spaCy correctly recognized before. Otherwise, your
# model might learn the new type, but "forget" what it previously knew.
# https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
'''
TRAIN_DATA = [
    (
        "Horses are too tall and they pretend to care about your feelings",
        {"entities": [(0, 6, LABEL)]},
    ),
    ("Do they bite?", {"entities": []}),
    (
        "horses are too tall and they pretend to care about your feelings",
        {"entities": [(0, 6, LABEL)]},
    ),
    ("horses pretend to care about your feelings", {"entities": [(0, 6, LABEL)]}),
    (
        "they pretend to care about your feelings, those horses",
        {"entities": [(48, 54, LABEL)]},
    ),
    ("horses?", {"entities": [(0, 6, LABEL)]}),
]
'''

@plac.annotations(
    model=("Model name. Defaults to blank 'en' model.", "option", "m", str),
    new_model_name=("New model name for model meta.", "option", "nm", str),
    output_dir=("Optional output directory", "option", "o", Path),
    n_iter=("Number of training iterations", "option", "n", int),
)
def main(model='/content/LinkModelOutput', new_model_name="Oil21", output_dir='/content/Last', n_iter=30):
    """Set up the pipeline and entity recognizer, and train the new entity."""
    random.seed(0)
    if model is not None:
        nlp = spacy.load(model)  # load existing spaCy model
        print("Loaded model '%s'" % model)
    else:
        nlp = spacy.blank("en")  # create blank Language class
        print("Created blank 'en' model")
    # Add entity recognizer to model if it's not in the pipeline
    # nlp.create_pipe works for built-ins that are registered with spaCy
    if "ner" not in nlp.pipe_names:
        ner = nlp.create_pipe("ner")
        nlp.add_pipe(ner)
    # otherwise, get it, so we can add labels to it
    else:
        ner = nlp.get_pipe("ner")

    ner.add_label(LABEL)  # add new entity label to entity recognizer
    # Adding extraneous labels shouldn't mess anything up
    #ner.add_label("VEGETABLE")
    if model is None:
        optimizer = nlp.begin_training()
    else:
        optimizer = nlp.resume_training()
    move_names = list(ner.move_names)
    # get names of other pipes to disable them during training
    pipe_exceptions = ["ner", "trf_wordpiecer", "trf_tok2vec"]
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe not in pipe_exceptions]
    # only train NER
    with nlp.disable_pipes(*other_pipes), warnings.catch_warnings():
        # show warnings for misaligned entity spans once
        warnings.filterwarnings("once", category=UserWarning, module='spacy')

        sizes = compounding(1.0, 4.0, 1.001)
        # batch up the examples using spaCy's minibatch
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            batches = minibatch(TRAIN_DATA, size=sizes)
            losses = {}
            for batch in batches:
                texts, annotations = zip(*batch)
                nlp.entity.update(texts, annotations, sgd=optimizer, drop=0.35, losses=losses)
            print("Losses", losses)

    # test the trained model
    test_text = "Here is Hindustan petroleum's oil reserves coup in Australia. Details can be found at https://www.textfixer.com/tools/remove-line-breaks.php?"
    doc = nlp(test_text)
    print("Entities in '%s'" % test_text)
    for ent in doc.ents:
        print(ent.label_, ent.text)

    # save model to output directory
    if output_dir is not None:
        output_dir = Path(output_dir)
        if not output_dir.exists():
            output_dir.mkdir()
        nlp.meta["name"] = new_model_name  # rename model
        nlp.to_disk(output_dir)
        print("Saved model to", output_dir)

        # test the saved model
        print("Loading from", output_dir)
        nlp2 = spacy.load(output_dir)
        # Check the classes have loaded back consistently
        assert nlp2.get_pipe("ner").move_names == move_names
        doc2 = nlp2(test_text)
        for ent in doc2.ents:
            print(ent.label_, ent.text)


if __name__ == "__main__":
    plac.call(main)

数据注释是在“Daccano”上完成的。 看一下数据:

{"id": 174, "text": "service\tmarathon petroleum reduces service postings marathon petroleum co said it reduced the contract price it will pay for all grades of service oil one dlr a barrel effective today the decrease brings marathon s posted price for both west texas intermediate and west texas sour to dlrs a bbl the south louisiana sweet grade of service was reduced to dlrs a bbl the company last changed its service postings on jan reuter", "meta": {}, "annotation_approver": null, "labels": [[61, 70, "OIL"], [147, 150, "OIL"]]}
{"id": 175, "text": "mutual funds\tmunsingwear inc mun th qtr jan loss shr loss cts vs loss seven cts net loss vs loss revs mln vs mln year shr profit cts vs profit cts net profit vs profit revs mln vs mln avg shrs vs note per shr adjusted for for stock split july and for split may reuter", "meta": {}, "annotation_approver": null, "labels": []}

我不是 spacy 专家,但我也遇到了同样的问题。有一些要点是必需的:注释工具、训练数据量、正确预测实体的混合。 首先确保您的训练数据已通过您选择的工具正确标记(您不会收到用户警告)。为了获得良好的预测,您的模型需要大量数据。这意味着您要训练的每个实体至少有 200 个示例。我个人标记尽可能多的数据。 spacy 的制造商建议混合模型正确预测的实体。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spacy 自定义名称实体识别 (NER)“灾难性遗忘”问题 的相关文章

随机推荐

  • 如何向 GCP 中的 dataproc 集群添加 jar 依赖项?

    特别是 如何添加 Spark bigquery connector 以便可以从 dataproc 的 Jupyter Web 界面中查询数据 关键链接 https github com GoogleCloudPlatform spark b
  • 获取乳胶输出块的高度

    我正在尝试确定如何获得乳胶块的高度output not整个文件 以及not代码 而是一个block of output 作为我想要完成的一个例子 我有乳胶代码 sum i 0 infty frac 1 n gt infty newline
  • 删除不再位于远程的跟踪分支

    有没有一种简单的方法可以删除远程等效项不再存在的所有跟踪分支 Example 分支机构 本地和远程 master 起源 主人 起源 错误修复 a 起源 错误修复 b 起源 错误修复 c 在本地 我只有一个主分支 现在我需要努力错误修复a 所
  • 如何删除布局和背景之间的空间?

    我拥有的是一个相对布局 其中包含其他两个相对布局 每个布局都有图像 我已将每个图像作为其相对布局的背景 但我仍然可以看到图像 布局 和整个父布局之间的空间 那么我怎样才能删除这个空间呢 这是我的 XML 代码
  • Chrome v41+ 显示性能问题:无;在很多节点上

    我最近注意到 Chrome 在申请时出现呕吐现象display none 到很多节点 CodePen 示例 http codepen io mattdietsche pen JomjWx 在上面的 CodePen 中 您可以看到切换时的滞后
  • 基础5和页面打印

    我正在使用 Zurb 基金会 我试图完全按照大屏幕中的外观打印页面 但所有内容都堆积起来 并且浮动错误 通过将 Foundation min css 中出现的每个 屏幕 替换为 打印 屏幕 我成功地在打印页面中添加了网格 问题是现在取的格子
  • 在多租户架构中为每个租户将异常记录在单独的文件中

    我有一个支持多租户的应用程序 即一台服务器和多个数据库 每个租户都有单独的数据库 应用程序中引发的所有异常都将记录在一个日志中 租户 ID 将与异常一起打印 我想在单独的文件中处理它 即为每个租户一个单独的日志文件 这将有助于确定此异常是由
  • 如何从网页复制特定元素

    我的目标是从网页中获取特定的文本区域 想象一下 就好像您能够在页面上的任何位置绘制一个矩形 并且该矩形中的所有内容都将被复制到剪贴板中 我正在使用 FireBug 请随意建议其他解决方案 我已经搜索了插件或书签 但没有找到任何有用的东西 及
  • Android 应用程序白标 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在尝试寻找为 Android 应用程序添加白色标签的最佳方法 基本上我希望能够构建几乎相同应用程序的多个版本 每个版本将具有不同的资源 例如
  • 多个程序集中相同的完全限定类名

    当我们在两个不同的程序集中定义相同的命名空间和类名而不使用 extern 别名来帮助它决定时 NET 编译器 以及运行时的 CLR 如何确定要使用哪个类 考虑这个示例 全部都在一个 cs 文件中 using System namespace
  • 序列化包含 char* 的结构

    我在序列化 char 字符串时遇到错误error C2228 left of serialize must have class struct union我可以使用 std string 然后从中获取 const char 但我需要 cha
  • 如何从 AWS 中的 Athena 获取结果格式 JSON?

    我想从 AWS 中的 Athena 获取结果值格式 JSON 当我从 Athena 中选择时 结果格式如下 test value report 1 test report 2 normal report 3 hard 有没有办法获得 JSO
  • 如何从 AAssetManager 获取 std::basic_istream?

    我正在使用 NDK 我需要读取资源媒体文件 因此 据我了解 为了访问我需要使用的资源AAssetManager 最终我需要得到std basic istream与它一起工作 那么问题来了 如何获得std basic istream from
  • PHP 中有“nullsafe 运算符”吗?

    有没有办法使用某种方式编写以下语句安全导航操作员 http docs codehaus org display GROOVY Operators Operators SafeNavigationOperator echo data gt g
  • 最上面的“固定”位置 div 与非位置 div 一起移动

    考虑以下代码 div width 100 height 64px border 1px solid 000 top fixed position fixed middle fixed position fixed top 64px bott
  • PHP imagick或任何其他工具,如何检测gif文件上是否有可见的透明度

    我正在开发一项可以将 gif 文件转换为 mp4 文件的服务 使用ffmpeg 我的问题是有些 gif 有visible当我将它们转换为 mp4 视频时 透明区域最终会变成白色 为了避免这个问题 我正在尝试检测 gif 是否有visible
  • 发送 HTTP 标头后服务器无法设置状态 - web api CORS

    好吧 我已经为这件事拼尽全力了 设置 我有一个设置了基本身份验证的 Web Api 2 0 项目 我在 web config 中启用了 CORS 我有 ELMAH 日志记录错误 我有一个 DelegatingHandler 处理传入的请求
  • 如何获得一个 ID 来区分类的不同实例?

    假设我有一个类 有两个实例 MyClass a new MyClass MyClass b new MyClass MyClass 有一个方法 PrintUniqueInstanceID void PrintUniqueInstanceID
  • 比较两个数组,删除匹配的项

    我有两个数组 names and employees 其中填充了代表名称的字符串 names是二维的 保存对匿名数组的引用 但我们关心的数据的位置是 names i 0 我想循环遍历 names并找出哪些名字不在 employees 起初我
  • Spacy 自定义名称实体识别 (NER)“灾难性遗忘”问题

    该模型无法记住之前训练它的标签 我知道这是 灾难性的遗忘 但似乎没有例子或博客可以帮助解决这个问题 对此最常见的反应是这个博客是这样的https explosion ai blog pseudo rehearsal catastropic