使用 en_trf_bertbaseuncased_lg 模型训练 NER SpaCy

Train NER SpaCy using en_trf_bertbaseuncased_lg model

我目前正在从事 NER 项目,我想通过尝试新的 SpaCy 模型 en_trf_bertbaseuncased_lg 来提高我的 NER 性能,但它给了我错误 KeyError: "[E001] No component 'trf_tok2vec' found in pipeline. Available names: ['ner']"。是不是SpaCy目前不支持这种语言模型的NER?谢谢!

   # 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
        for itn in tqdm(range(n_iter)):
            random.shuffle(train_data_list)
            losses = {}
            # batch up the examples using spaCy's minibatch
            batches = minibatch(train_data_list, size=compounding(8., 64., 1.001))
            for batch in batches:
                texts, annotations = zip(*batch)
                nlp.update(texts, annotations, sgd=optimizer, drop=0.35,
                           losses=losses)
            tqdm.write('Iter: ' + str(itn + 1) + ' Losses: ' + str(losses['ner']))
            if itn == 30 or itn == 40:
                output_dir = Path(output_dir)
                if not output_dir.exists():
                    output_dir.mkdir()
                nlp.to_disk(Path(output_dir))

它在

上出错
nlp.update(texts, annotations, sgd=optimizer, drop=0.35,
                           losses=losses)

根据 spaCy 上此模型的文档 here,此模型尚不支持命名实体识别。仅支持:

  • sentencizer
  • trf_wordpiecer
  • trf_tok2vec

您可以像这样获取给定模型的可用管道:

>>> import spacy

>>> nlp = spacy.load("en_trf_bertbaseuncased_lg")
>>> nlp.pipe_names
[sentencizer, trf_wordpiecer, trf_tok2vec]