开始使用免费开始使用

训练前的准备步骤

在训练 spaCy 模型之前和训练过程中,您需要:(1)禁用其他流水线组件,只训练目标组件;(2)将一条训练数据点的 Doc 容器及其对应的 annotations 转换为 Example 类。

在本练习中,您将通过使用已预加载、可通过 nlp 访问的 en_core_web_sm 模型来练习这两个步骤。Example 类已导入,text 字符串和相关的 annotations 也已提供供您使用。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • ner 外,禁用 nlp 模型的所有流水线组件。
  • text 字符串及其 annotations 转换为可用于训练的正确格式。

交互式实操练习

通过完成这段示例代码来试试这个练习。

nlp = spacy.load("en_core_web_sm")

# Disable all pipeline components of  except `ner`
other_pipes = [____ for ____ in nlp.____ if ____ != 'ner']
nlp.____(*other_pipes)

# Convert a text and its annotations to the correct format usable for training
doc = nlp.____(text)
example = Example.____(____, ____)
print("Example object for training: \n", example.to_dict())
编辑并运行代码