文本分类 -> 架构额外字段不允许。

7

我一直试图用PyCharm练习我从这个教程学到的内容:(https://realpython.com/sentiment-analysis-python/)。

而这行代码:

textcat.add_label("pos")

产生了警告:在“(Doc) -> Doc | (Doc) -> Doc”中找不到引用“add_label”

我知道这是因为"nlp.create_pipe()"生成的是一个Doc,而不是字符串。但是(基本上因为我不知道该怎么做!),我还是运行了脚本,但随后我从这一行得到了一个错误:

textcat = nlp.create_pipe("textcat", config={"architecture": "simple_cnn"})

错误信息:

raise ConfigValidationError(
thinc.config.ConfigValidationError:

Config validation error

textcat -> architecture extra fields not permitted

{'nlp': <spacy.lang.en.English object at 0x0000015E74F625E0>, 'name': 'textcat', 'architecture': 'simple_cnn', 'model': {'@architectures': 'spacy.TextCatEnsemble.v2', 'linear_model': {'@architectures': 'spacy.TextCatBOW.v1', 'exclusive_classes': True, 'ngram_size': 1, 'no_output_layer': False}, 'tok2vec': {'@architectures': 'spacy.Tok2Vec.v2', 'embed': {'@architectures': 'spacy.MultiHashEmbed.v1', 'width': 64, 'rows': [2000, 2000, 1000, 1000, 1000, 1000], 'attrs': ['ORTH', 'LOWER', 'PREFIX', 'SUFFIX', 'SHAPE', 'ID'], 'include_static_vectors': False}, 'encode': {'@architectures': 'spacy.MaxoutWindowEncoder.v2', 'width': 64, 'window_size': 1, 'maxout_pieces': 3, 'depth': 2}}}, 'threshold': 0.5, '@factories': 'textcat'}

我的使用环境:

  • Pycharm版本号: 2019.3.4
  • Python版本号: 3.8.6
  • spaCy版本号: 3.0.5

1
看起来这个教程是针对spaCy v2的,但你正在使用v3。在v3中,管道添加的方式发生了重大变化;我建议你查看快速入门并按照其中的步骤进行操作,它可以生成一个textcat示例。https://spacy.io/usage/training#quickstart - polm23
确实,您说得对先生,这是因为我使用的Spacy版本比教程作者更新。谢谢。 - Amira
5个回答

6

哎呀!那次完整的 spaCy 升级真的摧毁了那篇教程,是吧...

有几个问题可能可以解决。我还没有完全修复那篇破损的教程,它在待办事项清单上。然而,我已经找到了解决你遇到的确切问题的方法。

textcat = nlp.create_pipe("textcat", config={"architecture": "simple_cnn"})

create_pipe行为已过时,您可以直接使用add_pipe将其添加到工作流程中。因此,您可以执行以下操作之一:

from spacy.pipeline.textcat import single_label_cnn_config

<more good code>

nlp = spacy.load("en_core_web_trf")
if "textcat" not in nlp.pipe_names:
     nlp.add_pipe('textcat', config=single_label_cnn_config, last=True)
textcat = nlp.get_pipe('textcat')
textcat.add_label("pos")
textcat.add_label("neg")

请告诉我这是否有意义并且是否有帮助。 我将尝试在接下来的几周内彻底改进从spaCy开始的教程。


非常感谢您的回复,但不幸的是,出现了更多的错误,由于我完全是新手,无法解决它们,所以我又降级到了spacy 2.3.5,但我会关注您的教程,因为对于像我这样的初学者来说非常清晰,我将在新教程中回答我的问题。 - Amira
3
你好,我尝试使用你的add_pipe解决方案,但是遇到以下错误(Spacy 3.0.6):ValueError: [E962]针对管道“textcat”的配置不正确。预期是字典,而实际上是<class 'str'>。 - Vaibhav
@jlarks32 你好!我和前面的评论者有同样的问题。如果您知道如何解决,请更新您的答案。谢谢! - Egor

4
这似乎已经在 spacy 3.1.0 中奏效。
import en_core_web_md # or skip, see below
from spacy.pipeline.textcat import Config, single_label_cnn_config

nlp = en_core_web_md.load() # or nlp=spacy.load("en_core_web_sm")

config = Config().from_str(single_label_cnn_config)
if "textcat" not in nlp.pipe_names:
     nlp.add_pipe('textcat', config=config, last=True)

nlp.pipe_names
# ['tok2vec', 'tagger', 'parser', 'attribute_ruler', 'lemmatizer', 'ner', 'textcat']

3
感谢@polm23的帮助,整个问题是因为我使用的spaCy版本比教程作者更新。
我的版本:3.0.5 作者版本:2.3.2
目前,我只需将我的spaCy降级即可跟上教程。

1
尝试以下代码,适用于spacy 3.x:

# Import spaCy, load large model (folders) which is in project path
import spacy
nlp= spacy.load(r'en_core_web_lg\en_core_web_lg-3.1.0')

# Adding the built-in textcat component to the pipeline.
textcat = nlp.add_pipe("textcat")
nlp.pipe_names

# Adding the labels to textcat
textcat.add_label("pos")
textcat.add_label("neg")

print(nlp.pipe_names)
# ['tok2vec', 'tagger', 'parser', 'attribute_ruler', 'lemmatizer', 'ner', 'textcat']

0

textcat -> architecture extra fields not permitted in spaCy错误问题已经解决。这个问题可以通过降级spaCy版本来解决。运行以下代码将spaCy版本降级到2.3.6。

pip install -U spacy==2.3.6

这对我来说是工作。希望这也能帮到你。


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