python - 在 spacy 3 中更新 ner 模型时出错,有什么建议吗?
问题描述
我目前正在从fr_core_news_lg
管道更新 NER 模型。当我最后一次使用它时,该代码大约在 1 或 2 个月前工作。但是现在,发生了一些事情,我不能再运行它了。我没有对代码进行任何更改,只是想再次运行它。但我收到以下错误:
Traceback (most recent call last):
File "../nermodel.py", line 174, in <module>
ner_model.train(med_label)
File "../nermodel.py", line 102, in train
optimizer = self.nlp.entity.create_optimizer()
AttributeError: 'French' object has no attribute 'entity'
错误指向我用新示例更新我的 NER 模型的代码部分:
def train(self, label, n_iter=10, batch_size=50):
# creating an optimizer and selecting a list of pipes NOT to train
optimizer = self.nlp.entity.create_optimizer()
other_pipes = [pipe for pipe in self.nlp.pipe_names if pipe != 'ner']
# adding a named entity label
ner = self.nlp.get_pipe('ner')
ner.add_label(label)
with self.nlp.disable_pipes(*other_pipes):
for itn in range(n_iter):
random.shuffle(self.train_data)
losses = {}
# batch the examples and iterate over them
for batch in spacy.util.minibatch(self.train_data, size=batch_size):
texts = [text for text, entities in batch]
annotations = [entities for text, entities in batch]
# update the model
self.nlp.update(texts, annotations, sgd=optimizer, losses=losses)
print(losses)
print("Final loss: ", losses)
单个训练示例,以便 NER 了解“咨询”是一个实体,如下所示:
('et la consultation post-réanimation', {'entities': [(6, 18, 'MEDICAL_TERM')]})
我已将 SpaCy 更新到最新版本,并再次下载了fr_core_news_lg
模型,甚至在新的 python 环境中尝试过,但无济于事。这让我觉得管道或 SpaCy 库发生了变化。谷歌搜索,我无法找到确切的答案。有人可以解决这个问题吗?
编辑:提供了更多细节。
解决方案
我认为这段代码应该适合你:
def train(self, label, n_iter=10, batch_size=50):
# creating an optimizer and selecting a list of pipes NOT to train
optimizer = self.nlp.create_optimizer()
other_pipes = [pipe for pipe in self.nlp.pipe_names if pipe != 'ner']
# adding a named entity label
ner = self.nlp.get_pipe('ner')
ner.add_label(label)
with self.nlp.disable_pipes(*other_pipes):
for itn in range(n_iter):
random.shuffle(self.train_data)
losses = {}
# batch the examples and iterate over them
for batch in spacy.util.minibatch(self.train_data, size=batch_size):
for text, annotations in batch:
doc = nlp.make_doc(text)
example = Example.from_dict(doc, annotations)
nlp.update([example], drop=0.35, sgd=optimizer, losses=losses)
print(losses)
print("Final loss: ", losses)
为了进一步分解它,在 spacy 3 中有两个变化:
- 他们摆脱了实体
nlp.entity.create_optimizer()
- 我们不直接将文本和注释传递给
nlp.update()
但与Example
推荐阅读
- python - 动画未在 px.scatter_geo 中排序
- python - python中的不同for循环
- javascript - 当通知在通知中心(Windows 10)中“隐藏”时,Firebase 消息传递站点导航不起作用
- python - Python Pandas Dataframe - 行级操作
- rest - 使用 Rust/reqwest crate 的 Binance API 密钥错误
- angular - 使用 ng-packagr(Angular 库)将 lib 资产复制到目标目录
- java - 嵌入式 Redis 服务器无法启动
- mysql - 在选择语句中显示时间范围的月份
- python - 在 App 中的不同 Python 文件中查询时模型出现循环导入错误
- woocommerce - wcs_get_subscriptions 和 update_manual 不起作用