训练基本的 spacy 文本分类模型

GSw*_*art 5 python nlp spacy

我正在尝试使用 spaCy 训练基本文本分类模型。我有一个文本列表,我想构建一个模型,将文本分类为outcome1outcome2。假设我的数据如下所示:

texts = ["This is the first example text",
         "This is the second example text",
         "This is yet another text"]
y = ["outcome2", "outcome1", "outcome1"]
Run Code Online (Sandbox Code Playgroud)

我的问题是,我什至无法将文本处理成文档:

nlp = spacy.blank("en")

textcat = nlp.create_pipe("textcat")
textcat.add_label("outcome1")
textcat.add_label("outcome2")
textcat = nlp.add_pipe("textcat", last = True)

nlp.pipe_names
Run Code Online (Sandbox Code Playgroud)
>>> ['textcat']
Run Code Online (Sandbox Code Playgroud)

但是当我尝试处理任何文本时,我收到错误:

doc = nlp("This is a sentence")
Run Code Online (Sandbox Code Playgroud)
>>> ValueError: Cannot get dimension 'nO' for model 'sparse_linear': value unset
Run Code Online (Sandbox Code Playgroud)

我尝试按照教程(有点过时)并使用spaCy 快速启动小部件设置一个项目,但在初始化配置文件时我不断遇到错误。我缺少什么?

ˆᵛˆ*_*ˆᵛˆ 1

实际上我在这里发现了一个非常相似的讨论,这正是这个问题所要问的: https: //github.com/explosion/spaCy/discussions/9732

讨论指出,您必须指定标签、训练模型并对其进行初始化,然后才能使用。此外,从版本 3 开始,不建议使用您自己的训练循环进行训练,而是使用新的配置系统并让 spacy 为您处理训练。请参阅: https: //spacy.io/usage/training