文本分类器
文本分类器用于预测整个文档的类别,它有两种变体:textcat和textcat_multilabel。当您需要为每个文档预测一个确定的真实标签时,使用具有互斥标签的textcat。如果您想执行多标签分类并为每个文档预测零个、一个或多个真实标签,请改用textcat_multilabel组件。对于二元分类任务,您可以使用带有两个标签的textcat或带有一个标签的textcat_multilabel。
这两个组件都在本页有详细说明。
Assigned Attributes
预测结果将保存到doc.cats作为字典,其中键是类别名称,值是0到1之间的分数(包含边界值)。对于textcat(互斥类别),分数总和为1,而对于textcat_multilabel则没有特定的总和保证。这也意味着对于textcat,缺失值等同于0值(即False),并会作为0值计入损失和评分指标。但textcat_multilabel的情况不同,黄金标准数据中的缺失值不会影响损失或准确率计算。
请注意,在创建训练数据分配值时,每个类别的分数必须为0或1。不支持使用其他值,例如创建一个文档属于类别A一点又属于类别B一点的情况。
| 位置 | 值 |
|---|---|
Doc.cats | Category scores. Dict[str, float] |
配置与实现
默认配置由管道组件工厂定义,描述了组件应如何配置。您可以通过nlp.add_pipe中的config参数或在训练用的config.cfg中覆盖其设置。有关架构及其参数和超参数的详细信息,请参阅模型架构文档。
| 设置 | 描述 |
|---|---|
threshold | Cutoff to consider a prediction “positive”, relevant for textcat_multilabel when calculating accuracy scores. float |
model | A model instance that predicts scores for each category. Defaults to TextCatEnsemble. Model[List[Doc], List[Floats2d]] |
scorer | The scoring method. Defaults to Scorer.score_cats for the attribute "cats". Optional[Callable] |
explosion/spaCy/master/spacy/pipeline/textcat.py
explosion/spaCy/master/spacy/pipeline/textcat_multilabel.py
TextCategorizer.__init__ 方法
创建一个新的管道实例。在您的应用程序中,通常会使用快捷方式,通过其字符串名称并使用nlp.add_pipe来实例化该组件。
| 名称 | 描述 |
|---|---|
vocab | The shared vocabulary. Vocab |
model | The Thinc Model powering the pipeline component. Model[List[Doc], List[Floats2d]] |
name | String name of the component instance. Used to add entries to the losses during training. str |
| 仅关键字 | |
threshold | Cutoff to consider a prediction “positive”, relevant for textcat_multilabel when calculating accuracy scores. float |
scorer | The scoring method. Defaults to Scorer.score_cats for the attribute "cats". Optional[Callable] |
TextCategorizer.__call__ 方法
将管道应用于单个文档。文档会被原地修改并返回。
这通常在调用nlp对象处理文本时自动完成,
所有管道组件会按顺序应用于Doc对象。
__call__和pipe
都会委托给predict和
set_annotations方法。
| 名称 | 描述 |
|---|---|
doc | The document to process. Doc |
| 返回值 | 处理后的文档。Doc |
TextCategorizer.pipe 方法
将管道应用于文档流。这通常在调用nlp对象处理文本时自动完成,所有管道组件会按顺序应用于Doc对象。无论是__call__还是pipe方法,最终都会委托给predict和set_annotations方法执行。
| 名称 | 描述 |
|---|---|
stream | A stream of documents. Iterable[Doc] |
| 仅关键字 | |
batch_size | The number of documents to buffer. Defaults to 128. int |
| YIELDS | 按顺序处理后的文档。Doc |
TextCategorizer.initialize 方法v3.0
初始化组件以进行训练。get_examples应为一个返回可迭代Example对象的函数。至少需要提供一个示例。这些数据示例用于初始化组件模型,可以是完整的训练数据或代表性样本。初始化过程包括验证网络、推断缺失形状以及根据数据设置标签方案。该方法通常由Language.initialize调用,并允许您通过配置中的[initialize.components]块来自定义接收的参数。
| 名称 | 描述 |
|---|---|
get_examples | Function that returns gold-standard annotations in the form of Example objects. Must contain at least one Example. Callable[[], Iterable[Example]] |
| 仅关键字 | |
nlp | The current nlp object. Defaults to None. Optional[Language] |
labels | The label information to add to the component, as provided by the label_data property after initialization. To generate a reusable JSON file from your data, you should run the init labels command. If no labels are provided, the get_examples callback is used to extract the labels from the data, which may be a lot slower. Optional[Iterable[str]] |
positive_label | The positive label for a binary task with exclusive classes, None otherwise and by default. This parameter is only used during scoring. It is not available when using the textcat_multilabel component. Optional[str] |
TextCategorizer.predict 方法
在不修改的情况下,将组件的模型应用于一批Doc对象。
| 名称 | 描述 |
|---|---|
docs | The documents to predict. Iterable[Doc] |
| 返回值 | 模型对每个文档的预测结果。 |
TextCategorizer.set_annotations 方法
使用预先计算的分数批量修改Doc对象。
| 名称 | 描述 |
|---|---|
docs | The documents to modify. Iterable[Doc] |
scores | The scores to set, produced by TextCategorizer.predict. |
TextCategorizer.update 方法
从一批包含预测和黄金标准注释的Example对象中学习,并更新组件的模型。委托给predict和get_loss。
| 名称 | 描述 |
|---|---|
examples | A batch of Example objects to learn from. Iterable[Example] |
| 仅关键字 | |
drop | The dropout rate. float |
sgd | An optimizer. Will be created via create_optimizer if not set. Optional[Optimizer] |
losses | Optional record of the loss during training. Updated using the component name as the key. Optional[Dict[str, float]] |
| RETURNS | The updated losses dictionary. Dict[str, float] |
TextCategorizer.rehearse 方法实验性v3.0
对一批数据执行“预演”更新。预演更新旨在教导当前模型做出与初始模型相似的预测,以尝试解决“灾难性遗忘”问题。此功能为实验性功能。
| 名称 | 描述 |
|---|---|
examples | A batch of Example objects to learn from. Iterable[Example] |
| 仅关键字 | |
drop | The dropout rate. float |
sgd | An optimizer. Will be created via create_optimizer if not set. Optional[Optimizer] |
losses | Optional record of the loss during training. Updated using the component name as the key. Optional[Dict[str, float]] |
| RETURNS | The updated losses dictionary. Dict[str, float] |
TextCategorizer.get_loss 方法
计算这批文档及其预测分数的损失和损失梯度。
| 名称 | 描述 |
|---|---|
examples | The batch of examples. Iterable[Example] |
scores | Scores representing the model’s predictions. |
| RETURNS | The loss and the gradient, i.e. (loss, gradient). Tuple[float, float] |
TextCategorizer.score 方法v3.0
对一批示例进行评分。
| 名称 | 描述 |
|---|---|
examples | The examples to score. Iterable[Example] |
| 仅关键字 | |
| RETURNS | The scores, produced by Scorer.score_cats. Dict[str, Union[float, Dict[str, float]]] |
TextCategorizer.create_optimizer 方法
为管道组件创建一个优化器。
| 名称 | 描述 |
|---|---|
| 返回值 | 优化器。Optimizer |
TextCategorizer.use_params 方法上下文管理器
修改管道的模型以使用给定的参数值。
| 名称 | 描述 |
|---|---|
params | The parameter values to use in the model. dict |
TextCategorizer.add_label 方法
向管道添加一个新标签。如果输出维度已设置,或模型已完全初始化,则会引发错误。请注意,如果您向initialize方法提供了代表性数据样本,则无需调用此方法。在这种情况下,样本中发现的所有标签将自动添加到模型中,输出维度将自动推断。
| 名称 | 描述 |
|---|---|
label | The label to add. str |
| RETURNS | 0 if the label is already present, otherwise 1. int |
TextCategorizer.to_disk 方法
将管道序列化到磁盘。
| 名称 | 描述 |
|---|---|
path | A path to a directory, which will be created if it doesn’t exist. Paths may be either strings or Path-like objects. Union[str,Path] |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
TextCategorizer.from_disk 方法
从磁盘加载管道。就地修改对象并返回它。
| 名称 | 描述 |
|---|---|
path | A path to a directory. Paths may be either strings or Path-like objects. Union[str,Path] |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The modified TextCategorizer object. TextCategorizer |
TextCategorizer.to_bytes 方法
将管道序列化为字节串。
| 名称 | 描述 |
|---|---|
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The serialized form of the TextCategorizer object. bytes |
TextCategorizer.from_bytes 方法
从字节串加载管道。原地修改对象并返回它。
| 名称 | 描述 |
|---|---|
bytes_data | The data to load from. bytes |
| 仅关键字 | |
exclude | String names of serialization fields to exclude. Iterable[str] |
| RETURNS | The TextCategorizer object. TextCategorizer |
TextCategorizer.labels 属性
当前添加到组件中的标签。
| 名称 | 描述 |
|---|---|
| 返回值 | 添加到组件的标签。Tuple[str, …] |
TextCategorizer.label_data 属性v3.0
当前添加到组件的标签及其内部元信息。
这是由init labels生成的数据,并被
TextCategorizer.initialize用于
使用预定义的标签集初始化模型。
| 名称 | 描述 |
|---|---|
| 返回值 | 添加到组件的标签数据。Tuple[str, …] |
序列化字段
在序列化过程中,spaCy会导出多个用于恢复对象不同方面的数据字段。如果需要,您可以通过exclude参数传入字符串名称来将它们排除在序列化之外。
| 名称 | 描述 |
|---|---|
vocab | The shared Vocab. |
cfg | The config file. You usually don’t want to exclude this. |
model | The binary model data. You usually don’t want to exclude this. |