无法使用 CLI 在 spacy 3.0 中评估自定义 ner
Can't evaluate custom ner in spacy 3.0 using CLI
我正在尝试从磁盘加载带有自定义管道的自定义预训练模型作为 spacy 3.0 中的管道:
工厂的代码是这样的:
@CustomEng.factory("ner-crf")
def create_my_component(nlp, name):
crf_extractor = CRFExtractor().from_disk("path-to-model")
return CRFEntityExtractor(nlp, crf_extractor=crf_extractor)
然后我将 'ner-crf' 添加到我的语言 class 中,如下所示:
nlp = spacy.blank('custom-eng')
nlp.add_pipe('ner-crf')
nlp.to_disk('../model')
有一件事我认为可能是相关的:当我使用 to_disk
来保存 nlp
对象时,没有 ner-crf
包(我刚刚添加的管道)保存的对象。
然后我 运行 这个 CLI 命令来评估 NER 管道:
python -m spacy evaluate ../model/ ../corpus/dev.spacy --output ../model/metrics.json --gpu-id 0 --code ../../../spacy_utils/custom-eng/__init__.py
但是我得到这个错误:
Traceback (most recent call last):
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/__main__.py", line 4, in <module>
setup_cli()
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/_util.py", line 69, in setup_cli
command(prog_name=COMMAND)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/typer/main.py", line 497, in wrapper
return callback(**use_params) # type: ignore
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/evaluate.py", line 42, in evaluate_cli
evaluate(
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/evaluate.py", line 75, in evaluate
nlp = util.load_model(model)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 326, in load_model
return load_model_from_path(Path(name), **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 392, in load_model_from_path
return nlp.from_disk(model_path, exclude=exclude)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/language.py", line 1883, in from_disk
util.from_disk(path, deserializers, exclude)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 1176, in from_disk
reader(path / key)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/language.py", line 1877, in <lambda>
deserializers[name] = lambda p, proc=proc: proc.from_disk(
TypeError: from_disk() got an unexpected keyword argument 'exclude'
我使用的自定义 NER classes 属于 spacy-crfsuite 库,它在 spacy 2 中运行良好,但它们还没有 Spacy 3 的示例代码,所以我正在尝试使其运行我自己在 spacy 3.0 中。
从 spaCy v3.0 开始,管道组件应在其 to_disk
方法中支持 exclude
关键字。你可以在你的函数中添加exclude
关键字,给它一个默认值,并且干脆不在函数体中使用它的值,这个错误应该可以解决。
为了完整起见,这里是从 v2 到 v3 的迁移指南,其中可能包含一些其他有趣的提示:https://spacy.io/usage/v3#migrating
我正在尝试从磁盘加载带有自定义管道的自定义预训练模型作为 spacy 3.0 中的管道:
工厂的代码是这样的:
@CustomEng.factory("ner-crf")
def create_my_component(nlp, name):
crf_extractor = CRFExtractor().from_disk("path-to-model")
return CRFEntityExtractor(nlp, crf_extractor=crf_extractor)
然后我将 'ner-crf' 添加到我的语言 class 中,如下所示:
nlp = spacy.blank('custom-eng')
nlp.add_pipe('ner-crf')
nlp.to_disk('../model')
有一件事我认为可能是相关的:当我使用 to_disk
来保存 nlp
对象时,没有 ner-crf
包(我刚刚添加的管道)保存的对象。
然后我 运行 这个 CLI 命令来评估 NER 管道:
python -m spacy evaluate ../model/ ../corpus/dev.spacy --output ../model/metrics.json --gpu-id 0 --code ../../../spacy_utils/custom-eng/__init__.py
但是我得到这个错误:
Traceback (most recent call last):
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/__main__.py", line 4, in <module>
setup_cli()
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/_util.py", line 69, in setup_cli
command(prog_name=COMMAND)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/typer/main.py", line 497, in wrapper
return callback(**use_params) # type: ignore
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/evaluate.py", line 42, in evaluate_cli
evaluate(
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/cli/evaluate.py", line 75, in evaluate
nlp = util.load_model(model)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 326, in load_model
return load_model_from_path(Path(name), **kwargs)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 392, in load_model_from_path
return nlp.from_disk(model_path, exclude=exclude)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/language.py", line 1883, in from_disk
util.from_disk(path, deserializers, exclude)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/util.py", line 1176, in from_disk
reader(path / key)
File "/home/marzi/anaconda3/envs/spacy-tutorial/lib/python3.8/site-packages/spacy/language.py", line 1877, in <lambda>
deserializers[name] = lambda p, proc=proc: proc.from_disk(
TypeError: from_disk() got an unexpected keyword argument 'exclude'
我使用的自定义 NER classes 属于 spacy-crfsuite 库,它在 spacy 2 中运行良好,但它们还没有 Spacy 3 的示例代码,所以我正在尝试使其运行我自己在 spacy 3.0 中。
从 spaCy v3.0 开始,管道组件应在其 to_disk
方法中支持 exclude
关键字。你可以在你的函数中添加exclude
关键字,给它一个默认值,并且干脆不在函数体中使用它的值,这个错误应该可以解决。
为了完整起见,这里是从 v2 到 v3 的迁移指南,其中可能包含一些其他有趣的提示:https://spacy.io/usage/v3#migrating