加载已经训练好的自定义模型:SPACY

问题描述 投票:0回答:1

我正在与 spacy 合作开发命名实体识别模型 (ner)。这是带有 2 个训练句子的基本模型的代码:


  

ner = nlp.create_pipe('ner')


labels = ['BOY', 'GIRL']
for label in labels:
    ner.add_label(label)





TRAIN_DATA = [
    ("Who is John ? He is lost", {"entities": [(7, 11, "BOY")]}),
    ("I like Lara. She likes you too", {"entities": [(7, 11, "GIRL")]}),
]
examples = []
for text, annots in TRAIN_DATA:
    examples.append(Example.from_dict(nlp.make_doc(text), annots))

for i in range(40):
    random.shuffle(examples)
    for batch in minibatch(examples,size=10):
        nlp.update(batch)
text = "Where is Sara. She is likes you ?"
doc = nlp(text)
for ent in doc.ents:
    print(ent.text, ent.label_)

然后我保存模型以供以后使用。

nlp.to_disk('Test') #save
spacy.load('./Test')

不幸的是,当加载我的自定义模板时,我收到此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[72], line 2
      1 nlp.to_disk('Test') #save
----> 2 spacy.load('./Test')

File c:\Users\...\venv\Lib\site-packages\spacy\__init__.py:54, in load(name, vocab, disable, enable, exclude, config)
     30 def load(
     31     name: Union[str, Path],
     32     *,
   (...)
     37     config: Union[Dict[str, Any], Config] = util.SimpleFrozenDict(),
     38 ) -> Language:
     39     """Load a spaCy model from an installed package or a local path.
     40 
     41     name (str): Package name or model path.
   (...)
     52     RETURNS (Language): The loaded nlp object.
     53     """
---> 54     return util.load_model(
     55         name,
     56         vocab=vocab,
     57         disable=disable,
     58         enable=enable,
     59         exclude=exclude,
...
    191     err = f"Attempt to change dimension '{name}' for model '{self.name}' from {old_value} to {value}"
--> 192     raise ValueError(err)
    193 self._dims[name] = value

ValueError: Attempt to change dimension 'nV' for model 'hashembed' from 2000 to 5000
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings.

为什么我有这个而不是在空白模型上,当我保存它时一切正常。

如何使用这个定制模板?

python spacy
1个回答
0
投票

尝试此链接中的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.