无法加载Spacy en_core_web_trf。

12

如自指南所述,我已经使用(conda环境)进行了安装。

conda install -c conda-forge spacy
python -m spacy download en_core_web_trf

我已经安装了spacy-transformers,但是当我简单地执行以下操作时:
import spacy
spacy.load("en_core_web_trf")

它向我显示了这个错误:

ValueError: [E002] Can't find factory for 'transformer' for language English (en). This usually happens when spaCy calls `nlp.create_pipe` with a custom component name that's not registered on the current language class. If you're using a Transformer, make sure to install 'spacy-transformers'. If you're using a custom component, make sure you've added the decorator `@Language.component` (for function components) or `@Language.factory` (for class components).

Available factories: attribute_ruler, tok2vec, merge_noun_chunks, merge_entities, merge_subtokens, token_splitter, parser, beam_parser, entity_linker, ner, beam_ner, entity_ruler, lemmatizer, tagger, morphologizer, senter, sentencizer, textcat, spancat, textcat_multilabel, en.lemmatizer

更多关于错误的信息:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11108/2648447056.py in <module>
----> 1 nlp_en = spacy.load("en_core_web_trf")

~\Anaconda3\envs\rl\lib\site-packages\spacy\__init__.py in load(name, vocab, disable, exclude, config)
     49     RETURNS (Language): The loaded nlp object.
     50     """
---> 51     return util.load_model(
     52         name, vocab=vocab, disable=disable, exclude=exclude, config=config
     53     )

~\Anaconda3\envs\rl\lib\site-packages\spacy\util.py in load_model(name, vocab, disable, exclude, config)
    345             return get_lang_class(name.replace("blank:", ""))()
    346         if is_package(name):  # installed as package
--> 347             return load_model_from_package(name, **kwargs)
    348         if Path(name).exists():  # path to model data directory
    349             return load_model_from_path(Path(name), **kwargs)

~\Anaconda3\envs\rl\lib\site-packages\spacy\util.py in load_model_from_package(name, vocab, disable, exclude, config)
    378     """
    379     cls = importlib.import_module(name)
--> 380     return cls.load(vocab=vocab, disable=disable, exclude=exclude, config=config)
    381 
    382 

~\Anaconda3\envs\rl\lib\site-packages\en_core_web_trf\__init__.py in load(**overrides)
      8 
      9 def load(**overrides):
---> 10     return load_model_from_init_py(__file__, **overrides)

~\Anaconda3\envs\rl\lib\site-packages\spacy\util.py in load_model_from_init_py(init_file, vocab, disable, exclude, config)
    538     if not model_path.exists():
    539         raise IOError(Errors.E052.format(path=data_path))
--> 540     return load_model_from_path(
    541         data_path,
    542         vocab=vocab,

~\Anaconda3\envs\rl\lib\site-packages\spacy\util.py in load_model_from_path(model_path, meta, vocab, disable, exclude, config)
    413     overrides = dict_to_dot(config)
    414     config = load_config(config_path, overrides=overrides)
--> 415     nlp = load_model_from_config(config, vocab=vocab, disable=disable, exclude=exclude)
    416     return nlp.from_disk(model_path, exclude=exclude, overrides=overrides)
    417 

~\Anaconda3\envs\rl\lib\site-packages\spacy\util.py in load_model_from_config(config, vocab, disable, exclude, auto_fill, validate)
    450     # registry, including custom subclasses provided via entry points
    451     lang_cls = get_lang_class(nlp_config["lang"])
--> 452     nlp = lang_cls.from_config(
    453         config,
    454         vocab=vocab,

~\Anaconda3\envs\rl\lib\site-packages\spacy\language.py in from_config(cls, config, vocab, disable, exclude, meta, auto_fill, validate)
   1712                     # The pipe name (key in the config) here is the unique name
   1713                     # of the component, not necessarily the factory
-> 1714                     nlp.add_pipe(
   1715                         factory,
   1716                         name=pipe_name,

~\Anaconda3\envs\rl\lib\site-packages\spacy\language.py in add_pipe(self, factory_name, name, before, after, first, last, source, config, raw_config, validate)
    774                     lang_code=self.lang,
    775                 )
--> 776             pipe_component = self.create_pipe(
    777                 factory_name,
    778                 name=name,

~\Anaconda3\envs\rl\lib\site-packages\spacy\language.py in create_pipe(self, factory_name, name, config, raw_config, validate)
    639                 lang_code=self.lang,
    640             )
--> 641             raise ValueError(err)
    642         pipe_meta = self.get_factory_meta(factory_name)
    643         # This is unideal, but the alternative would mean you always need to

1
如果你遇到了这个错误,那么说明 spacy-transformers 没有被安装,或者它被安装在了另一个环境中,无法找到。请尝试重新安装它。 - polm23
@polm23谢谢您的回答!我已经重置了我的电脑,现在它可以正常工作,而且没有触碰任何东西。也许之前conda不能够正确加载,就在我关闭电脑之前? - Omar
1
听起来可能是你的PATH变量没有更新。只要它能工作就好了。 - polm23
3个回答

20

对于那些尝试过这个解决方案但仍然无法使其工作的人,有一些他们没有提到的事情(因为它很琐碎),但是让我盯着屏幕看了很长时间,就是在此之后:

!pip install spacy-transformers

您仍需放置

import spacy_transformers

在你的代码顶部。

10

确定您已经安装了spacy-transformers吗? 在安装spacy之后?

我使用pip进行安装: pip install spacy-transformers 我没有遇到加载en_core_web_trf时的任何问题。


1
谢谢你的回答!我已经安装了Spacy-transformers。我重启了电脑,现在它可以工作了。不知道为什么之前不能工作。 - Omar
2
软件包安装命令实际上是"pip install spacy[transformers]",在Windows上,我还需要系统重启才能使一切正常工作。 - negfrequency

4
# !pip install spacy
# !pip install spacy-transformers
# !python3 -m spacy download en_core_web_trf

如果您正在使用Google Colab,那么它的运行效果很好,请使用以下方法:

# !pip install spacy==(last version of spacy(3.2.4)) 

其余的都一样,Colab下载的是旧版本。希望你能得到答案!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接