python - 如何解决错误:nlp.add_pipe` 现在采用已注册组件工厂的字符串名称,而不是可调用组件
问题描述
我正在使用空白的 spacy 模型来训练我自己的 ner 数据。我正在训练我的模型以获取来自 train_data 的实体。
nlp = spacy.blank('en')
def train_model(train_data) :
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner)
for _ ,annotation in train_data :
for ent in annotation['entities']:
ner.add_label(ent[2])
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(15):
print("Starting Iteration" + str(itn))
random.shuffle(train_data)
losses = {}
index = 0
for text, annotations in train_data :
try:
nlp.update([text], [annotations], sgd=optimizer, drop=0.20,
losses=losses)
except Exception as e:
pass
print('Losses', losses)
我收到以下错误。
nlp.add_pipe
现在采用已注册组件工厂的字符串名称,而不是可调用组件。预期的字符串,但在 0x0000022969A29C88> 处得到 <spacy.pipeline.ner.EntityRecognizer 对象>(名称:'None')。
如果您使用以下命令创建了组件
nlp.create_pipe('name')
:删除 nlp.create_pipe 并nlp.add_pipe('name')
改为调用。如果您传入了一个组件,例如
TextCategorizer()
:nlp.add_pipe
使用字符串名称调用,例如nlp.add_pipe('textcat')
。如果您正在使用自定义组件:将装饰器
@Language.component
(用于功能组件)或@Language.factory
(用于类组件/工厂)添加到您的自定义组件并为其分配一个名称,例如@Language.component('your_name')
. 然后,您可以运行nlp.add_pipe('your_name')
以将其添加到管道中。
解决方案
关于错误 - 替换
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner)
和
ner = nlp.add_pipe('ner')
推荐阅读
- reactjs - 从 React Native 中的 api 拦截器(组件外部)重定向到屏幕
- css - 使 CSS 图像背景变暗
- c# - Unity3D:GetComponent(string)和GetComponent(Type)之间的结果差异?
- linux - rsyslogd-3000:连接()处的 omazuremds 错误。errno=没有这样的文件或目录
- html - HTML 找不到样式表信息
- node.js - 过滤和更新数据
- java - 打印带有敏感数据的 HTTPClient 请求参数
- node.js - 监听其他应用程序中发生的事件
- java - Rabbitmq 连接被拒绝,在 linux 服务器上的 docker 容器内运行
- python - Python 函数从另一个函数调用变量。但为什么?