【问题标题】:Training custom NER model训练自定义 NER 模型
【发布时间】:2019-12-05 04:11:01
【问题描述】:

我一直在用一些文本训练我的 NER 模型,并尝试使用自定义实体在其中找到城市。

例子:-

    ('paragraph Designated Offices Party A New York Party B Delaware paragraph pricing source calculation Market Value shall generally accepted pricing source reasonably agreed parties paragraph Spot rate Spot Rate specified paragraph reasonably agreed parties',
  {'entities': [(37, 41, 'DesignatedBankLoc'),(54, 62, 'CounterpartyBankLoc')]})

我在这里寻找 2 个实体 DesignatedBankLocCounterpartyBankLoc。单个文本也可以有多个实体。

目前我正在对 60 行数据进行如下训练:

import spacy
import random
def train_spacy(data,iterations):
    TRAIN_DATA = data
    nlp = spacy.blank('en')  # create blank Language class
    # create the built-in pipeline components and add them to 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, last=True)


    # add labels
    for _, annotations in TRAIN_DATA:
         for ent in annotations.get('entities'):
            # print (ent[2])
            ner.add_label(ent[2])

    # get names of other pipes to disable them during training
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
    with nlp.disable_pipes(*other_pipes):  # only train NER
        optimizer = nlp.begin_training()
        for itn in range(iterations):
            print("Statring iteration " + str(itn))
            random.shuffle(TRAIN_DATA)
            losses = {}
            for text, annotations in TRAIN_DATA:
                nlp.update(
                    [text],  # batch of texts
                    [annotations],  # batch of annotations
                    drop=0.5,  # dropout - make it harder to memorise data
                    sgd=optimizer,  # callable to update weights
                    losses=losses)
            print(losses)
    return nlp


prdnlp = train_spacy(TRAIN_DATA, 100)

我的问题是:-

当输入不同/相同的文本模式包含训练有素的城市时,模型预测正确。 模型不会预测任何实体,即使相同/不同的文本模式但不同的城市也不会出现在训练数据集中。

请告诉我为什么会这样,请让我理解它是如何得到训练的概念?

【问题讨论】:

    标签: python machine-learning nltk spacy named-entity-recognition


    【解决方案1】:

    根据经验,您有 60 行数据并训练 100 次迭代。您过度拟合实体的值而不是它们的位置。

    要检查这一点,请尝试在句子中的随机位置注入城市名称,看看会发生什么。如果算法标记了它们,你可能会过拟合。

    有两种解决方案:

    • 为这些实体创建更多具有更多不同值的训练数据
    • 测试不同的迭代次数

    【讨论】:

    • 感谢您的回复,我想知道 drop ,迭代次数如何影响模型以及如何检查过度拟合?
    • 我已经尝试过使用相同迭代但下降值不同的训练模型我在两种情况下都有损失我如何比较它并查看哪个效果更好?
    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多